Opened 5 years ago

Closed 5 years ago

#13223 closed Bugs (fixed)

Undefined behavior inside assign_bits while importing data to cpp_int

Reported by: Dennis <yowidin@…> Owned by: John Maddock
Milestone: To Be Determined Component: multiprecision
Version: Boost 1.64.0 Severity: Problem
Keywords: Cc:

Description

Trying to import bits from unsigned char array results in undefined behavior as reported by Clang.

Minimal example:

#include <iostream>
#include <cstdint>

#include <boost/multiprecision/cpp_int.hpp>

using mp_int_t = boost::multiprecision::int128_t;
using source_t = std::vector<std::uint8_t>;

int main() {
   source_t src(16, 0);

   mp_int_t result;
   import_bits(result, src.begin(), src.end(), 0, false);

   return 0;
}

Compile command:

clang++ main.cpp \
   -g -O1 -fsanitize=undefined -fno-omit-frame-pointer \
   -fno-optimize-sibling-calls -std=c++11 -o main

Runtime output:

boost/multiprecision/cpp_int/import_export.hpp:64:21: runtime error: shift exponent 128 is too large for 8-bit type 'unsigned char'

Environment information:

  • Ubuntu 16.04 LTS 64bit
  • Tested with Clang 3.8.0 and Clang 5.0.0

Change History (1)

Note: See TracTickets for help on using tickets.