fofi/FoFiType1C.cc    |   21 +++++++++++----------
 fofi/FoFiType1C.h     |    2 +-
 poppler/UnicodeMap.cc |   15 ++++++++-------
 poppler/UnicodeMap.h  |    4 ++--
 4 files changed, 22 insertions(+), 20 deletions(-)

New commits:
commit e702c6508e9b2e8082d99683ba933e9679608251
Author: Albert Astals Cid <[email protected]>
Date:   Sun Sep 29 17:41:44 2019 +0200

    Move the const_cast from assignment to free
    
    The problem is that some of the times the pointers hold values
    to const tables and some others hold values to dynamic memory.
    
    Instead of const_casting the const tables when needed and holding a non
    const pointer, we hold a const pointer and only const cast on free if
    needed

diff --git a/fofi/FoFiType1C.cc b/fofi/FoFiType1C.cc
index 8ecc066a..cdcef18a 100644
--- a/fofi/FoFiType1C.cc
+++ b/fofi/FoFiType1C.cc
@@ -107,7 +107,7 @@ FoFiType1C::~FoFiType1C() {
       charset != fofiType1CISOAdobeCharset &&
       charset != fofiType1CExpertCharset &&
       charset != fofiType1CExpertSubsetCharset) {
-    gfree(charset);
+    gfree(const_cast<unsigned short *>(charset));
   }
 }
 
@@ -2503,25 +2503,25 @@ bool FoFiType1C::readCharset() {
   int nLeft, i, j;
 
   if (topDict.charsetOffset == 0) {
-    charset = const_cast<unsigned short*>(fofiType1CISOAdobeCharset);
+    charset = fofiType1CISOAdobeCharset;
     charsetLength = sizeof(fofiType1CISOAdobeCharset) / sizeof(unsigned short);
   } else if (topDict.charsetOffset == 1) {
-    charset = const_cast<unsigned short*>(fofiType1CExpertCharset);
+    charset = fofiType1CExpertCharset;
     charsetLength = sizeof(fofiType1CExpertCharset) / sizeof(unsigned short);
   } else if (topDict.charsetOffset == 2) {
-    charset = const_cast<unsigned short*>(fofiType1CExpertSubsetCharset);
+    charset = fofiType1CExpertSubsetCharset;
     charsetLength = sizeof(fofiType1CExpertSubsetCharset) / sizeof(unsigned 
short);
   } else {
-    charset = (unsigned short *)gmallocn(nGlyphs, sizeof(unsigned short));
+    unsigned short *customCharset = (unsigned short *)gmallocn(nGlyphs, 
sizeof(unsigned short));
     charsetLength = nGlyphs;
     for (i = 0; i < nGlyphs; ++i) {
-      charset[i] = 0;
+      customCharset[i] = 0;
     }
     pos = topDict.charsetOffset;
     charsetFormat = getU8(pos++, &parsedOk);
     if (charsetFormat == 0) {
       for (i = 1; i < nGlyphs; ++i) {
-       charset[i] = (unsigned short)getU16BE(pos, &parsedOk);
+       customCharset[i] = (unsigned short)getU16BE(pos, &parsedOk);
        pos += 2;
        if (!parsedOk) {
          break;
@@ -2537,7 +2537,7 @@ bool FoFiType1C::readCharset() {
          break;
        }
        for (j = 0; j <= nLeft && i < nGlyphs; ++j) {
-         charset[i++] = (unsigned short)c++;
+         customCharset[i++] = (unsigned short)c++;
        }
       }
     } else if (charsetFormat == 2) {
@@ -2551,16 +2551,17 @@ bool FoFiType1C::readCharset() {
          break;
        }
        for (j = 0; j <= nLeft && i < nGlyphs; ++j) {
-         charset[i++] = (unsigned short)c++;
+         customCharset[i++] = (unsigned short)c++;
        }
       }
     }
     if (!parsedOk) {
-      gfree(charset);
+      gfree(customCharset);
       charset = nullptr;
       charsetLength = 0;
       return false;
     }
+    charset = customCharset;
   }
   return true;
 }
diff --git a/fofi/FoFiType1C.h b/fofi/FoFiType1C.h
index faae806d..ff69651a 100644
--- a/fofi/FoFiType1C.h
+++ b/fofi/FoFiType1C.h
@@ -246,7 +246,7 @@ private:
   int nGlyphs;
   int nFDs;
   unsigned char *fdSelect;
