aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/sph
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-06-21 05:35:23 +0200
committerJay Berkenbilt <ejb@ql.org>2019-06-21 19:17:21 +0200
commitd71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e (patch)
tree60f4a13cbadee1a02a49f35f325460f2ad507c95 /libqpdf/sph
parentf40ffc9d6392edf9b6fe74d288d6d578e6d1a240 (diff)
downloadqpdf-d71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e.tar.zst
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.
Diffstat (limited to 'libqpdf/sph')
-rw-r--r--libqpdf/sph/md_helper.c4
-rw-r--r--libqpdf/sph/sph_types.h8
2 files changed, 6 insertions, 6 deletions
diff --git a/libqpdf/sph/md_helper.c b/libqpdf/sph/md_helper.c
index 5384f03f..829391a7 100644
--- a/libqpdf/sph/md_helper.c
+++ b/libqpdf/sph/md_helper.c
@@ -145,7 +145,7 @@ SPH_XCAT(sph_, HASH)(void *cc, const void *data, size_t len)
clen = SPH_BLEN - current;
if (clen > len)
- clen = len;
+ clen = (unsigned)len;
memcpy(sc->buf + current, data, clen);
data = (const unsigned char *)data + clen;
current += clen;
@@ -257,7 +257,7 @@ SPH_XCAT(HASH, _addbits_and_close)(void *cc,
{
unsigned z;
- z = 0x80 >> n;
+ z = 0x80U >> n;
sc->buf[current ++] = ((ub & -z) | z) & 0xFF;
}
#endif
diff --git a/libqpdf/sph/sph_types.h b/libqpdf/sph/sph_types.h
index f434d8bb..e3b648c1 100644
--- a/libqpdf/sph/sph_types.h
+++ b/libqpdf/sph/sph_types.h
@@ -1337,8 +1337,8 @@ sph_bswap64(sph_u64 x)
static SPH_INLINE void
sph_enc16be(void *dst, unsigned val)
{
- ((unsigned char *)dst)[0] = (val >> 8);
- ((unsigned char *)dst)[1] = val;
+ ((unsigned char *)dst)[0] = (unsigned char)(val >> 8);
+ ((unsigned char *)dst)[1] = (unsigned char)val;
}
static SPH_INLINE unsigned
@@ -1351,8 +1351,8 @@ sph_dec16be(const void *src)
static SPH_INLINE void
sph_enc16le(void *dst, unsigned val)
{
- ((unsigned char *)dst)[0] = val;
- ((unsigned char *)dst)[1] = val >> 8;
+ ((unsigned char *)dst)[0] = (unsigned char)val;
+ ((unsigned char *)dst)[1] = (unsigned char)(val >> 8);
}
static SPH_INLINE unsigned