2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-23 11:28:56 +00:00
qpdf/libqpdf/InsecureRandomDataProvider.cc
Jay Berkenbilt d71f05ca07 Fix sign and conversion warnings (major)
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.
2019-06-21 13:17:21 -04:00

51 lines
1.1 KiB
C++

#include <qpdf/InsecureRandomDataProvider.hh>
#include <qpdf/qpdf-config.h>
#include <qpdf/QUtil.hh>
#include <stdlib.h>
InsecureRandomDataProvider::InsecureRandomDataProvider() :
seeded_random(false)
{
}
InsecureRandomDataProvider::~InsecureRandomDataProvider()
{
}
void
InsecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
{
for (size_t i = 0; i < len; ++i)
{
data[i] = static_cast<unsigned char>((this->random() & 0xff0) >> 4);
}
}
long
InsecureRandomDataProvider::random()
{
if (! this->seeded_random)
{
// Seed the random number generator with something simple, but
// just to be interesting, don't use the unmodified current
// time. It would be better if this were a more secure seed.
QUtil::srandom(static_cast<unsigned int>(
QUtil::get_current_time() ^ 0xcccc));
this->seeded_random = true;
}
# ifdef HAVE_RANDOM
return ::random();
# else
return rand();
# endif
}
RandomDataProvider*
InsecureRandomDataProvider::getInstance()
{
static InsecureRandomDataProvider instance;
return &instance;
}