-  unsigned short *charset;
+  const unsigned short *charset;
   unsigned short charsetLength;
   int gsubrBias;
 
diff --git a/poppler/UnicodeMap.cc b/poppler/UnicodeMap.cc
index 6125294b..6e7e467b 100644
--- a/poppler/UnicodeMap.cc
+++ b/poppler/UnicodeMap.cc
@@ -14,7 +14,7 @@
 // under GPL version 2 or later
 //
 // Copyright (C) 2010 Jakub Wilk <[email protected]>
-// Copyright (C) 2017, 2018 Albert Astals Cid <[email protected]>
+// Copyright (C) 2017-2019 Albert Astals Cid <[email protected]>
 // Copyright (C) 2017 Adrian Johnson <[email protected]>
 // Copyright (C) 2017 Jean Ghali <[email protected]>
 // Copyright (C) 2018 Adam Reichold <[email protected]>
@@ -71,7 +71,7 @@ UnicodeMap *UnicodeMap::parse(GooString *encodingNameA) {
   map = new UnicodeMap(encodingNameA->copy());
 
   size = 8;
-  map->ranges = (UnicodeMapRange *)gmallocn(size, sizeof(UnicodeMapRange));
+  UnicodeMapRange *customRanges = (UnicodeMapRange *)gmallocn(size, 
sizeof(UnicodeMapRange));
   eMapsSize = 0;
 
   line = 1;
@@ -86,10 +86,10 @@ UnicodeMap *UnicodeMap::parse(GooString *encodingNameA) {
       if (nBytes <= 4) {
        if (map->len == size) {
          size *= 2;
-         map->ranges = (UnicodeMapRange *)
-           greallocn(map->ranges, size, sizeof(UnicodeMapRange));
+         customRanges = (UnicodeMapRange *)
+           greallocn(customRanges, size, sizeof(UnicodeMapRange));
        }
-       range = &map->ranges[map->len];
+       range = &customRanges[map->len];
        sscanf(tok1, "%x", &range->start);
        sscanf(tok2, "%x", &range->end);
        sscanf(tok3, "%x", &range->code);
@@ -125,6 +125,7 @@ UnicodeMap *UnicodeMap::parse(GooString *encodingNameA) {
 
   fclose(f);
 
+  map->ranges = customRanges;
   return map;
 }
 
@@ -144,7 +145,7 @@ UnicodeMap::UnicodeMap(const char *encodingNameA, bool 
unicodeOutA,
   encodingName = new GooString(encodingNameA);
   unicodeOut = unicodeOutA;
   kind = unicodeMapResident;
-  ranges = const_cast<UnicodeMapRange*>(rangesA);
+  ranges = rangesA;
   len = lenA;
   eMaps = nullptr;
   eMapsLen = 0;
@@ -165,7 +166,7 @@ UnicodeMap::UnicodeMap(const char *encodingNameA, bool 
unicodeOutA,
 UnicodeMap::~UnicodeMap() {
   delete encodingName;
   if (kind == unicodeMapUser && ranges) {
-    gfree(ranges);
+    gfree(const_cast<UnicodeMapRange *>(ranges));
   }
   if (eMaps) {
     gfree(eMaps);
diff --git a/poppler/UnicodeMap.h b/poppler/UnicodeMap.h
index 74f6ded0..17f1d59c 100644
--- a/poppler/UnicodeMap.h
+++ b/poppler/UnicodeMap.h
@@ -16,7 +16,7 @@
 // under GPL version 2 or later
 //
 // Copyright (C) 2017 Adrian Johnson <[email protected]>
-// Copyright (C) 2018 Albert Astals Cid <[email protected]>
+// Copyright (C) 2018, 2019 Albert Astals Cid <[email protected]>
 // Copyright (C) 2018 Adam Reichold <[email protected]>
 // Copyright (C) 2019 Volker Krause <[email protected]>
 //
@@ -104,7 +104,7 @@ private:
   UnicodeMapKind kind;
   bool unicodeOut;
   union {
-    UnicodeMapRange *ranges;   // (user, resident)
+    const UnicodeMapRange *ranges;     // (user, resident)
     UnicodeMapFunc func;       // (func)
   };
   int len;                     // (user, resident)
_______________________________________________
poppler mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/poppler

Reply via email to