aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFFormFieldObjectHelper.cc
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/QPDFFormFieldObjectHelper.cc
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/QPDFFormFieldObjectHelper.cc')
-rw-r--r--libqpdf/QPDFFormFieldObjectHelper.cc34
1 files changed, 20 insertions, 14 deletions
diff --git a/libqpdf/QPDFFormFieldObjectHelper.cc b/libqpdf/QPDFFormFieldObjectHelper.cc
index 08042df2..b3c61ce5 100644
--- a/libqpdf/QPDFFormFieldObjectHelper.cc
+++ b/libqpdf/QPDFFormFieldObjectHelper.cc
@@ -4,6 +4,7 @@
#include <qpdf/QPDFAnnotationObjectHelper.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/Pl_QPDFTokenizer.hh>
+#include <qpdf/QIntC.hh>
#include <stdlib.h>
QPDFFormFieldObjectHelper::Members::~Members()
@@ -189,7 +190,7 @@ QPDFFormFieldObjectHelper::getQuadding()
if (fv.isInteger())
{
QTC::TC("qpdf", "QPDFFormFieldObjectHelper Q present");
- result = static_cast<int>(fv.getIntValue());
+ result = QIntC::to_int(fv.getIntValue());
}
return result;
}
@@ -198,7 +199,7 @@ int
QPDFFormFieldObjectHelper::getFlags()
{
QPDFObjectHandle f = getInheritableFieldValue("/Ff");
- return f.isInteger() ? f.getIntValue() : 0;
+ return f.isInteger() ? f.getIntValueAsInt() : 0;
}
bool
@@ -245,8 +246,8 @@ QPDFFormFieldObjectHelper::getChoices()
QPDFObjectHandle opt = getInheritableFieldValue("/Opt");
if (opt.isArray())
{
- size_t n = opt.getArrayNItems();
- for (size_t i = 0; i < n; ++i)
+ int n = opt.getArrayNItems();
+ for (int i = 0; i < n; ++i)
{
QPDFObjectHandle item = opt.getArrayItem(i);
if (item.isString())
@@ -631,8 +632,8 @@ ValueSetter::writeAppearance()
{
// Try to make the found item the second one, but
// adjust for under/overflow.
- int wanted_first = found_idx - 1;
- int wanted_last = found_idx + max_rows - 2;
+ int wanted_first = QIntC::to_int(found_idx) - 1;
+ int wanted_last = QIntC::to_int(found_idx + max_rows) - 2;
QTC::TC("qpdf", "QPDFFormFieldObjectHelper list found");
while (wanted_first < 0)
{
@@ -640,7 +641,7 @@ ValueSetter::writeAppearance()
++wanted_first;
++wanted_last;
}
- while (wanted_last >= static_cast<int>(nopt))
+ while (wanted_last >= QIntC::to_int(nopt))
{
QTC::TC("qpdf", "QPDFFormFieldObjectHelper list last too high");
if (wanted_first > 0)
@@ -650,8 +651,9 @@ ValueSetter::writeAppearance()
--wanted_last;
}
highlight = true;
- highlight_idx = found_idx - wanted_first;
- for (int i = wanted_first; i <= wanted_last; ++i)
+ highlight_idx = found_idx - QIntC::to_size(wanted_first);
+ for (size_t i = QIntC::to_size(wanted_first);
+ i <= QIntC::to_size(wanted_last); ++i)
{
lines.push_back(opt.at(i));
}
@@ -672,13 +674,15 @@ ValueSetter::writeAppearance()
// Write the lines centered vertically, highlighting if needed
size_t nlines = lines.size();
- double dy = bbox.ury - ((bbox.ury - bbox.lly - (nlines * tfh)) / 2.0);
+ double dy = bbox.ury - ((bbox.ury - bbox.lly -
+ (static_cast<double>(nlines) * tfh)) / 2.0);
if (highlight)
{
write("q\n0.85 0.85 0.85 rg\n" +
QUtil::double_to_string(bbox.llx) + " " +
- QUtil::double_to_string(bbox.lly + dy -
- (tfh * (highlight_idx + 1))) + " " +
+ QUtil::double_to_string(
+ bbox.lly + dy -
+ (tfh * (static_cast<double>(highlight_idx + 1)))) + " " +
QUtil::double_to_string(bbox.urx - bbox.llx) + " " +
QUtil::double_to_string(tfh) +
" re f\nQ\n");
@@ -693,8 +697,10 @@ ValueSetter::writeAppearance()
// which doesn't seem really worth the effort.
if (i == 0)
{
- write(QUtil::double_to_string(bbox.llx + dx) + " " +
- QUtil::double_to_string(bbox.lly + dy) + " Td\n");
+ write(QUtil::double_to_string(bbox.llx + static_cast<double>(dx)) +
+ " " +
+ QUtil::double_to_string(bbox.lly + static_cast<double>(dy)) +
+ " Td\n");
}
else
{