From 8f2445a205cb97f084358c82dc8da999e2c350ed Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Thu, 16 Feb 2023 12:54:37 -0700 Subject: [PATCH] Per #2402, update the implementation of CgFontCollection to use std::vector instead of dynamically allocating memory to avoid SonarQube findings. --- src/tools/other/mode_graphics/cgraph_font.cc | 119 ++++++------------- src/tools/other/mode_graphics/cgraph_font.h | 10 +- 2 files changed, 39 insertions(+), 90 deletions(-) diff --git a/src/tools/other/mode_graphics/cgraph_font.cc b/src/tools/other/mode_graphics/cgraph_font.cc index 9f12dca012..7892c943c5 100644 --- a/src/tools/other/mode_graphics/cgraph_font.cc +++ b/src/tools/other/mode_graphics/cgraph_font.cc @@ -115,7 +115,7 @@ face = 0; if ( !get_env(cg_font_env, gs_font_dir) ) { mlog << Error - << "\n\n CgFont::init_from_scratch() -> " + << "\nCgFont::init_from_scratch() -> " << "unable to get environment variable \"" << cg_font_env << "\"\n\n"; @@ -258,7 +258,8 @@ clear(); if ( (n < 0) || (n >= total_predef_fonts) ) { - mlog << Error << "\n\n CgFont::set_by_number(int) -> range check error\n\n"; + mlog << Error << "\nCgFont::set_by_number(int) -> " + << "range check error\n\n"; exit ( 1 ); @@ -274,7 +275,8 @@ full_afm_name << gs_font_dir << '/' << gs_font_dir << '/' << short_afm_name; if ( !file_exists(full_afm_name.c_str()) ) { - mlog << Error << "\n\n CgFont::set_by_number(int) -> can't find afm file \"" << full_afm_name << "\"\n\n"; + mlog << Error << "\nCgFont::set_by_number(int) -> " + << "can't find afm file \"" << full_afm_name << "\"\n\n"; exit ( 1 ); @@ -284,7 +286,8 @@ afm = new Afm; if ( !(afm->read(full_afm_name)) ) { - mlog << Error << "\n\n CgFont::set_by_number(int) -> trouble reading afm file \"" << full_afm_name << "\"\n\n"; + mlog << Error << "\nCgFont::set_by_number(int) -> " + << "trouble reading afm file \"" << full_afm_name << "\"\n\n"; exit ( 1 ); @@ -374,8 +377,6 @@ void CgFontCollection::init_from_scratch() { -e = (CgFont **) 0; - clear(); return; @@ -390,34 +391,28 @@ void CgFontCollection::clear() { -if ( e ) { - - int j, error; - - for (j=0; jface); +int j, error; - if ( error ) { +for (j=0; j trouble closing typeface \"" << e[j]->short_pfb_name << "\"\n\n"; + error = FT_Done_Face(e[j].face); - exit ( 1 ); + if ( error ) { - } + mlog << Error << "\nCgFontCollection::clear() -> " + << "trouble closing typeface \"" << e[j].short_pfb_name + << "\"\n\n"; - e[j]->face = 0; - - if ( e[j] ) { delete e[j]; e[j] = (CgFont *) 0; } + exit ( 1 ); } - delete [] e; e = (CgFont **) 0; + e[j].face = 0; } +e.clear(); + Nelements = Nalloc = 0; return; @@ -445,7 +440,7 @@ for (j=0; jdump(out, depth + 1); + e[j].dump(out, depth + 1); } @@ -470,37 +465,13 @@ void CgFontCollection::extend(int n) if ( n <= Nalloc ) return; -int j, k; -CgFont ** u = (CgFont **) 0; - - -k = n/alloc_inc; +int k = n/alloc_inc; if ( n%alloc_inc ) ++k; n = k*alloc_inc; -u = new CgFont * [n]; - -if ( !u ) { - - mlog << Error << "\n\n CgFontCollection::extend(int) -> memory allocation error\n\n"; - - exit ( 1 ); - -} - -for (j=0; jps_font_number == n ) return ( e[j] ); + if ( e[j].ps_font_number == n ) return ( & e[j] ); } @@ -641,7 +594,7 @@ return ( (CgFont *) 0 ); //////////////////////////////////////////////////////////////////////// -CgFont * CgFontCollection::lookup_by_ps_name(const char * name) const +CgFont * CgFontCollection::lookup_by_ps_name(const char * name) { @@ -649,7 +602,7 @@ int j; for (j=0; jps_name == name ) return ( e[j] ); + if ( e[j].ps_name == name ) return ( & e[j] ); } @@ -662,19 +615,20 @@ return ( (CgFont *) 0 ); //////////////////////////////////////////////////////////////////////// -CgFont * CgFontCollection::operator[](int k) const +CgFont * CgFontCollection::operator[](int k) { if ( (k < 0) || (k >= Nelements) ) { - mlog << Error << "\n\n CgFont * CgFontCollection::operator[](int) const -> range check error\n\n"; + mlog << Error << "\nCgFont * CgFontCollection::operator[](int) const -> " + << "range check error\n\n"; exit ( 1 ); } -return ( e[k] ); +return ( & e[k] ); } @@ -702,8 +656,3 @@ return ( false ); //////////////////////////////////////////////////////////////////////// - - - - - diff --git a/src/tools/other/mode_graphics/cgraph_font.h b/src/tools/other/mode_graphics/cgraph_font.h index f8e4f8b493..287a2c289f 100644 --- a/src/tools/other/mode_graphics/cgraph_font.h +++ b/src/tools/other/mode_graphics/cgraph_font.h @@ -18,6 +18,7 @@ #include +#include #include "ft2build.h" #include FT_FREETYPE_H @@ -98,7 +99,7 @@ class CgFontCollection { int Nalloc; - CgFont ** e; + std::vector e; public: @@ -117,12 +118,11 @@ class CgFontCollection { void add (const CgFont &); void add_no_repeat (const CgFont &); - CgFont * lookup_by_ps_name(const char *) const; + CgFont * lookup_by_ps_name(const char *); - CgFont * lookup_by_ps_font_number(int) const; // for builtin fonts + CgFont * lookup_by_ps_font_number(int); // for builtin fonts - - CgFont * operator[](int) const; + CgFont * operator[](int); };