Opened 7 years ago

Closed 7 years ago

#6752 closed defect (fixed)

Don't call strlen on a C++ string / CPLString

Reported by: Kurt Schwehr Owned by: Kurt Schwehr
Priority: normal Milestone:
Component: default Version: unspecified
Severity: normal Keywords:
Cc:

Description

The C++ string class has .size() and .empty(). Prefer those to calling strlen(osString) or strlen(osString.c_str()). This causes the creation of a C string const buffer when .size() or .empty() might already know the string size. O(n) -> O(1) where n is the string length.

find . -name \*.cpp | xargs grep strlen | egrep 'os[A-Z]' | wc -l
     169  # Lots of false positives

e.g.

find . -name \*.cpp | xargs grep strlen | egrep 'os[A-Z]' | head -20
# Removed a few false matches
./frmts/adrg/adrgdataset.cpp:    if( strlen(osBaseFileName) != 8 ||
./frmts/adrg/new/adrgdataset.cpp:    if (strlen(osBaseFileName) != 8 || osBaseFileName[6] != '0' || osBaseFileName[7] != '1')
./frmts/adrg/new/srpdataset.cpp:        if( strlen(osQALFileName) > 0 )
./frmts/adrg/srpdataset.cpp:        if( strlen(osQALFileName) > 0 )
./frmts/aigrid/aigdataset.cpp:    if( strlen(osClrFilename) == 0 )
./frmts/aigrid/aigdataset.cpp:    if( strlen(osClrFilename) > 0 )
./frmts/aigrid/aigdataset.cpp:        if( !EQUALN(papszFileList[i],osOldPath,strlen(osOldPath)) )
./frmts/aigrid/aigdataset.cpp:        osNewFilename = osNewPath + (papszFileList[i] + strlen(osOldPath));
./frmts/dimap/dimapdataset.cpp:    if( strlen(osProjection) > 0 )
./frmts/ers/ersdataset.cpp:        if( strlen(osId) == 0 )
./frmts/ers/ersdataset.cpp:    if( strlen(osRawFilename) > 0 )
./frmts/gtiff/gt_citation.cpp:    if( osCitation[strlen(osCitation) - 1] != '|' )
./frmts/hfa/hfaopen.cpp:                    strlen(hHFA->poDictionary->osDictionaryText.c_str()) + 1,
./frmts/hfa/hfaopen.cpp:            if( strncmp(aosNL[i], pszOldBase, strlen(pszOldBase)) == 0 )
./frmts/ilwis/ilwisdataset.cpp:        VSIFWriteL( osLine.c_str(), 1, strlen(osLine), filIni );

Change History (1)

comment:1 by Even Rouault, 7 years ago

Resolution: fixed
Status: newclosed

In 36979:

Remove uses of strlen(a_std_string) (fixes #6752)

Note: See TracTickets for help on using tickets.