diff options
author | Sune Vuorela <sune@vuorela.dk> | 2025-01-08 15:06:37 +0000 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2025-01-08 15:06:37 +0000 |
commit | f79ec6a90e75d28c9cbcfebc93cd05e4b2016802 (patch) | |
tree | 832b9804a2b956dea70c770ebb8913b7ffdb8639 | |
parent | 8c0980ed360d55cd5b86ba7de8735517e5a0532a (diff) |
use unique_ptr for most owned GooStrings
36 files changed, 206 insertions, 284 deletions
diff --git a/cpp/poppler-document.cpp b/cpp/poppler-document.cpp index d2edeb75..fcfe1468 100644 --- a/cpp/poppler-document.cpp +++ b/cpp/poppler-document.cpp @@ -314,15 +314,13 @@ bool document::set_info_key(const std::string &key, const ustring &val) return false; } - GooString *goo_val; + std::unique_ptr<GooString> goo_val; - if (val.empty()) { - goo_val = nullptr; - } else { + if (!val.empty()) { goo_val = detail::ustring_to_unicode_GooString(val); } - d->doc->setDocInfoStringEntry(key.c_str(), goo_val); + d->doc->setDocInfoStringEntry(key.c_str(), std::move(goo_val)); return true; } @@ -381,16 +379,14 @@ bool document::set_info_date(const std::string &key, time_type val) return false; } - GooString *goo_date; + std::unique_ptr<GooString> goo_date; - if (val == time_type(-1)) { - goo_date = nullptr; - } else { + if (val != time_type(-1)) { time_t t = static_cast<time_t>(val); goo_date = timeToDateString(&t); } - d->doc->setDocInfoStringEntry(key.c_str(), goo_date); + d->doc->setDocInfoStringEntry(key.c_str(), std::move(goo_date)); return true; } @@ -407,15 +403,13 @@ bool document::set_info_date_t(const std::string &key, time_t val) return false; } - GooString *goo_date; + std::unique_ptr<GooString> goo_date; - if (val == time_t(-1)) { - goo_date = nullptr; - } else { + if (val != time_t(-1)) { goo_date = timeToDateString(&val); } - d->doc->setDocInfoStringEntry(key.c_str(), goo_date); + d->doc->setDocInfoStringEntry(key.c_str(), std::move(goo_date)); return true; } @@ -451,15 +445,13 @@ bool document::set_title(const ustring &title) return false; } - GooString *goo_title; + std::unique_ptr<GooString> goo_title; - if (title.empty()) { - goo_title = nullptr; - } else { + if (!title.empty()) { goo_title = detail::ustring_to_unicode_GooString(title); } - d->doc->setDocInfoTitle(goo_title); + d->doc->setDocInfoTitle(std::move(goo_title)); return true; } @@ -495,15 +487,13 @@ bool document::set_author(const ustring &author) return false; } - GooString *goo_author; + std::unique_ptr<GooString> goo_author; - if (author.empty()) { - goo_author = nullptr; - } else { + if (!author.empty()) { goo_author = detail::ustring_to_unicode_GooString(author); } - d->doc->setDocInfoAuthor(goo_author); + d->doc->setDocInfoAuthor(std::move(goo_author)); return true; } @@ -539,15 +529,13 @@ bool document::set_subject(const ustring &subject) return false; } - GooString *goo_subject; + std::unique_ptr<GooString> goo_subject; - if (subject.empty()) { - goo_subject = nullptr; - } else { + if (!subject.empty()) { goo_subject = detail::ustring_to_unicode_GooString(subject); } - d->doc->setDocInfoSubject(goo_subject); + d->doc->setDocInfoSubject(std::move(goo_subject)); return true; } @@ -583,15 +571,13 @@ bool document::set_keywords(const ustring &keywords) return false; } - GooString *goo_keywords; + std::unique_ptr<GooString> goo_keywords; - if (keywords.empty()) { - goo_keywords = nullptr; - } else { + if (!keywords.empty()) { goo_keywords = detail::ustring_to_unicode_GooString(keywords); } - d->doc->setDocInfoKeywords(goo_keywords); + d->doc->setDocInfoKeywords(std::move(goo_keywords)); return true; } @@ -627,15 +613,13 @@ bool document::set_creator(const ustring &creator) return false; } - GooString *goo_creator; + std::unique_ptr<GooString> goo_creator; - if (creator.empty()) { - goo_creator = nullptr; - } else { + if (!creator.empty()) { goo_creator = detail::ustring_to_unicode_GooString(creator); } - d->doc->setDocInfoCreator(goo_creator); + d->doc->setDocInfoCreator(std::move(goo_creator)); return true; } @@ -671,15 +655,13 @@ bool document::set_producer(const ustring &producer) return false; } - GooString *goo_producer; + std::unique_ptr<GooString> goo_producer; - if (producer.empty()) { - goo_producer = nullptr; - } else { + if (!producer.empty()) { goo_producer = detail::ustring_to_unicode_GooString(producer); } - d->doc->setDocInfoProducer(goo_producer); + d->doc->setDocInfoProducer(std::move(goo_producer)); return true; } @@ -735,16 +717,14 @@ bool document::set_creation_date(time_type creation_date) return false; } - GooString *goo_creation_date; + std::unique_ptr<GooString> goo_creation_date; - if (creation_date == time_type(-1)) { - goo_creation_date = nullptr; - } else { + if (creation_date != time_type(-1)) { time_t t = static_cast<time_t>(creation_date); goo_creation_date = timeToDateString(&t); } - d->doc->setDocInfoCreatDate(goo_creation_date); + d->doc->setDocInfoCreatDate(std::move(goo_creation_date)); return true; } @@ -760,15 +740,13 @@ bool document::set_creation_date_t(time_t creation_date) return false; } - GooString *goo_creation_date; + std::unique_ptr<GooString> goo_creation_date; - if (creation_date == time_t(-1)) { - goo_creation_date = nullptr; - } else { + if (creation_date != time_t(-1)) { goo_creation_date = timeToDateString(&creation_date); } - d->doc->setDocInfoCreatDate(goo_creation_date); + d->doc->setDocInfoCreatDate(std::move(goo_creation_date)); return true; } @@ -824,16 +802,14 @@ bool document::set_modification_date(time_type mod_date) return false; } - GooString *goo_mod_date; + std::unique_ptr<GooString> goo_mod_date; - if (mod_date == time_type(-1)) { - goo_mod_date = nullptr; - } else { + if (mod_date != time_type(-1)) { time_t t = static_cast<time_t>(mod_date); goo_mod_date = timeToDateString(&t); } - d->doc->setDocInfoModDate(goo_mod_date); + d->doc->setDocInfoModDate(std::move(goo_mod_date)); return true; } @@ -849,15 +825,13 @@ bool document::set_modification_date_t(time_t mod_date) return false; } - GooString *goo_mod_date; + std::unique_ptr<GooString> goo_mod_date; - if (mod_date == time_t(-1)) { - goo_mod_date = nullptr; - } else { + if (mod_date != time_t(-1)) { goo_mod_date = timeToDateString(&mod_date); } - d->doc->setDocInfoModDate(goo_mod_date); + d->doc->setDocInfoModDate(std::move(goo_mod_date)); return true; } diff --git a/cpp/poppler-private.cpp b/cpp/poppler-private.cpp index ed37f288..f86e58da 100644 --- a/cpp/poppler-private.cpp +++ b/cpp/poppler-private.cpp @@ -104,7 +104,7 @@ ustring detail::unicode_to_ustring(const Unicode *u, int length) return str; } -GooString *detail::ustring_to_unicode_GooString(const ustring &str) +std::unique_ptr<GooString> detail::ustring_to_unicode_GooString(const ustring &str) { const size_t len = str.size() * 2 + 2; const ustring::value_type *me = str.data(); @@ -115,6 +115,5 @@ GooString *detail::ustring_to_unicode_GooString(const ustring &str) ba[i * 2 + 2] = ((*me >> 8) & 0xff); ba[i * 2 + 3] = (*me & 0xff); } - GooString *goo = new GooString(&ba[0], len); - return goo; + return std::make_unique<GooString>(&ba[0], len); } diff --git a/cpp/poppler-private.h b/cpp/poppler-private.h index 7c8ff376..1d5a89da 100644 --- a/cpp/poppler-private.h +++ b/cpp/poppler-private.h @@ -52,7 +52,7 @@ rectf pdfrectangle_to_rectf(const PDFRectangle &pdfrect); ustring unicode_GooString_to_ustring(const GooString *str); ustring unicode_to_ustring(const Unicode *u, int length); -GooString *ustring_to_unicode_GooString(const ustring &str); +std::unique_ptr<GooString> ustring_to_unicode_GooString(const ustring &str); } diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index a27b2403..c3ae4bbf 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -1123,7 +1123,7 @@ char *_poppler_goo_string_to_utf8(const GooString *s) return result; } -static GooString *_poppler_goo_string_from_utf8(const gchar *src) +static std::unique_ptr<GooString> _poppler_goo_string_from_utf8(const gchar *src) { if (src == nullptr) { return nullptr; @@ -1136,7 +1136,7 @@ static GooString *_poppler_goo_string_from_utf8(const gchar *src) return nullptr; } - GooString *result = new GooString(utf16, outlen); + std::unique_ptr<GooString> result = std::make_unique<GooString>(utf16, outlen); g_free(utf16); if (!hasUnicodeByteOrderMark(result->toStr())) { @@ -1337,16 +1337,14 @@ void poppler_document_set_title(PopplerDocument *document, const gchar *title) { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *goo_title; - if (!title) { - goo_title = nullptr; - } else { + std::unique_ptr<GooString> goo_title; + if (title) { goo_title = _poppler_goo_string_from_utf8(title); if (!goo_title) { return; } } - document->doc->setDocInfoTitle(goo_title); + document->doc->setDocInfoTitle(std::move(goo_title)); } /** @@ -1382,16 +1380,14 @@ void poppler_document_set_author(PopplerDocument *document, const gchar *author) { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *goo_author; - if (!author) { - goo_author = nullptr; - } else { + std::unique_ptr<GooString> goo_author; + if (author) { goo_author = _poppler_goo_string_from_utf8(author); if (!goo_author) { return; } } - document->doc->setDocInfoAuthor(goo_author); + document->doc->setDocInfoAuthor(std::move(goo_author)); } /** @@ -1427,16 +1423,14 @@ void poppler_document_set_subject(PopplerDocument *document, const gchar *subjec { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *goo_subject; - if (!subject) { - goo_subject = nullptr; - } else { + std::unique_ptr<GooString> goo_subject; + if (subject) { goo_subject = _poppler_goo_string_from_utf8(subject); if (!goo_subject) { return; } } - document->doc->setDocInfoSubject(goo_subject); + document->doc->setDocInfoSubject(std::move(goo_subject)); } /** @@ -1472,16 +1466,14 @@ void poppler_document_set_keywords(PopplerDocument *document, const gchar *keywo { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *goo_keywords; - if (!keywords) { - goo_keywords = nullptr; - } else { + std::unique_ptr<GooString> goo_keywords; + if (keywords) { goo_keywords = _poppler_goo_string_from_utf8(keywords); if (!goo_keywords) { return; } } - document->doc->setDocInfoKeywords(goo_keywords); + document->doc->setDocInfoKeywords(std::move(goo_keywords)); } /** @@ -1519,16 +1511,14 @@ void poppler_document_set_creator(PopplerDocument *document, const gchar *creato { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *goo_creator; - if (!creator) { - goo_creator = nullptr; - } else { + std::unique_ptr<GooString> goo_creator; + if (creator) { goo_creator = _poppler_goo_string_from_utf8(creator); if (!goo_creator) { return; } } - document->doc->setDocInfoCreator(goo_creator); + document->doc->setDocInfoCreator(std::move(goo_creator)); } /** @@ -1566,16 +1556,14 @@ void poppler_document_set_producer(PopplerDocument *document, const gchar *produ { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *goo_producer; - if (!producer) { - goo_producer = nullptr; - } else { + std::unique_ptr<GooString> goo_producer; + if (producer) { goo_producer = _poppler_goo_string_from_utf8(producer); if (!goo_producer) { return; } } - document->doc->setDocInfoProducer(goo_producer); + document->doc->setDocInfoProducer(std::move(goo_producer)); } /** @@ -1617,8 +1605,8 @@ void poppler_document_set_creation_date(PopplerDocument *document, time_t creati { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *str = creation_date == (time_t)-1 ? nullptr : timeToDateString(&creation_date); - document->doc->setDocInfoCreatDate(str); + std::unique_ptr<GooString> str = creation_date == (time_t)-1 ? nullptr : timeToDateString(&creation_date); + document->doc->setDocInfoCreatDate(std::move(str)); } /** @@ -1658,13 +1646,13 @@ void poppler_document_set_creation_date_time(PopplerDocument *document, GDateTim { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *str = nullptr; + std::unique_ptr<GooString> str = nullptr; if (creation_datetime) { str = _poppler_convert_date_time_to_pdf_date(creation_datetime); } - document->doc->setDocInfoCreatDate(str); + document->doc->setDocInfoCreatDate(std::move(str)); } /** @@ -1706,8 +1694,8 @@ void poppler_document_set_modification_date(PopplerDocument *document, time_t mo { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *str = modification_date == (time_t)-1 ? nullptr : timeToDateString(&modification_date); - document->doc->setDocInfoModDate(str); + std::unique_ptr<GooString> str = modification_date == (time_t)-1 ? nullptr : timeToDateString(&modification_date); + document->doc->setDocInfoModDate(std::move(str)); } /** @@ -1747,13 +1735,13 @@ void poppler_document_set_modification_date_time(PopplerDocument *document, GDat { g_return_if_fail(POPPLER_IS_DOCUMENT(document)); - GooString *str = nullptr; + std::unique_ptr<GooString> str = nullptr; if (modification_datetime) { str = _poppler_convert_date_time_to_pdf_date(modification_datetime); } - document->doc->setDocInfoModDate(str); + document->doc->setDocInfoModDate(std::move(str)); } /** @@ -3827,7 +3815,7 @@ GDateTime *_poppler_convert_pdf_date_to_date_time(const GooString *date) * * Returns: The converted date **/ -GooString *_poppler_convert_date_time_to_pdf_date(GDateTime *datetime) +std::unique_ptr<GooString> _poppler_convert_date_time_to_pdf_date(GDateTime *datetime) { int offset_min; gchar *date_str; @@ -3845,7 +3833,7 @@ GooString *_poppler_convert_date_time_to_pdf_date(GDateTime *datetime) } g_free(date_str); - return new GooString(std::move(out_str)); + return std::make_unique<GooString>(std::move(out_str)); } static void _poppler_sign_document_thread(GTask *task, PopplerDocument *document, const PopplerSigningData *signing_data, GCancellable *cancellable) diff --git a/glib/poppler-form-field.cc b/glib/poppler-form-field.cc index 745ca058..89a5449f 100644 --- a/glib/poppler-form-field.cc +++ b/glib/poppler-form-field.cc @@ -820,17 +820,15 @@ gchar *poppler_form_field_text_get_text(PopplerFormField *field) **/ void poppler_form_field_text_set_text(PopplerFormField *field, const gchar *text) { - GooString *goo_tmp; gchar *tmp; gsize length = 0; g_return_if_fail(field->widget->getType() == formText); tmp = text ? g_convert(text, -1, "UTF-16BE", "UTF-8", nullptr, &length, nullptr) : nullptr; - goo_tmp = new GooString(tmp, length); + std::unique_ptr<GooString> goo_tmp = std::make_unique<GooString>(tmp, length); g_free(tmp); - static_cast<FormWidgetText *>(field->widget)->setContent(goo_tmp); - delete goo_tmp; + static_cast<FormWidgetText *>(field->widget)->setContent(std::move(goo_tmp)); } /** @@ -1076,17 +1074,15 @@ void poppler_form_field_choice_toggle_item(PopplerFormField *field, gint index) **/ void poppler_form_field_choice_set_text(PopplerFormField *field, const gchar *text) { - GooString *goo_tmp; gchar *tmp; gsize length = 0; g_return_if_fail(field->widget->getType() == formChoice); tmp = text ? g_convert(text, -1, "UTF-16BE", "UTF-8", nullptr, &length, nullptr) : nullptr; - goo_tmp = new GooString(tmp, length); + std::unique_ptr<GooString> goo_tmp = std::make_unique<GooString>(tmp, length); g_free(tmp); - static_cast<FormWidgetChoice *>(field->widget)->setEditChoice(goo_tmp); - delete goo_tmp; + static_cast<FormWidgetChoice *>(field->widget)->setEditChoice(std::move(goo_tmp)); } /** diff --git a/glib/poppler-private.h b/glib/poppler-private.h index 4d8e0703..41101de0 100644 --- a/glib/poppler-private.h +++ b/glib/poppler-private.h @@ -163,7 +163,7 @@ const PDFRectangle *_poppler_annot_get_cropbox(PopplerAnnot *poppler_annot); char *_poppler_goo_string_to_utf8(const GooString *s); gboolean _poppler_convert_pdf_date_to_gtime(const GooString *date, time_t *gdate); GDateTime *_poppler_convert_pdf_date_to_date_time(const GooString *date); -GooString *_poppler_convert_date_time_to_pdf_date(GDateTime *datetime); +std::unique_ptr<GooString> _poppler_convert_date_time_to_pdf_date(GDateTime *datetime); AnnotStampImageHelper *_poppler_convert_cairo_image_to_stamp_image_helper(const cairo_surface_t *image); PopplerColor *_poppler_convert_annot_color_to_poppler_color(const AnnotColor *color); std::unique_ptr<AnnotColor> _poppler_convert_poppler_color_to_annot_color(const PopplerColor *poppler_color); diff --git a/poppler/Annot.cc b/poppler/Annot.cc index 809d1748..e7aae41d 100644 --- a/poppler/Annot.cc +++ b/poppler/Annot.cc @@ -1484,7 +1484,7 @@ void Annot::update(const char *key, Object &&value) annotLocker(); /* Set M to current time, unless we are updating M itself */ if (strcmp(key, "M") != 0) { - modified.reset(timeToDateString(nullptr)); + modified = timeToDateString(nullptr); annotObj.dictSet("M", Object(modified->copy())); } @@ -1526,12 +1526,12 @@ void Annot::setName(GooString *new_name) update("NM", Object(name->copy())); } -void Annot::setModified(GooString *new_modified) +void Annot::setModified(std::unique_ptr<GooString> new_modified) { annotLocker(); if (new_modified) { - modified = std::make_unique<GooString>(new_modified); + modified = std::move(new_modified); update("M", Object(modified->copy())); } else { modified.reset(nullptr); @@ -2271,10 +2271,10 @@ void AnnotMarkup::setOpacity(double opacityA) invalidateAppearance(); } -void AnnotMarkup::setDate(GooString *new_date) +void AnnotMarkup::setDate(std::unique_ptr<GooString> new_date) { if (new_date) { - date = std::make_unique<GooString>(new_date); + date = std::move(new_date); update("CreationDate", Object(date->copy())); } else { date.reset(nullptr); @@ -2824,7 +2824,7 @@ AnnotFreeText::AnnotFreeText(PDFDoc *docA, PDFRectangle *rectA) : AnnotMarkup(do type = typeFreeText; annotObj.dictSet("Subtype", Object(objName, "FreeText")); - annotObj.dictSet("DA", Object(new GooString())); + annotObj.dictSet("DA", Object(std::make_unique<GooString>())); initialize(docA, annotObj.getDict()); } diff --git a/poppler/Annot.h b/poppler/Annot.h index c97b4242..21686fdb 100644 --- a/poppler/Annot.h +++ b/poppler/Annot.h @@ -742,7 +742,7 @@ public: // new_content should never be NULL virtual void setContents(std::unique_ptr<GooString> &&new_content); void setName(GooString *new_name); - void setModified(GooString *new_modified); + void setModified(std::unique_ptr<GooString> new_modified); void setFlags(unsigned int new_flags); void setBorder(std::unique_ptr<AnnotBorder> &&new_border); @@ -893,7 +893,7 @@ public: void setPopup(std::unique_ptr<AnnotPopup> &&new_popup); void setLabel(std::unique_ptr<GooString> &&new_label); void setOpacity(double opacityA); - void setDate(GooString *new_date); + void setDate(std::unique_ptr<GooString> new_date); protected: void removeReferencedObjects() override; diff --git a/poppler/Catalog.cc b/poppler/Catalog.cc index 5adf2da1..598d2b3b 100644 --- a/poppler/Catalog.cc +++ b/poppler/Catalog.cc @@ -542,7 +542,7 @@ void Catalog::addEmbeddedFile(GooFile *file, const std::string &fileName) const bool addFile = !fileAlreadyAdded && (sameFileName || fileName < efNameI->toStr()); if (addFile) { // If the new name is smaller lexicographically than an existing file add it in its correct position - embeddedFilesNamesArray->add(Object(new GooString(fileName))); + embeddedFilesNamesArray->add(Object(std::make_unique<GooString>(fileName))); embeddedFilesNamesArray->add(Object(fileSpecRef)); fileAlreadyAdded = true; } @@ -556,7 +556,7 @@ void Catalog::addEmbeddedFile(GooFile *file, const std::string &fileName) if (!fileAlreadyAdded) { // The new file is bigger lexicographically than the existing ones - embeddedFilesNamesArray->add(Object(new GooString(fileName))); + embeddedFilesNamesArray->add(Object(std::make_unique<GooString>(fileName))); embeddedFilesNamesArray->add(Object(fileSpecRef)); } diff --git a/poppler/DateInfo.cc b/poppler/DateInfo.cc index d3142bc8..d9a5f157 100644 --- a/poppler/DateInfo.cc +++ b/poppler/DateInfo.cc @@ -125,9 +125,9 @@ std::string timeToStringWithFormat(const time_t *timeA, const char *format) return buf; } -GooString *timeToDateString(const time_t *timeA) +std::unique_ptr<GooString> timeToDateString(const time_t *timeA) { - return new GooString(timeToStringWithFormat(timeA, "D:%Y%m%d%H%M%S%z")); + return std::make_unique<GooString>(timeToStringWithFormat(timeA, "D:%Y%m%d%H%M%S%z")); } // Convert PDF date string to time. Returns -1 if conversion fails. diff --git a/poppler/DateInfo.h b/poppler/DateInfo.h index 200536a0..b7cc324b 100644 --- a/poppler/DateInfo.h +++ b/poppler/DateInfo.h @@ -33,9 +33,9 @@ bool POPPLER_PRIVATE_EXPORT parseDateString(const GooString *date, int *year, in /* Converts the time_t into a PDF Date format string. * If timeA is NULL, current time is used. - * Returns new GooString. Free with delete. + * Returns goostring. */ -GooString POPPLER_PRIVATE_EXPORT *timeToDateString(const time_t *timeA); +std::unique_ptr<GooString> POPPLER_PRIVATE_EXPORT timeToDateString(const time_t *timeA); /* Converts the time_t into a string with the specified format. * If timeA is NULL, current time is used. diff --git a/poppler/FileSpec.cc b/poppler/FileSpec.cc index 62782603..55d73bb8 100644 --- a/poppler/FileSpec.cc +++ b/poppler/FileSpec.cc @@ -190,7 +190,7 @@ Object FileSpec::newFileSpecObject(XRef *xref, GooFile *file, const std::string Dict *fsDict = new Dict(xref); fsDict->set("Type", Object(objName, "Filespec")); - fsDict->set("UF", Object(new GooString(fileName))); + fsDict->set("UF", Object(std::make_unique<GooString>(fileName))); fsDict->set("EF", Object(efDict)); return Object(fsDict); diff --git a/poppler/Form.cc b/poppler/Form.cc index 1a5ac7fc..131c678f 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -431,14 +431,14 @@ void FormWidgetText::setTextFontSize(int fontSize) parent()->setTextFontSize(fontSize); } -void FormWidgetText::setContent(const GooString *new_content) +void FormWidgetText::setContent(std::unique_ptr<GooString> new_content) { - parent()->setContentCopy(new_content); + parent()->setContent(std::move(new_content)); } -void FormWidgetText::setAppearanceContent(const GooString *new_content) +void FormWidgetText::setAppearanceContent(std::unique_ptr<GooString> new_content) { - parent()->setAppearanceContentCopy(new_content); + parent()->setAppearanceContent(std::move(new_content)); } FormFieldText *FormWidgetText::parent() const @@ -507,19 +507,19 @@ bool FormWidgetChoice::isSelected(int i) const return parent()->isSelected(i); } -void FormWidgetChoice::setEditChoice(const GooString *new_content) +void FormWidgetChoice::setEditChoice(std::unique_ptr<GooString> new_content) { if (!hasEdit()) { error(errInternal, -1, "FormFieldChoice::setEditChoice : trying to edit an non-editable choice"); return; } - parent()->setEditChoice(new_content); + parent()->setEditChoice(std::move(new_content)); } -void FormWidgetChoice::setAppearanceChoiceContent(const GooString *new_content) +void FormWidgetChoice::setAppearanceChoiceContent(std::unique_ptr<GooString> new_content) { - parent()->setAppearanceChoiceContentCopy(new_content); + parent()->setAppearanceChoiceContent(std::move(new_content)); } int FormWidgetChoice::getNumChoices() const @@ -939,8 +939,7 @@ bool FormWidgetSignature::createSignature(Object &vObj, Ref vRef, const GooStrin vObj.dictAdd("Filter", Object(objName, "Adobe.PPKLite")); vObj.dictAdd("SubFilter", Object(objName, toStdString(signatureType).c_str())); vObj.dictAdd("Name", Object(name.copy())); - GooString *date = timeToDateString(nullptr); - vObj.dictAdd("M", Object(date)); + vObj.dictAdd("M", Object(timeToDateString(nullptr))); if (reason && (reason->getLength() > 0)) { vObj.dictAdd("Reason", Object(reason->copy())); } @@ -1678,12 +1677,12 @@ void FormFieldText::print(int indent) printf("%*s- (%d %d): [text] terminal: %s children: %zu\n", indent, "", ref.num, ref.gen, terminal ? "Yes" : "No", terminal ? widgets.size() : children.size()); } -void FormFieldText::setContentCopy(const GooString *new_content) +void FormFieldText::setContent(std::unique_ptr<GooString> new_content) { content.reset(); if (new_content) { - content = new_content->copy(); + content = std::move(new_content); // append the unicode marker <FE FF> if needed if (!hasUnicodeByteOrderMark(content->toStr())) { @@ -1720,12 +1719,12 @@ void FormFieldText::setContentCopy(const GooString *new_content) updateChildrenAppearance(); } -void FormFieldText::setAppearanceContentCopy(const GooString *new_content) +void FormFieldText::setAppearanceContent(std::unique_ptr<GooString> new_content) { internalContent.reset(); if (new_content) { - internalContent = new_content->copy(); + internalContent = std::move(new_content); } updateChildrenAppearance(); } @@ -1735,7 +1734,7 @@ FormFieldText::~FormFieldText() = default; void FormFieldText::reset(const std::vector<std::string> &excludedFields) { if (!isAmongExcludedFields(excludedFields)) { - setContentCopy(defaultContent.get()); + setContent(defaultContent ? defaultContent->copy() : nullptr); if (defaultContent == nullptr) { obj.getDict()->remove("V"); } @@ -2029,7 +2028,7 @@ void FormFieldChoice::updateSelection() if (numSelected == 0) { // No options are selected - objV = Object(new GooString("")); + objV = Object(std::make_unique<GooString>("")); } else if (numSelected == 1) { // Only one option is selected for (int i = 0; i < numChoices; i++) { @@ -2107,14 +2106,14 @@ void FormFieldChoice::select(int i) updateSelection(); } -void FormFieldChoice::setEditChoice(const GooString *new_content) +void FormFieldChoice::setEditChoice(std::unique_ptr<GooString> new_content) { editedChoice.reset(); unselectAll(); if (new_content) { - editedChoice = new_content->copy(); + editedChoice = std::move(new_content); // append the unicode marker <FE FF> if needed if (!hasUnicodeByteOrderMark(editedChoice->toStr())) { @@ -2124,12 +2123,12 @@ void FormFieldChoice::setEditChoice(const GooString *new_content) updateSelection(); } -void FormFieldChoice::setAppearanceChoiceContentCopy(const GooString *new_content) +void FormFieldChoice::setAppearanceChoiceContent(std::unique_ptr<GooString> new_content) { appearanceSelectedChoice.reset(); if (new_content) { - appearanceSelectedChoice = new_content->copy(); + appearanceSelectedChoice = std::move(new_content); // append the unicode marker <FE FF> if needed if (!hasUnicodeByteOrderMark(appearanceSelectedChoice->toStr())) { @@ -2818,8 +2817,8 @@ Form::AddFontResult Form::addFontToDefaultResources(const std::string &filepath, { // We only support fonts with identity cmaps for now Dict *cidSystemInfo = new Dict(xref); - cidSystemInfo->set("Registry", Object(new GooString("Adobe"))); - cidSystemInfo->set("Ordering", Object(new GooString("Identity"))); + cidSystemInfo->set("Registry", Object(std::make_unique<GooString>("Adobe"))); + cidSystemInfo->set("Ordering", Object(std::make_unique<GooString>("Identity"))); cidSystemInfo->set("Supplement", Object(0)); descendantFont->set("CIDSystemInfo", Object(cidSystemInfo)); } diff --git a/poppler/Form.h b/poppler/Form.h index 7a1e49cc..7928c2ff 100644 --- a/poppler/Form.h +++ b/poppler/Form.h @@ -210,9 +210,9 @@ public: const GooString *getContent() const; // expects a UTF16BE string - void setContent(const GooString *new_content); + void setContent(std::unique_ptr<GooString> new_content); // sets the text inside the field appearance stream - void setAppearanceContent(const GooString *new_content); + void setAppearanceContent(std::unique_ptr<GooString> new_content); void updateWidgetAppearance() override; @@ -249,7 +249,7 @@ public: const GooString *getExportVal(int i) const; // select the i-th choice void select(int i); - void setAppearanceChoiceContent(const GooString *new_content); + void setAppearanceChoiceContent(std::unique_ptr<GooString> new_content); // toggle selection of the i-th choice void toggle(int i); @@ -258,7 +258,7 @@ public: // except a UTF16BE string // only work for editable combo box, set the user-entered text as the current choice - void setEditChoice(const GooString *new_content); + void setEditChoice(std::unique_ptr<GooString> new_content); const GooString *getEditChoice() const; @@ -490,8 +490,8 @@ public: const GooString *getContent() const { return content.get(); } const GooString *getAppearanceContent() const { return internalContent ? internalContent.get() : content.get(); } - void setContentCopy(const GooString *new_content); - void setAppearanceContentCopy(const GooString *new_content); + void setContent(std::unique_ptr<GooString> new_content); + void setAppearanceContent(std::unique_ptr<GooString> new_content); ~FormFieldText() override; bool isMultiline() const { return multiline; } @@ -549,7 +549,7 @@ public: const GooString *getSelectedChoice() const; const GooString *getAppearanceSelectedChoice() const { return appearanceSelectedChoice ? appearanceSelectedChoice.get() : getSelectedChoice(); } - void setAppearanceChoiceContentCopy(const GooString *new_content); + void setAppearanceChoiceContent(std::unique_ptr<GooString> new_content); // select the i-th choice void select(int i); @@ -561,7 +561,7 @@ public: void deselectAll(); // only work for editable combo box, set the user-entered text as the current choice - void setEditChoice(const GooString *new_content); + void setEditChoice(std::unique_ptr<GooString> new_content); const GooString *getEditChoice() const; diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc index bfe7c9ac..f994f4e9 100644 --- a/poppler/GfxFont.cc +++ b/poppler/GfxFont.cc @@ -1742,8 +1742,8 @@ GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, std::optional<std: if (!obj2.isString() || !obj3.isString()) { error(errSyntaxError, -1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font"); error(errSyntaxError, -1, "Assuming Adobe-Identity for character collection"); - obj2 = Object(new GooString("Adobe")); - obj3 = Object(new GooString("Identity")); + obj2 = Object(std::make_unique<GooString>("Adobe")); + obj3 = Object(std::make_unique<GooString>("Identity")); } collection = obj2.getString()->copy(); collection->append('-'); diff --git a/poppler/Link.cc b/poppler/Link.cc index 08d5523e..f1730b7a 100644 --- a/poppler/Link.cc +++ b/poppler/Link.cc @@ -754,7 +754,7 @@ Object LinkJavaScript::createObject(XRef *xref, const std::string &js) { Dict *linkDict = new Dict(xref); linkDict->add("S", Object(objName, "JavaScript")); - linkDict->add("JS", Object(new GooString(js))); + linkDict->add("JS", Object(std::make_unique<GooString>(js))); return Object(linkDict); } diff --git a/poppler/Object.h b/poppler/Object.h index 8eb0c26b..de6aca36 100644 --- a/poppler/Object.h +++ b/poppler/Object.h @@ -226,12 +226,6 @@ public: type = objReal; real = realA; } - explicit Object(GooString *stringA) - { - assert(stringA); - type = objString; - string = stringA; - } explicit Object(std::unique_ptr<GooString> stringA) { assert(stringA); diff --git a/poppler/Outline.cc b/poppler/Outline.cc index 9e760662..32f152ce 100644 --- a/poppler/Outline.cc +++ b/poppler/Outline.cc @@ -91,8 +91,7 @@ static void insertChildHelper(const std::string &itemTitle, int destPageNum, uns Object outlineItem = Object(new Dict(xref)); - GooString *g = new GooString(itemTitle); - outlineItem.dictSet("Title", Object(g)); + outlineItem.dictSet("Title", Object(std::make_unique<GooString>(itemTitle))); outlineItem.dictSet("Dest", Object(a)); outlineItem.dictSet("Count", Object(1)); outlineItem.dictAdd("Parent", Object(parentObjRef)); @@ -302,8 +301,7 @@ int Outline::addOutlineTreeNodeList(const std::vector<OutlineTreeNode> &nodeList } lastRef = outlineItemRef; - GooString *g = new GooString(node.title); - outlineItem.dictSet("Title", Object(g)); + outlineItem.dictSet("Title", Object(std::make_unique<GooString>(node.title))); outlineItem.dictSet("Dest", Object(a)); itemCount++; @@ -490,9 +488,9 @@ void OutlineItem::open() void OutlineItem::setTitle(const std::string &titleA) { Object dict = xref->fetch(ref); - GooString *g = new GooString(titleA); + auto g = std::make_unique<GooString>(titleA); title = TextStringToUCS4(g->toStr()); - dict.dictSet("Title", Object(g)); + dict.dictSet("Title", Object(std::move(g))); xref->setModifiedObject(&dict, ref); } diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index e6f52df7..b567250b 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -719,12 +719,9 @@ bool PDFDoc::isLinearized(bool tryingToReconstruct) } } -void PDFDoc::setDocInfoStringEntry(const char *key, GooString *value) +void PDFDoc::setDocInfoStringEntry(const char *key, std::unique_ptr<GooString> value) { bool removeEntry = !value || value->getLength() == 0 || (value->toStr() == unicodeByteOrderMark); - if (removeEntry) { - delete value; - } Object infoObj = getDocInfo(); if (infoObj.isNull() && removeEntry) { @@ -737,7 +734,7 @@ void PDFDoc::setDocInfoStringEntry(const char *key, GooString *value) if (removeEntry) { infoObj.dictSet(key, Object(objNull)); } else { - infoObj.dictSet(key, Object(value)); + infoObj.dictSet(key, Object(std::move(value))); } if (infoObj.dictGetLength() == 0) { @@ -1597,14 +1594,14 @@ Object PDFDoc::createTrailerDict(int uxrefSize, bool incrUpdate, Goffset startxR Array *array = new Array(xRef); // Get the first part of the ID array->add(obj4.arrayGet(0)); - array->add(Object(new GooString((const char *)digest, 16))); + array->add(Object(std::make_unique<GooString>((const char *)digest, 16))); trailerDict->set("ID", Object(array)); } } else { // new file => same values for the two identifiers Array *array = new Array(xRef); - array->add(Object(new GooString((const char *)digest, 16))); - array->add(Object(new GooString((const char *)digest, 16))); + array->add(Object(std::make_unique<GooString>((const char *)digest, 16))); + array->add(Object(std::make_unique<GooString>((const char *)digest, 16))); trailerDict->set("ID", Object(array)); } @@ -2222,7 +2219,7 @@ std::optional<PDFDoc::SignatureData> PDFDoc::createSignature(::Page *destPage, s annotObj.dictSet("Rect", Object(rectArray)); const std::string daStr = da.toAppearanceString(); - annotObj.dictSet("DA", Object(new GooString(daStr))); + annotObj.dictSet("DA", Object(std::make_unique<GooString>(daStr))); const Ref ref = getXRef()->addIndirectObject(annotObj); catalog->addFormToAcroForm(ref); diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 6356d936..36e5f4d6 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -257,19 +257,18 @@ public: // Set doc info string entry. nullptr or empty value will cause a removal. // Takes ownership of value. - void setDocInfoStringEntry(const char *key, GooString *value); + void setDocInfoStringEntry(const char *key, std::unique_ptr<GooString> value); // Set document's properties in document's Info dictionary. // nullptr or empty value will cause a removal. - // Takes ownership of value. - void setDocInfoTitle(GooString *title) { setDocInfoStringEntry("Title", title); } - void setDocInfoAuthor(GooString *author) { setDocInfoStringEntry("Author", author); } - void setDocInfoSubject(GooString *subject) { setDocInfoStringEntry("Subject", subject); } - void setDocInfoKeywords(GooString *keywords) { setDocInfoStringEntry("Keywords", keywords); } - void setDocInfoCreator(GooString *creator) { setDocInfoStringEntry("Creator", creator); } - void setDocInfoProducer(GooString *producer) { setDocInfoStringEntry("Producer", producer); } - void setDocInfoCreatDate(GooString *creatDate) { setDocInfoStringEntry("CreationDate", creatDate); } - void setDocInfoModDate(GooString *modDate) { setDocInfoStringEntry("ModDate", modDate); } + void setDocInfoTitle(std::unique_ptr<GooString> title) { setDocInfoStringEntry("Title", std::move(title)); } + void setDocInfoAuthor(std::unique_ptr<GooString> author) { setDocInfoStringEntry("Author", std::move(author)); } + void setDocInfoSubject(std::unique_ptr<GooString> subject) { setDocInfoStringEntry("Subject", std::move(subject)); } + void setDocInfoKeywords(std::unique_ptr<GooString> keywords) { setDocInfoStringEntry("Keywords", std::move(keywords)); } + void setDocInfoCreator(std::unique_ptr<GooString> creator) { setDocInfoStringEntry("Creator", std::move(creator)); } + void setDocInfoProducer(std::unique_ptr<GooString> producer) { setDocInfoStringEntry("Producer", std::move(producer)); } + void setDocInfoCreatDate(std::unique_ptr<GooString> creatDate) { setDocInfoStringEntry("CreationDate", std::move(creatDate)); } + void setDocInfoModDate(std::unique_ptr<GooString> modDate) { setDocInfoStringEntry("ModDate", std::move(modDate)); } // Get document's properties from document's Info dictionary. // Returns nullptr on fail. diff --git a/poppler/Parser.cc b/poppler/Parser.cc index ce0f1fd2..02f9c1b7 100644 --- a/poppler/Parser.cc +++ b/poppler/Parser.cc @@ -163,7 +163,7 @@ Object Parser::getObj(bool simpleOnly, const unsigned char *fileKey, CryptAlgori const Object &contentsObj = dict->lookupNF("Contents"); if (contentsObj.isString()) { std::unique_ptr<GooString> s = decryptedString(contentsObj.getString(), fileKey, encAlgorithm, keyLength, objNum, objGen); - dict->set("Contents", Object(s.release())); + dict->set("Contents", Object(std::move(s))); } } } diff --git a/qt5/src/poppler-annotation.cc b/qt5/src/poppler-annotation.cc index 27e36aca..9b0cc3eb 100644 --- a/qt5/src/poppler-annotation.cc +++ b/qt5/src/poppler-annotation.cc @@ -1516,9 +1516,8 @@ void Annotation::setModificationDate(const QDateTime &date) if (d->pdfAnnot) { if (date.isValid()) { const time_t t = date.toSecsSinceEpoch(); - GooString *s = timeToDateString(&t); - d->pdfAnnot->setModified(s); - delete s; + std::unique_ptr<GooString> s = timeToDateString(&t); + d->pdfAnnot->setModified(std::move(s)); } else { d->pdfAnnot->setModified(nullptr); } @@ -1555,9 +1554,8 @@ void Annotation::setCreationDate(const QDateTime &date) if (markupann) { if (date.isValid()) { const time_t t = date.toSecsSinceEpoch(); - GooString *s = timeToDateString(&t); - markupann->setDate(s); - delete s; + std::unique_ptr<GooString> s = timeToDateString(&t); + markupann->setDate(std::move(s)); } else { markupann->setDate(nullptr); } diff --git a/qt5/src/poppler-document.cc b/qt5/src/poppler-document.cc index e2b335e2..c6aafbc6 100644 --- a/qt5/src/poppler-document.cc +++ b/qt5/src/poppler-document.cc @@ -276,8 +276,8 @@ bool Document::setInfo(const QString &key, const QString &val) return false; } - GooString *goo = QStringToUnicodeGooString(val); - m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), goo); + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(val); + m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), std::move(goo)); return true; } diff --git a/qt5/src/poppler-form.cc b/qt5/src/poppler-form.cc index 323fa7cf..8f147e81 100644 --- a/qt5/src/poppler-form.cc +++ b/qt5/src/poppler-form.cc @@ -420,17 +420,15 @@ QString FormFieldText::text() const void FormFieldText::setText(const QString &text) { FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); - GooString *goo = QStringToUnicodeGooString(text); - fwt->setContent(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwt->setContent(std::move(goo)); } void FormFieldText::setAppearanceText(const QString &text) { FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); - GooString *goo = QStringToUnicodeGooString(text); - fwt->setAppearanceContent(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwt->setAppearanceContent(std::move(goo)); } bool FormFieldText::isPassword() const @@ -570,9 +568,8 @@ void FormFieldChoice::setEditChoice(const QString &text) FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); if (fwc->isCombo() && fwc->hasEdit()) { - GooString *goo = QStringToUnicodeGooString(text); - fwc->setEditChoice(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwc->setEditChoice(std::move(goo)); } } @@ -590,9 +587,8 @@ bool FormFieldChoice::canBeSpellChecked() const void FormFieldChoice::setAppearanceChoiceText(const QString &text) { FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); - GooString *goo = QStringToUnicodeGooString(text); - fwc->setAppearanceChoiceContent(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwc->setAppearanceChoiceContent(std::move(goo)); } class CertificateInfoPrivate diff --git a/qt5/src/poppler-private.cc b/qt5/src/poppler-private.cc index bc67d023..55d5b6ef 100644 --- a/qt5/src/poppler-private.cc +++ b/qt5/src/poppler-private.cc @@ -133,22 +133,21 @@ QString UnicodeParsedString(const std::string &s1) } } -GooString *QStringToUnicodeGooString(const QString &s) +std::unique_ptr<GooString> QStringToUnicodeGooString(const QString &s) { if (s.isEmpty()) { - return new GooString(); + return std::make_unique<GooString>(); } int len = s.length() * 2 + 2; - char *cstring = (char *)gmallocn(len, sizeof(char)); - cstring[0] = (char)0xfe; - cstring[1] = (char)0xff; - for (int i = 0; i < s.length(); ++i) { - cstring[2 + i * 2] = s.at(i).row(); - cstring[3 + i * 2] = s.at(i).cell(); + std::string string; + string.reserve(len); + string.push_back((char)0xfe); + string.push_back((char)0xff); + for (auto element : s) { + string.push_back(element.row()); + string.push_back(element.cell()); } - GooString *ret = new GooString(cstring, len); - gfree(cstring); - return ret; + return std::make_unique<GooString>(std::move(string)); } std::unique_ptr<GooString> QStringToGooString(const QString &s) @@ -163,7 +162,7 @@ std::unique_ptr<GooString> QStringToGooString(const QString &s) return ret; } -GooString *QDateTimeToUnicodeGooString(const QDateTime &dt) +std::unique_ptr<GooString> QDateTimeToUnicodeGooString(const QDateTime &dt) { if (!dt.isValid()) { return nullptr; diff --git a/qt5/src/poppler-private.h b/qt5/src/poppler-private.h index 53c8b48f..30f55589 100644 --- a/qt5/src/poppler-private.h +++ b/qt5/src/poppler-private.h @@ -82,11 +82,11 @@ POPPLER_QT5_EXPORT QString UnicodeParsedString(const std::string &s1); // Returns a big endian UTF-16 string with BOM or an empty string without BOM. // The caller owns the returned pointer. -POPPLER_QT5_EXPORT GooString *QStringToUnicodeGooString(const QString &s); +POPPLER_QT5_EXPORT std::unique_ptr<GooString> QStringToUnicodeGooString(const QString &s); POPPLER_QT5_EXPORT std::unique_ptr<GooString> QStringToGooString(const QString &s); -GooString *QDateTimeToUnicodeGooString(const QDateTime &dt); +std::unique_ptr<GooString> QDateTimeToUnicodeGooString(const QDateTime &dt); void qt5ErrorFunction(ErrorCategory /*category*/, Goffset pos, const char *msg); diff --git a/qt5/tests/check_strings.cpp b/qt5/tests/check_strings.cpp index f41e2058..2f919f47 100644 --- a/qt5/tests/check_strings.cpp +++ b/qt5/tests/check_strings.cpp @@ -190,7 +190,7 @@ void TestStrings::check_QStringToUnicodeGooString() QFETCH(QString, string); QFETCH(QByteArray, result); - GooString *goo = Poppler::QStringToUnicodeGooString(string); + std::unique_ptr<GooString> goo = Poppler::QStringToUnicodeGooString(string); if (string.isEmpty()) { QVERIFY(goo->toStr().empty()); QCOMPARE(goo->getLength(), 0); @@ -199,8 +199,6 @@ void TestStrings::check_QStringToUnicodeGooString() QCOMPARE(goo->getLength(), string.length() * 2 + 2); QCOMPARE(result, QByteArray::fromRawData(goo->c_str() + 2, goo->getLength() - 2)); } - - delete goo; } void TestStrings::check_QStringToGooString_data() diff --git a/qt5/tests/check_utf_conversion.cpp b/qt5/tests/check_utf_conversion.cpp index 6a4280b8..88a32359 100644 --- a/qt5/tests/check_utf_conversion.cpp +++ b/qt5/tests/check_utf_conversion.cpp @@ -131,12 +131,10 @@ void TestUTFConversion::testUnicodeToAscii7() // Test string is one 'Registered' and twenty 'Copyright' chars // so it's long enough to reproduce the bug given that glibc // malloc() always returns 8-byte aligned memory addresses. - GooString *goo = Poppler::QStringToUnicodeGooString(QString::fromUtf8("®©©©©©©©©©©©©©©©©©©©©")); // clazy:exclude=qstring-allocations + std::unique_ptr<GooString> goo = Poppler::QStringToUnicodeGooString(QString::fromUtf8("®©©©©©©©©©©©©©©©©©©©©")); // clazy:exclude=qstring-allocations const std::vector<Unicode> in = TextStringToUCS4(goo->toStr()); - delete goo; - int in_norm_len; int *in_norm_idx; Unicode *in_norm = unicodeNormalizeNFKC(in.data(), in.size(), &in_norm_len, &in_norm_idx, true); diff --git a/qt6/src/poppler-annotation.cc b/qt6/src/poppler-annotation.cc index 1845139f..4551005a 100644 --- a/qt6/src/poppler-annotation.cc +++ b/qt6/src/poppler-annotation.cc @@ -1158,9 +1158,8 @@ void Annotation::setModificationDate(const QDateTime &date) if (d->pdfAnnot) { if (date.isValid()) { const time_t t = date.toSecsSinceEpoch(); - GooString *s = timeToDateString(&t); - d->pdfAnnot->setModified(s); - delete s; + std::unique_ptr<GooString> s = timeToDateString(&t); + d->pdfAnnot->setModified(std::move(s)); } else { d->pdfAnnot->setModified(nullptr); } @@ -1197,9 +1196,8 @@ void Annotation::setCreationDate(const QDateTime &date) if (markupann) { if (date.isValid()) { const time_t t = date.toSecsSinceEpoch(); - GooString *s = timeToDateString(&t); - markupann->setDate(s); - delete s; + std::unique_ptr<GooString> s = timeToDateString(&t); + markupann->setDate(std::move(s)); } else { markupann->setDate(nullptr); } diff --git a/qt6/src/poppler-document.cc b/qt6/src/poppler-document.cc index 08367705..498d66d6 100644 --- a/qt6/src/poppler-document.cc +++ b/qt6/src/poppler-document.cc @@ -276,8 +276,8 @@ bool Document::setInfo(const QString &key, const QString &val) return false; } - GooString *goo = QStringToUnicodeGooString(val); - m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), goo); + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(val); + m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), std::move(goo)); return true; } diff --git a/qt6/src/poppler-form.cc b/qt6/src/poppler-form.cc index 514ab710..c4777368 100644 --- a/qt6/src/poppler-form.cc +++ b/qt6/src/poppler-form.cc @@ -420,17 +420,15 @@ QString FormFieldText::text() const void FormFieldText::setText(const QString &text) { FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); - GooString *goo = QStringToUnicodeGooString(text); - fwt->setContent(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwt->setContent(std::move(goo)); } void FormFieldText::setAppearanceText(const QString &text) { FormWidgetText *fwt = static_cast<FormWidgetText *>(m_formData->fm); - GooString *goo = QStringToUnicodeGooString(text); - fwt->setAppearanceContent(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwt->setAppearanceContent(std::move(goo)); } bool FormFieldText::isPassword() const @@ -570,9 +568,8 @@ void FormFieldChoice::setEditChoice(const QString &text) FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); if (fwc->isCombo() && fwc->hasEdit()) { - GooString *goo = QStringToUnicodeGooString(text); - fwc->setEditChoice(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwc->setEditChoice(std::move(goo)); } } @@ -590,9 +587,8 @@ bool FormFieldChoice::canBeSpellChecked() const void FormFieldChoice::setAppearanceChoiceText(const QString &text) { FormWidgetChoice *fwc = static_cast<FormWidgetChoice *>(m_formData->fm); - GooString *goo = QStringToUnicodeGooString(text); - fwc->setAppearanceChoiceContent(goo); - delete goo; + std::unique_ptr<GooString> goo = QStringToUnicodeGooString(text); + fwc->setAppearanceChoiceContent(std::move(goo)); } class CertificateInfoPrivate diff --git a/qt6/src/poppler-private.cc b/qt6/src/poppler-private.cc index 21603ac1..7bb294cf 100644 --- a/qt6/src/poppler-private.cc +++ b/qt6/src/poppler-private.cc @@ -133,22 +133,21 @@ QString UnicodeParsedString(const std::string &s1) } } -GooString *QStringToUnicodeGooString(const QString &s) +std::unique_ptr<GooString> QStringToUnicodeGooString(const QString &s) { if (s.isEmpty()) { - return new GooString(); + return std::make_unique<GooString>(); } int len = s.length() * 2 + 2; - char *cstring = (char *)gmallocn(len, sizeof(char)); - cstring[0] = (char)0xfe; - cstring[1] = (char)0xff; - for (int i = 0; i < s.length(); ++i) { - cstring[2 + i * 2] = s.at(i).row(); - cstring[3 + i * 2] = s.at(i).cell(); + std::string string; + string.reserve(len); + string.push_back((char)0xfe); + string.push_back((char)0xff); + for (auto element : s) { + string.push_back(element.row()); + string.push_back(element.cell()); } - GooString *ret = new GooString(cstring, len); - gfree(cstring); - return ret; + return std::make_unique<GooString>(string); } std::unique_ptr<GooString> QStringToGooString(const QString &s) @@ -163,7 +162,7 @@ std::unique_ptr<GooString> QStringToGooString(const QString &s) return ret; } -GooString *QDateTimeToUnicodeGooString(const QDateTime &dt) +std::unique_ptr<GooString> QDateTimeToUnicodeGooString(const QDateTime &dt) { if (!dt.isValid()) { return nullptr; diff --git a/qt6/src/poppler-private.h b/qt6/src/poppler-private.h index b8d04f2b..ca18cbb7 100644 --- a/qt6/src/poppler-private.h +++ b/qt6/src/poppler-private.h @@ -79,13 +79,13 @@ POPPLER_QT6_EXPORT QString UnicodeParsedString(const GooString *s1); POPPLER_QT6_EXPORT QString UnicodeParsedString(const std::string &s1); -POPPLER_QT6_EXPORT GooString *QStringToUnicodeGooString(const QString &s); +POPPLER_QT6_EXPORT std::unique_ptr<GooString> QStringToUnicodeGooString(const QString &s); // Returns a big endian UTF-16 string with BOM or an empty string without BOM. // The caller owns the returned pointer. POPPLER_QT6_EXPORT std::unique_ptr<GooString> QStringToGooString(const QString &s); -GooString *QDateTimeToUnicodeGooString(const QDateTime &dt); +std::unique_ptr<GooString> QDateTimeToUnicodeGooString(const QDateTime &dt); void qt6ErrorFunction(ErrorCategory /*category*/, Goffset pos, const char *msg); diff --git a/qt6/tests/check_strings.cpp b/qt6/tests/check_strings.cpp index 294cc449..262dd68b 100644 --- a/qt6/tests/check_strings.cpp +++ b/qt6/tests/check_strings.cpp @@ -189,7 +189,7 @@ void TestStrings::check_QStringToUnicodeGooString() QFETCH(QString, string); QFETCH(QByteArray, result); - GooString *goo = Poppler::QStringToUnicodeGooString(string); + std::unique_ptr<GooString> goo = Poppler::QStringToUnicodeGooString(string); if (string.isEmpty()) { QVERIFY(goo->toStr().empty()); QCOMPARE(goo->getLength(), 0); @@ -198,8 +198,6 @@ void TestStrings::check_QStringToUnicodeGooString() QCOMPARE(goo->getLength(), string.length() * 2 + 2); QCOMPARE(result, QByteArray::fromRawData(goo->c_str() + 2, goo->getLength() - 2)); } - - delete goo; } void TestStrings::check_QStringToGooString_data() diff --git a/qt6/tests/check_utf_conversion.cpp b/qt6/tests/check_utf_conversion.cpp index 7a7e5b5c..62ce65eb 100644 --- a/qt6/tests/check_utf_conversion.cpp +++ b/qt6/tests/check_utf_conversion.cpp @@ -129,12 +129,10 @@ void TestUTFConversion::testUnicodeToAscii7() // Test string is one 'Registered' and twenty 'Copyright' chars // so it's long enough to reproduce the bug given that glibc // malloc() always returns 8-byte aligned memory addresses. - GooString *goo = Poppler::QStringToUnicodeGooString(QString::fromUtf8("®©©©©©©©©©©©©©©©©©©©©")); // clazy:exclude=qstring-allocations + std::unique_ptr<GooString> goo = Poppler::QStringToUnicodeGooString(QString::fromUtf8("®©©©©©©©©©©©©©©©©©©©©")); // clazy:exclude=qstring-allocations const std::vector<Unicode> in = TextStringToUCS4(goo->toStr()); - delete goo; - int in_norm_len; int *in_norm_idx; Unicode *in_norm = unicodeNormalizeNFKC(in.data(), in.size(), &in_norm_len, &in_norm_idx, true); diff --git a/utils/pdfunite.cc b/utils/pdfunite.cc index 144e36b4..8ac97838 100644 --- a/utils/pdfunite.cc +++ b/utils/pdfunite.cc @@ -48,7 +48,7 @@ static void doMergeNameTree(PDFDoc *doc, XRef *srcXRef, XRef *countRef, int oldR const Object &mvalue = mergeNameArray.arrayGetNF(j + 1); if (mkey.isString() && mvalue.isRef()) { if (mkey.getString()->cmp(key.getString()) < 0) { - newNameArray->add(Object(new GooString(mkey.getString()->c_str()))); + newNameArray->add(Object(mkey.getString()->copy())); newNameArray->add(Object(Ref { mvalue.getRef().num + numOffset, mvalue.getRef().gen })); j += 2; } else if (mkey.getString()->cmp(key.getString()) == 0) { @@ -60,7 +60,7 @@ static void doMergeNameTree(PDFDoc *doc, XRef *srcXRef, XRef *countRef, int oldR j += 2; } } - newNameArray->add(Object(new GooString(key.getString()->c_str()))); + newNameArray->add(Object(key.getString()->copy())); newNameArray->add(Object(value.getRef())); } } @@ -68,7 +68,7 @@ static void doMergeNameTree(PDFDoc *doc, XRef *srcXRef, XRef *countRef, int oldR const Object &mkey = mergeNameArray.arrayGetNF(j); const Object &mvalue = mergeNameArray.arrayGetNF(j + 1); if (mkey.isString() && mvalue.isRef()) { - newNameArray->add(Object(new GooString(mkey.getString()->c_str()))); + newNameArray->add(Object(mkey.getString()->copy())); newNameArray->add(Object(Ref { mvalue.getRef().num + numOffset, mvalue.getRef().gen })); } j += 2; @@ -81,7 +81,7 @@ static void doMergeNameTree(PDFDoc *doc, XRef *srcXRef, XRef *countRef, int oldR const Object &key = mergeNameArray.arrayGetNF(i); const Object &value = mergeNameArray.arrayGetNF(i + 1); if (key.isString() && value.isRef()) { - newNameArray->add(Object(new GooString(key.getString()->c_str()))); + newNameArray->add(Object(key.getString()->copy())); newNameArray->add(Object(Ref { value.getRef().num + numOffset, value.getRef().gen })); } } |