Skip to content

Commit

Permalink
Fix calls to tolower() and toupper() to make sure argument is unsigne…
Browse files Browse the repository at this point in the history
…d, otherwise unspecified behavior
  • Loading branch information
rouault committed Jan 18, 2024
1 parent 09d8938 commit c80de4b
Show file tree
Hide file tree
Showing 38 changed files with 115 additions and 78 deletions.
2 changes: 1 addition & 1 deletion frmts/aigrid/aigopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ VSILFILE *AIGLLOpen(const char *pszFilename, const char *pszAccess)
for (i = (int)strlen(pszUCFilename) - 1;
pszUCFilename[i] != '/' && pszUCFilename[i] != '\\'; i--)
{
pszUCFilename[i] = (char)toupper(pszUCFilename[i]);
pszUCFilename[i] = (char)toupper((unsigned char)(pszUCFilename[i]));
}

fp = VSIFOpenL(pszUCFilename, pszAccess);
Expand Down
5 changes: 3 additions & 2 deletions frmts/georaster/oci_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,8 @@ void OWUpperIfNoQuotes(char *pszText)

for (size_t i = 0; i < nSize; i++)
{
pszText[i] = static_cast<char>(toupper(pszText[i]));
pszText[i] =
static_cast<char>(toupper(static_cast<unsigned char>(pszText[i])));
}
}

Expand Down Expand Up @@ -1488,7 +1489,7 @@ CPLString OWParseSDO_GEOR_INIT(const char *pszInsert, int nField)

for (pszIn = szUpcase; *pszIn != '\0'; pszIn++)
{
*pszIn = (char)toupper(*pszIn);
*pszIn = (char)toupper(static_cast<unsigned char>(*pszIn));
}

