Commit 8cb36405 authored by Adam Reichold's avatar Adam Reichold

Adjust all users of Dict::add to avoid leaking memory due to by now...

Adjust all users of Dict::add to avoid leaking memory due to by now unnecessary calls to copyString.
parent d479e37d
......@@ -2810,14 +2810,14 @@ static GfxFont * createAnnotDrawFont(XRef * xref, Dict *fontResDict)
const Ref dummyRef = { -1, -1 };
Dict *fontDict = new Dict(xref);
fontDict->add(copyString("BaseFont"), Object(objName, "Helvetica"));
fontDict->add(copyString("Subtype"), Object(objName, "Type0"));
fontDict->add(copyString("Encoding"), Object(objName, "WinAnsiEncoding"));
fontDict->add("BaseFont", Object(objName, "Helvetica"));
fontDict->add("Subtype", Object(objName, "Type0"));
fontDict->add("Encoding", Object(objName, "WinAnsiEncoding"));
Dict *fontsDict = new Dict(xref);
fontsDict->add(copyString("AnnotDrawFont"), Object(fontDict));
fontsDict->add("AnnotDrawFont", Object(fontDict));
fontResDict->add(copyString("Font"), Object(fontsDict));
fontResDict->add("Font", Object(fontsDict));
return GfxFont::makeFont(xref, "AnnotDrawFont", dummyRef, fontDict);
}
......@@ -4883,19 +4883,19 @@ void AnnotWidget::generateFieldAppearance(bool *addedDingbatsResource) {
const GooString *appearBuf = appearBuilder.buffer();
// build the appearance stream dictionary
Dict *appearDict = new Dict(xref);
appearDict->add(copyString("Length"), Object(appearBuf->getLength()));
appearDict->add(copyString("Subtype"), Object(objName, "Form"));
appearDict->add("Length", Object(appearBuf->getLength()));
appearDict->add("Subtype", Object(objName, "Form"));
Array *bbox = new Array(xref);
bbox->add(Object(0));
bbox->add(Object(0));
bbox->add(Object(rect->x2 - rect->x1));
bbox->add(Object(rect->y2 - rect->y1));
appearDict->add(copyString("BBox"), Object(bbox));
appearDict->add("BBox", Object(bbox));
// set the resource dictionary
Object *resDict = form->getDefaultResourcesObj();
if (resDict->isDict()) {
appearDict->add(copyString("Resources"), resDict->copy());
appearDict->add("Resources", resDict->copy());
}
// build the appearance stream
......@@ -4934,7 +4934,7 @@ void AnnotWidget::updateAppearanceStream()
// Write the AP dictionary
obj1 = Object(new Dict(xref));
obj1.dictAdd(copyString("N"), Object(updatedAppearanceStream.num, updatedAppearanceStream.gen));
obj1.dictAdd("N", Object(updatedAppearanceStream.num, updatedAppearanceStream.gen));
// Update our internal pointers to the appearance dictionary
appearStreams = new AnnotAppearance(doc, &obj1);
......@@ -4967,14 +4967,14 @@ void AnnotWidget::draw(Gfx *gfx, GBool printing) {
// We are forcing ZaDb but the font does not exist
// so create a fake one
Dict *fontDict = new Dict(gfx->getXRef());
fontDict->add(copyString("BaseFont"), Object(objName, "ZapfDingbats"));
fontDict->add(copyString("Subtype"), Object(objName, "Type1"));
fontDict->add("BaseFont", Object(objName, "ZapfDingbats"));
fontDict->add("Subtype", Object(objName, "Type1"));
Dict *fontsDict = new Dict(gfx->getXRef());
fontsDict->add(copyString("ZaDb"), Object(fontDict));
fontsDict->add("ZaDb", Object(fontDict));
Dict *dict = new Dict(gfx->getXRef());
dict->add(copyString("Font"), Object(fontsDict));
dict->add("Font", Object(fontsDict));
gfx->pushResources(dict);
delete dict;
}
......
......@@ -4912,7 +4912,6 @@ void Gfx::opBeginImage(Object args[], int numArgs) {
}
Stream *Gfx::buildImageStream() {
char *key;
Stream *str;
// build dictionary
......@@ -4922,13 +4921,11 @@ Stream *Gfx::buildImageStream() {
if (!obj.isName()) {
error(errSyntaxError, getPos(), "Inline image dictionary key must be a name object");
} else {
key = copyString(obj.getName());
obj = parser->getObj();
if (obj.isEOF() || obj.isError()) {
gfree(key);
auto val = parser->getObj();
if (val.isEOF() || val.isError()) {
break;
}
dict.dictAdd(key, std::move(obj));
dict.dictAdd(obj.getName(), std::move(val));
}
obj = parser->getObj();
}
......
......@@ -263,7 +263,7 @@ public:
// Dict accessors.
int dictGetLength() const;
void dictAdd(char *key, Object &&val);
void dictAdd(const char *key, Object &&val);
void dictSet(const char *key, Object &&val);
void dictRemove(const char *key);
GBool dictIs(const char *dictType) const;
......@@ -353,7 +353,7 @@ inline Object Object::arrayGetNF(int i) const
inline int Object::dictGetLength() const
{ OBJECT_TYPE_CHECK(objDict); return dict->getLength(); }
inline void Object::dictAdd(char *key, Object &&val)
inline void Object::dictAdd(const char *key, Object &&val)
{ OBJECT_TYPE_CHECK(objDict); dict->add(key, std::move(val)); }
inline void Object::dictSet(const char *key, Object &&val)
......
......@@ -1645,7 +1645,7 @@ void PDFDoc::replacePageDict(int pageNo, int rotate,
mediaBoxArray->add(Object(mediaBox->y2));
Object mediaBoxObject(mediaBoxArray);
Object trimBoxObject = mediaBoxObject.copy();
pageDict->add(copyString("MediaBox"), std::move(mediaBoxObject));
pageDict->add("MediaBox", std::move(mediaBoxObject));
if (cropBox != nullptr) {
Array *cropBoxArray = new Array(getXRef());
cropBoxArray->add(Object(cropBox->x1));
......@@ -1654,10 +1654,10 @@ void PDFDoc::replacePageDict(int pageNo, int rotate,
cropBoxArray->add(Object(cropBox->y2));
Object cropBoxObject(cropBoxArray);
trimBoxObject = cropBoxObject.copy();
pageDict->add(copyString("CropBox"), std::move(cropBoxObject));
pageDict->add("CropBox", std::move(cropBoxObject));
}
pageDict->add(copyString("TrimBox"), std::move(trimBoxObject));
pageDict->add(copyString("Rotate"), Object(rotate));
pageDict->add("TrimBox", std::move(trimBoxObject));
pageDict->add("Rotate", Object(rotate));
getXRef()->setModifiedObject(&page, *refPage);
}
......
......@@ -112,20 +112,18 @@ Object Parser::getObj(GBool simpleOnly,
if (strict) goto err;
shift();
} else {
// buf1 might go away in shift(), so construct the key
char *key = copyString(buf1.getName());
// buf1 will go away in shift(), so keep the key
const auto key = std::move(buf1);
shift();
if (buf1.isEOF() || buf1.isError()) {
gfree(key);
if (strict && buf1.isError()) goto err;
break;
}
Object obj2 = getObj(gFalse, fileKey, encAlgorithm, keyLength, objNum, objGen, recursion + 1);
if (unlikely(obj2.isError() && recursion + 1 >= recursionLimit)) {
gfree(key);
break;
}
obj.dictAdd(key, std::move(obj2));
obj.dictAdd(key.getName(), std::move(obj2));
}
}
if (buf1.isEOF()) {
......
......@@ -91,7 +91,7 @@ static void doMergeNameTree(PDFDoc *doc, XRef *srcXRef, XRef *countRef, int oldR
newNameArray->add(Object(value.getRef().num + numOffset, value.getRef().gen));
}
}
srcNameTree->add(copyString("Names"), Object(newNameArray));
srcNameTree->add("Names", Object(newNameArray));
doc->markPageObjects(mergeNameTree, srcXRef, countRef, numOffset, oldRefNum, newRefNum);
}
}
......@@ -106,7 +106,7 @@ static void doMergeNameDict(PDFDoc *doc, XRef *srcXRef, XRef *countRef, int oldR
} else if (srcNameTree.isNull() && mergeNameTree.isDict()) {
Object newNameTree(new Dict(srcXRef));
doMergeNameTree(doc, srcXRef, countRef, oldRefNum, newRefNum, newNameTree.getDict(), mergeNameTree.getDict(), numOffset);
srcNameDict->add(copyString(key), std::move(newNameTree));
srcNameDict->add(key, std::move(newNameTree));
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment