ConvertDiskImage: fix undefined behavior

This commit is contained in:
Wolfgang Thaller 2019-09-29 21:51:04 +02:00
parent 3d84d382ad
commit 37cb3b81e8
2 changed files with 13 additions and 4 deletions

View File

@ -1,2 +1,4 @@
add_executable(ConvertDiskImage ConvertDiskImage.cc) add_executable(ConvertDiskImage ConvertDiskImage.cc)
target_link_libraries(ConvertDiskImage ResourceFiles) target_link_libraries(ConvertDiskImage ResourceFiles)
install(TARGETS ConvertDiskImage RUNTIME DESTINATION bin)

View File

@ -6,7 +6,7 @@
#include <cassert> #include <cassert>
#include <vector> #include <vector>
void decompreassADC(std::vector<uint8_t>& outbuf, const std::vector<uint8_t>& inbuf) void decompressADC(std::vector<uint8_t>& outbuf, const std::vector<uint8_t>& inbuf)
{ {
outbuf.reserve(0x40000); outbuf.reserve(0x40000);
auto p = inbuf.begin(); auto p = inbuf.begin();
@ -19,6 +19,7 @@ void decompreassADC(std::vector<uint8_t>& outbuf, const std::vector<uint8_t>& in
int n = (cmd & 0x7f) + 1; int n = (cmd & 0x7f) + 1;
outbuf.insert(outbuf.end(), p, p + n); outbuf.insert(outbuf.end(), p, p + n);
p += n; p += n;
assert(p <= inbuf.end());
} }
else else
{ {
@ -37,8 +38,14 @@ void decompreassADC(std::vector<uint8_t>& outbuf, const std::vector<uint8_t>& in
} }
++off; ++off;
outbuf.reserve(outbuf.size() + n); assert(n > 0);
outbuf.insert(outbuf.end(), outbuf.end() - off, outbuf.end() - off + n); assert(off > 0);
assert(outbuf.size() - off >= 0);
outbuf.resize(outbuf.size() + n);
auto dst = outbuf.end() - n;
auto src = dst - off;
while(n--)
*dst++ = *src++;
} }
} }
} }
@ -119,7 +126,7 @@ int main(int argc, char* argv[])
break; break;
case 0x83: case 0x83:
decompreassADC(outbuf, inbuf); decompressADC(outbuf, inbuf);
break; break;
} }