Opened 7 years ago
Closed 20 months ago
#11726 closed Bugs (fixed)
boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1
| Reported by: | davidlt | Owned by: | Joel de Guzman |
|---|---|---|---|
| Milestone: | To Be Determined | Component: | spirit |
| Version: | Boost 1.57.0 | Severity: | Problem |
| Keywords: | spirit | Cc: |
Description
Boost Spirit is triggered undefined behaviour in C++.
#include <iostream>
#include <boost/spirit/home/support/detail/endian.hpp>
int main(void) {
const unsigned char raw_bytes[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
long long result = boost::spirit::detail::load_little_endian<long long, 8ul>(raw_bytes);
std::cout << result << std::endl;
return 0;
}
or
#include <iostream>
#include <memory>
#include <tuple>
#include <boost/spirit/home/support/detail/endian.hpp>
int main(void) {
long long* p;
std::size_t sz;
std::tie(p, sz) = std::get_temporary_buffer<long long>(1);
assert(sz != 0);
boost::spirit::detail::store_little_endian<long long, 8ul>(p, -1);
long long result2 = boost::spirit::detail::load_little_endian<long long, 8ul>(p);
std::cout << result2 << std::endl;
return 0;
}
Compile:
g++ -fsanitize=undefined -fno-omit-frame-pointer -g -std=c++14 -O2 test.cpp
Used GCC 4.9.3 in my case.
Result:
/usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 /usr/include/boost/spirit/home/support/detail/endian/endian.hpp:76:57: runtime error: left shift of negative value -1 -1
Tested with Boost 1.57, but code has not been changed in master.
I guess, this will trigger undefined behaviour every time a singed type is used. Reference: C++ standard 5.8 "Shift operators" section.
From Boost:
75 static T load_little(const unsigned char* bytes)
76 { return *bytes | (next::load_little(bytes + 1) << 8); }
Attachments (1)
Change History (3)
comment:1 by , 7 years ago
by , 7 years ago
| Attachment: | 0001-Fix-undefined-behavior-in-.-support-detail-endian-en.patch added |
|---|
proposed patch (same as on GitHub)
comment:2 by , 20 months ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.

Patch proposed: https://github.com/boostorg/spirit/pull/161