mirror of
https://github.com/qpdf/qpdf.git
synced 2024-11-14 16:54:03 +00:00
d71f05ca07
This makes all integer type conversions that have potential data loss explicit with calls that do range checks and raise an exception. After this commit, qpdf builds with no warnings when -Wsign-conversion -Wconversion is used with gcc or clang or when -W3 -Wd4800 is used with MSVC. This significantly reduces the likelihood of potential crashes from bogus integer values. There are some parts of the code that take int when they should take size_t or an offset. Such places would make qpdf not support files with more than 2^31 of something that usually wouldn't be so large. In the event that such a file shows up and is valid, at least qpdf would raise an error in the right spot so the issue could be legitimately addressed rather than failing in some weird way because of a silent overflow condition.
114 lines
1.9 KiB
C++
114 lines
1.9 KiB
C++
#include <qpdf/Pl_ASCIIHexDecoder.hh>
|
|
#include <qpdf/QTC.hh>
|
|
#include <stdexcept>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
Pl_ASCIIHexDecoder::Pl_ASCIIHexDecoder(char const* identifier, Pipeline* next) :
|
|
Pipeline(identifier, next),
|
|
pos(0),
|
|
eod(false)
|
|
{
|
|
this->inbuf[0] = '0';
|
|
this->inbuf[1] = '0';
|
|
this->inbuf[2] = '\0';
|
|
}
|
|
|
|
Pl_ASCIIHexDecoder::~Pl_ASCIIHexDecoder()
|
|
{
|
|
}
|
|
|
|
void
|
|
Pl_ASCIIHexDecoder::write(unsigned char* buf, size_t len)
|
|
{
|
|
if (this->eod)
|
|
{
|
|
return;
|
|
}
|
|
for (size_t i = 0; i < len; ++i)
|
|
{
|
|
char ch = static_cast<char>(toupper(buf[i]));
|
|
switch (ch)
|
|
{
|
|
case ' ':
|
|
case '\f':
|
|
case '\v':
|
|
case '\t':
|
|
case '\r':
|
|
case '\n':
|
|
QTC::TC("libtests", "Pl_ASCIIHexDecoder ignore space");
|
|
// ignore whitespace
|
|
break;
|
|
|
|
case '>':
|
|
this->eod = true;
|
|
flush();
|
|
break;
|
|
|
|
default:
|
|
if (((ch >= '0') && (ch <= '9')) ||
|
|
((ch >= 'A') && (ch <= 'F')))
|
|
{
|
|
this->inbuf[this->pos++] = ch;
|
|
if (this->pos == 2)
|
|
{
|
|
flush();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
char t[2];
|
|
t[0] = ch;
|
|
t[1] = 0;
|
|
throw std::runtime_error(
|
|
std::string("character out of range"
|
|
" during base Hex decode: ") + t);
|
|
}
|
|
break;
|
|
}
|
|
if (this->eod)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
Pl_ASCIIHexDecoder::flush()
|
|
{
|
|
if (this->pos == 0)
|
|
{
|
|
QTC::TC("libtests", "Pl_ASCIIHexDecoder no-op flush");
|
|
return;
|
|
}
|
|
int b[2];
|
|
for (int i = 0; i < 2; ++i)
|
|
{
|
|
if (this->inbuf[i] >= 'A')
|
|
{
|
|
b[i] = this->inbuf[i] - 'A' + 10;
|
|
}
|
|
else
|
|
{
|
|
b[i] = this->inbuf[i] - '0';
|
|
}
|
|
}
|
|
unsigned char ch = static_cast<unsigned char>((b[0] << 4) + b[1]);
|
|
|
|
QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush",
|
|
(this->pos == 2) ? 0 : 1);
|
|
getNext()->write(&ch, 1);
|
|
|
|
this->pos = 0;
|
|
this->inbuf[0] = '0';
|
|
this->inbuf[1] = '0';
|
|
this->inbuf[2] = '\0';
|
|
}
|
|
|
|
void
|
|
Pl_ASCIIHexDecoder::finish()
|
|
{
|
|
flush();
|
|
getNext()->finish();
|
|
}
|