char *pszStart = strstr(szUpcase, "SDO_GEOR.INIT");
Expand Down
2 changes: 1 addition & 1 deletion frmts/grib/degrib/degrib/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ static int Clock_GetWord (char **Start, char **End, char word[30],
f_integer = 1;
while ((*ptr != ' ') && (*ptr != ',') && (*ptr != '\0')) {
if (cnt < 29) {
word[cnt] = (char) toupper (*ptr);
word[cnt] = (char) toupper ((unsigned char)*ptr);
cnt++;
}
if (*ptr == ':') {
Expand Down
14 changes: 7 additions & 7 deletions frmts/grib/degrib/degrib/myutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ void strToUpper (char *str)
return;
}

while ((*ptr++ = toupper (*str++)) != '\0') {
while ((*ptr++ = toupper ((unsigned char)(*str++))) != '\0') {
}
}
#endif
Expand Down Expand Up @@ -908,7 +908,7 @@ void strToLower (char *str)
return;
}

while ((*ptr++ = tolower (*str++)) != '\0') {
while ((*ptr++ = tolower ((unsigned char)*str++)) != '\0') {
}
}
#endif
Expand All @@ -920,7 +920,7 @@ void strToLower (char *str)
int str2lw (char *s) {
int i = 0, len = strlen (s);
while (i < len) {
s[i] = (char) tolower(s[i]);
s[i] = (char) tolower((unsigned char)s[i]);
i++;
}
return len;
Expand Down Expand Up @@ -968,18 +968,18 @@ int strcmpNoCase (const char *str1, const char *str2)
return 1;
}

for (; tolower (*str1) == tolower (*str2); str1++, str2++) {
for (; tolower ((unsigned char)*str1) == tolower ((unsigned char)*str2); str1++, str2++) {
if (*str1 == '\0')
return 0;
}
return (tolower (*str1) - tolower (*str2) < 0) ? -1 : 1;
return (tolower ((unsigned char)*str1) - tolower ((unsigned char)*str2) < 0) ? -1 : 1;
/*
strlen1 = strlen (str1);
strlen2 = strlen (str2);
min = (strlen1 < strlen2) ? strlen1 : strlen2;
for (i = 0; i < min; i++) {
c1 = tolower (str1[i]);
c2 = tolower (str2[i]);
c1 = tolower ((unsigned char)str1[i]);
c2 = tolower ((unsigned char)str2[i]);
if (c1 < c2)
return -1;
if (c1 > c2)
Expand Down
6 changes: 3 additions & 3 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3431,14 +3431,14 @@ void netCDFDataset::SetProjectionFromVar(
for (unsigned int i = 0;
i < strlen(poDS->papszDimName[poDS->nXDimID]) && i < 3; i++)
{
szDimNameX[i] =
(char)tolower((poDS->papszDimName[poDS->nXDimID])[i]);
szDimNameX[i] = (char)tolower(static_cast<unsigned char>(
(poDS->papszDimName[poDS->nXDimID])[i]));
}
szDimNameX[3] = '\0';
// for( unsigned int i = 0;
// (i < strlen(poDS->papszDimName[poDS->nYDimID])
// && i < 3 ); i++ ) {
// szDimNameY[i]=(char)tolower((poDS->papszDimName[poDS->nYDimID])[i]);
// szDimNameY[i]=(char)tolower(static_cast<unsigned char>((poDS->papszDimName[poDS->nYDimID])[i]));
// }
// szDimNameY[3] = '\0';
}
Expand Down
6 changes: 3 additions & 3 deletions frmts/nitf/mgrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,13 @@ static long Break_MGRS_String(char *MGRS, long *Zone,
if (num_letters == 3)
{
/* get letters */
Letters[0] = (toupper(MGRS[j]) - (long)'A');
Letters[0] = (toupper((unsigned char)MGRS[j]) - (long)'A');
if ((Letters[0] == LETTER_I) || (Letters[0] == LETTER_O))
error_code |= MGRS_STRING_ERROR;
Letters[1] = (toupper(MGRS[j + 1]) - (long)'A');
Letters[1] = (toupper((unsigned char)MGRS[j + 1]) - (long)'A');
if ((Letters[1] == LETTER_I) || (Letters[1] == LETTER_O))
error_code |= MGRS_STRING_ERROR;
Letters[2] = (toupper(MGRS[j + 2]) - (long)'A');
Letters[2] = (toupper((unsigned char)MGRS[j + 2]) - (long)'A');
if ((Letters[2] == LETTER_I) || (Letters[2] == LETTER_O))
error_code |= MGRS_STRING_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion frmts/pcidsk/sdk/core/cpcidskblockfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ SysTileDir * CPCIDSKBlockFile::CreateTileDir(void)
std::string oFileOptions = GetFileOptions();

for (char & chIter : oFileOptions)
chIter = (char) toupper((uchar) chIter);
chIter = (char) toupper(static_cast<unsigned char>(chIter));

// Check if we should create a TILEV1 or TILEV2 block directory.
bool bTileV1 = oFileOptions.find("TILEV1") != std::string::npos;
Expand Down
10 changes: 5 additions & 5 deletions frmts/pcidsk/sdk/core/pcidsk_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::string &PCIDSK::UCaseStr( std::string &target )
for( unsigned int i = 0; i < target.size(); i++ )
{
if( islower(static_cast<unsigned char>(target[i])) )
target[i] = (char) toupper(target[i]);
target[i] = (char) toupper(static_cast<unsigned char>(target[i]));
}

return target;
Expand Down Expand Up @@ -403,9 +403,9 @@ int PCIDSK::pci_strcasecmp( const char *string1, const char *string2 )
char c2 = string2[i];

if( islower(static_cast<unsigned char>(c1)) )
c1 = (char) toupper(c1);
c1 = (char) toupper(static_cast<unsigned char>(c1));
if( islower(static_cast<unsigned char>(c2)) )
c2 = (char) toupper(c2);
c2 = (char) toupper(static_cast<unsigned char>(c2));

if( c1 < c2 )
return -1;
Expand Down Expand Up @@ -441,9 +441,9 @@ int PCIDSK::pci_strncasecmp( const char *string1, const char *string2, size_t le
char c2 = string2[i];

if( islower(static_cast<unsigned char>(c1)) )
c1 = (char) toupper(c1);
c1 = (char) toupper(static_cast<unsigned char>(c1));
if( islower(static_cast<unsigned char>(c2)) )
c2 = (char) toupper(c2);
c2 = (char) toupper(static_cast<unsigned char>(c2));

if( c1 < c2 )
return -1;
Expand Down
4 changes: 2 additions & 2 deletions frmts/pcidsk/sdk/segment/cpcidskgcp2segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ void CPCIDSKGCP2Segment::Load()
double x = pimpl_->seg_data.GetDouble(offset + 48, 22);
double y = pimpl_->seg_data.GetDouble(offset + 70, 22);

char cElevDatum = (char)toupper(pimpl_->seg_data.buffer[offset + 47]);
char cElevDatum = (char)toupper(static_cast<unsigned char>(pimpl_->seg_data.buffer[offset + 47]));
PCIDSK::GCP::EElevationDatum elev_datum = cElevDatum != 'M' ?
GCP::EEllipsoidal : GCP::EMeanSeaLevel;

char elev_unit_c = (char)toupper(pimpl_->seg_data.buffer[offset + 46]);
char elev_unit_c = (char)toupper(static_cast<unsigned char>(pimpl_->seg_data.buffer[offset + 46]));
PCIDSK::GCP::EElevationUnit elev_unit = elev_unit_c == 'M' ? GCP::EMetres :
elev_unit_c == 'F' ? GCP::EInternationalFeet :
elev_unit_c == 'A' ? GCP::EAmericanFeet : GCP::EUnknown;
Expand Down
2 changes: 1 addition & 1 deletion frmts/pcidsk/sdk/segment/cpcidskgeoref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ std::string CPCIDSKGeoref::ReformatGeosys( std::string const& geosysIn )
if( *ptr == 'A' || *ptr == 'B' || *ptr == 'Y' || *ptr == 'Z' )
ups_zone = *ptr;
else if( *ptr == 'a' || *ptr == 'b' || *ptr == 'y' || *ptr == 'z' )
ups_zone = toupper( *ptr );
ups_zone = toupper( static_cast<unsigned char>(*ptr) );
else
ups_zone = ' ';

Expand Down
6 changes: 4 additions & 2 deletions frmts/raw/ehdrdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,11 +1094,13 @@ GDALDataset *EHdrDataset::Open(GDALOpenInfo *poOpenInfo, bool bFileSizeCheck)
}
else if (EQUAL(papszTokens[0], "PIXELTYPE"))
{
chPixelType = static_cast<char>(toupper(papszTokens[1][0]));
chPixelType = static_cast<char>(
toupper(static_cast<unsigned char>(papszTokens[1][0])));
}
else if (EQUAL(papszTokens[0], "byteorder"))
{
chByteOrder = static_cast<char>(toupper(papszTokens[1][0]));
chByteOrder = static_cast<char>(
toupper(static_cast<unsigned char>(papszTokens[1][0])));
}

// http://www.worldclim.org/futdown.htm have the projection extensions
Expand Down
3 changes: 2 additions & 1 deletion frmts/raw/eirdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ GDALDataset *EIRDataset::Open(GDALOpenInfo *poOpenInfo)
else if (EQUAL(aosTokens[0], "BYTE_ORDER"))
{
// M for MSB, L for LSB
chByteOrder = static_cast<char>(toupper(aosTokens[1][0]));
chByteOrder = static_cast<char>(
toupper(static_cast<unsigned char>(aosTokens[1][0])));
}
else if (EQUAL(aosTokens[0], "DATA_OFFSET"))
{
Expand Down
3 changes: 2 additions & 1 deletion frmts/wms/minidriver_wms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ CPLErr WMSMiniDriver_WMS::Initialize(CPLXMLNode *config,
// according to the WMS spec so force upper case
for (int i = 0; i < (int)m_transparent.size(); i++)
{
m_transparent[i] = (char)toupper(m_transparent[i]);
m_transparent[i] =
(char)toupper(static_cast<unsigned char>(m_transparent[i]));
}
}

Expand Down
6 changes: 4 additions & 2 deletions gcore/gdal_mdreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,10 @@ static bool GDAL_IMD_AA2R(char ***ppapszIMD)
{
CPLString osValue = CSLFetchNameValue(papszIMD, osTarget);
CPLString osLine;
osTarget.Printf("IMAGE_1.%c%s", tolower(keylist[iKey][0]),
keylist[iKey] + 1);
osTarget.Printf(
"IMAGE_1.%c%s",
tolower(static_cast<unsigned char>(keylist[iKey][0])),
keylist[iKey] + 1);

osLine = osTarget + "=" + osValue;

Expand Down
6 changes: 4 additions & 2 deletions gcore/gdal_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2202,8 +2202,10 @@ int GDALReadWorldFile2(const char *pszBaseFilename, const char *pszExtension,

for (int i = 0; szExtUpper[i] != '\0'; i++)
{
szExtUpper[i] = static_cast<char>(toupper(szExtUpper[i]));
szExtLower[i] = static_cast<char>(tolower(szExtLower[i]));
szExtUpper[i] = static_cast<char>(
toupper(static_cast<unsigned char>(szExtUpper[i])));
szExtLower[i] = static_cast<char>(
tolower(static_cast<unsigned char>(szExtLower[i])));
}

const char *pszTFW = CPLResetExtension(pszBaseFilename, szExtLower);
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogr_srs_pci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ OGRErr OGRSpatialReference::importFromPCI(const char *pszProj,

if (nCode >= -99 && nCode <= 999)
snprintf(szEarthModel, sizeof(szEarthModel), "%c%03d",
toupper(*pszEM), nCode);
toupper(static_cast<unsigned char>(*pszEM)), nCode);

break;
}
Expand Down
9 changes: 5 additions & 4 deletions ogr/ogrsf_frmts/avc/avc_binwr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ AVCBinFile *AVCBinWriteCreateTable(const char *pszInfoPath,
for (i = 0; *pszPtr != '\0' && *pszPtr != '.' && *pszPtr != ' ';
i++, pszPtr++)
{
szCoverName[i] = (char)tolower(*pszPtr);
szCoverName[i] = (char)tolower(static_cast<unsigned char>(*pszPtr));
}
szCoverName[i] = '\0';

Expand All @@ -1802,13 +1802,13 @@ AVCBinFile *AVCBinWriteCreateTable(const char *pszInfoPath,

for (i = 0; i < 3 && *pszPtr != '\0' && *pszPtr != ' '; i++, pszPtr++)
{
szExt[i] = (char)tolower(*pszPtr);
szExt[i] = (char)tolower(static_cast<unsigned char>(*pszPtr));
}
szExt[i] = '\0';

for (i = 0; *pszPtr != '\0' && *pszPtr != ' '; i++, pszPtr++)
{
szSubclass[i] = (char)tolower(*pszPtr);
szSubclass[i] = (char)tolower(static_cast<unsigned char>(*pszPtr));
}
szSubclass[i] = '\0';

Expand Down Expand Up @@ -1979,7 +1979,8 @@ AVCBinFile *_AVCBinWriteCreateDBFTable(const char *pszPath,
for (i = (int)strlen(psFile->pszFilename); *pszDBFBasename;
i++, pszDBFBasename++)
{
psFile->pszFilename[i] = (char)tolower(*pszDBFBasename);
psFile->pszFilename[i] =
(char)tolower(static_cast<unsigned char>(*pszDBFBasename));
}

strcat(psFile->pszFilename, ".dbf");
Expand Down
3 changes: 2 additions & 1 deletion ogr/ogrsf_frmts/avc/avc_e00gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ const char *AVCE00GenStartSection(AVCE00GenInfo *psInfo, AVCFileType eType,
int i;
for (i = 0; pszClassName[i] != '\0'; i++)
{
psInfo->pszBuf[i] = (char)toupper(pszClassName[i]);
psInfo->pszBuf[i] =
(char)toupper(static_cast<unsigned char>(pszClassName[i]));
}
psInfo->pszBuf[i] = '\0';
}
Expand Down
4 changes: 2 additions & 2 deletions ogr/ogrsf_frmts/avc/avc_e00read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ static int _AVCE00ReadBuildSqueleton(AVCE00ReadPtr psInfo, char **papszCoverDir)
CPLSPrintf("EXP 0 %s%s.E00", szCWD, osCoverPathTruncated.c_str()));
pcTmp = pszEXPPath;
for (; *pcTmp != '\0'; pcTmp++)
*pcTmp = (char)toupper(*pcTmp);
*pcTmp = (char)toupper(static_cast<unsigned char>(*pcTmp));

/*-----------------------------------------------------------------
* EXP Header
Expand Down Expand Up @@ -1309,7 +1309,7 @@ static int _AVCE00ReadBuildSqueleton(AVCE00ReadPtr psInfo, char **papszCoverDir)
papszCoverDir[iFile]);
pcTmp = (char *)szFname;
for (; *pcTmp != '\0'; pcTmp++)
*pcTmp = (char)toupper(*pcTmp);
*pcTmp = (char)toupper(static_cast<unsigned char>(*pcTmp));
papszCoverDir[iFile][nLen - 4] = '.';

papszTables = CSLAddString(papszTables, szFname);
Expand Down
7 changes: 4 additions & 3 deletions ogr/ogrsf_frmts/avc/avc_e00write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ static void _AVCE00WriteRenameTable(AVCTableDef *psTableDef,

snprintf(szNewName, sizeof(szNewName), "%s", pszNewCoverName);
for (i = 0; szNewName[i] != '\0'; i++)
szNewName[i] = (char)toupper(szNewName[i]);
szNewName[i] = (char)toupper(static_cast<unsigned char>(szNewName[i]));

/*-----------------------------------------------------------------
* Extract components from the current table name.
Expand Down Expand Up @@ -662,7 +662,7 @@ static int _AVCE00WriteCreateCoverFile(AVCE00WritePtr psInfo, AVCFileType eType,
* Make sure filename is all lowercase and attempt to create the file
*----------------------------------------------------------------*/
for (i = 0; szFname[i] != '\0'; i++)
szFname[i] = (char)tolower(szFname[i]);
szFname[i] = (char)tolower(static_cast<unsigned char>(szFname[i]));

if (nStatus == 0)
{
Expand Down Expand Up @@ -961,7 +961,8 @@ int AVCE00DeleteCoverage(const char *pszCoverToDelete)
{
/* Convert table filename to lowercases */
for (j = 0; papszFiles[i] && papszFiles[i][j] != '\0'; j++)
papszFiles[i][j] = (char)tolower(papszFiles[i][j]);
papszFiles[i][j] =
(char)tolower(static_cast<unsigned char>(papszFiles[i][j]));

/* Delete the .DAT file */
pszFname = CPLSPrintf("%s%s.dat", pszInfoPath, papszFiles[i]);
Expand Down
12 changes: 6 additions & 6 deletions ogr/ogrsf_frmts/cad/libopencad/opencad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ static int CheckCADFile(CADFileIO * pCADFileIO)
size_t nPathLen = strlen( pszFilePath );

if( nPathLen > 3 &&
toupper( pszFilePath[nPathLen - 3] ) == 'D' &&
toupper( pszFilePath[nPathLen - 2] ) == 'X' &&
toupper( pszFilePath[nPathLen - 1] ) == 'F' )
toupper( static_cast<unsigned char>(pszFilePath[nPathLen - 3]) ) == 'D' &&
toupper( static_cast<unsigned char>(pszFilePath[nPathLen - 2]) ) == 'X' &&
toupper( static_cast<unsigned char>(pszFilePath[nPathLen - 1]) ) == 'F' )
{
//TODO: "AutoCAD Binary DXF"
//std::cerr << "DXF ASCII and binary is not supported yet.";
return 0;
}
if( ! ( nPathLen > 3 &&
toupper( pszFilePath[nPathLen - 3] ) == 'D' &&
toupper( pszFilePath[nPathLen - 2] ) == 'W' &&
toupper( pszFilePath[nPathLen - 1] ) == 'G' ) )
toupper( static_cast<unsigned char>(pszFilePath[nPathLen - 3]) ) == 'D' &&
toupper( static_cast<unsigned char>(pszFilePath[nPathLen - 2]) ) == 'W' &&
toupper( static_cast<unsigned char>(pszFilePath[nPathLen - 1]) ) == 'G' ) )
{
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ogr/ogrsf_frmts/dxf/ogr_autocad_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ CPLString ACTextUnescape(const char *pszRawInput, const char *pszEncoding,
if (pszInput[1] == ' ')
osResult += '^';
else
osResult += static_cast<char>(toupper(pszInput[1])) ^ 0x40;
osResult += static_cast<char>(toupper(
static_cast<unsigned char>(pszInput[1]))) ^
0x40;
pszInput++;
}
else if (STARTS_WITH_CI(pszInput, "%%c") ||
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/edigeo/ogredigeodatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ VSILFILE *OGREDIGEODataSource::OpenFile(const char *pszType,
{
CPLString osExtLower = osExt;
for (int i = 0; i < (int)osExt.size(); i++)
osExtLower[i] = (char)tolower(osExt[i]);
osExtLower[i] = (char)tolower(static_cast<unsigned char>(osExt[i]));
CPLString osFilename2 = CPLFormCIFilename(
CPLGetPath(pszName), osTmp.c_str(), osExtLower.c_str());
fp = VSIFOpenL(osFilename2, "rb");
Expand Down
Loading

0 comments on commit c80de4b

Please sign in to comment.