Skip to content

Commit

Permalink
port/: fix cppcheck nullPointerOutOfMemory
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 18, 2025
1 parent b0aacd1 commit 8c7e61e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
5 changes: 5 additions & 0 deletions port/cpl_minizip_unzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,11 @@ extern unzFile ZEXPORT cpl_unzOpen2(const char *path,
us.current_file_ok = 0;

s = static_cast<unz_s *>(ALLOC(sizeof(unz_s)));
if (!s)
{
ZCLOSE(us.z_filefunc, us.filestream);
return nullptr;
}
*s = us;
cpl_unzGoToFirstFile(reinterpret_cast<unzFile>(s));
return reinterpret_cast<unzFile>(s);
Expand Down
90 changes: 48 additions & 42 deletions port/cpl_vsisimple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,67 +1399,73 @@ GIntBig CPLGetPhysicalRAM(void)
// which seems to be necessary for some container solutions
// Cf https://lists.osgeo.org/pipermail/gdal-dev/2023-January/056784.html
FILE *f = fopen("/proc/meminfo", "rb");
char szLine[256];
while (fgets(szLine, sizeof(szLine), f))
if (f)
{
// Find line like "MemTotal: 32525176 kB"
if (strncmp(szLine, "MemTotal:", strlen("MemTotal:")) == 0)
char szLine[256];
while (fgets(szLine, sizeof(szLine), f))
{
char *pszVal = szLine + strlen("MemTotal:");
pszVal += strspn(pszVal, " ");
char *pszEnd = strstr(pszVal, " kB");
if (pszEnd)
// Find line like "MemTotal: 32525176 kB"
if (strncmp(szLine, "MemTotal:", strlen("MemTotal:")) == 0)
{
*pszEnd = 0;
if (CPLGetValueType(pszVal) == CPL_VALUE_INTEGER)
char *pszVal = szLine + strlen("MemTotal:");
pszVal += strspn(pszVal, " ");
char *pszEnd = strstr(pszVal, " kB");
if (pszEnd)
{
const GUIntBig nLimit =
CPLScanUIntBig(pszVal,
static_cast<int>(strlen(pszVal))) *
1024;
nVal = static_cast<GIntBig>(
std::min(static_cast<GUIntBig>(nVal), nLimit));
*pszEnd = 0;
if (CPLGetValueType(pszVal) == CPL_VALUE_INTEGER)
{
const GUIntBig nLimit =
CPLScanUIntBig(
pszVal, static_cast<int>(strlen(pszVal))) *
1024;
nVal = static_cast<GIntBig>(
std::min(static_cast<GUIntBig>(nVal), nLimit));
}
}
break;
}
break;
}
fclose(f);
}
fclose(f);
}

char szGroupName[256];
bool bFromMemory = false;
szGroupName[0] = 0;
{
FILE *f = fopen("/proc/self/cgroup", "rb");
char szLine[256];
// Find line like "6:memory:/user.slice/user-1000.slice/[email protected]"
// and store "/user.slice/user-1000.slice/[email protected]" in
// szMemoryPath for cgroup V1 or single line "0::/...." for cgroup V2.
while (fgets(szLine, sizeof(szLine), f))
if (f)
{
const char *pszMemory = strstr(szLine, ":memory:");
if (pszMemory)
char szLine[256];
// Find line like "6:memory:/user.slice/user-1000.slice/[email protected]"
// and store "/user.slice/user-1000.slice/[email protected]" in
// szMemoryPath for cgroup V1 or single line "0::/...." for cgroup V2.
while (fgets(szLine, sizeof(szLine), f))
{
bFromMemory = true;
snprintf(szGroupName, sizeof(szGroupName), "%s",
pszMemory + strlen(":memory:"));
char *pszEOL = strchr(szGroupName, '\n');
if (pszEOL)
*pszEOL = '\0';
break;
}
if (strncmp(szLine, "0::", strlen("0::")) == 0)
{
snprintf(szGroupName, sizeof(szGroupName), "%s",
szLine + strlen("0::"));
char *pszEOL = strchr(szGroupName, '\n');
if (pszEOL)
*pszEOL = '\0';
break;
const char *pszMemory = strstr(szLine, ":memory:");
if (pszMemory)
{
bFromMemory = true;
snprintf(szGroupName, sizeof(szGroupName), "%s",
pszMemory + strlen(":memory:"));
char *pszEOL = strchr(szGroupName, '\n');
if (pszEOL)
*pszEOL = '\0';
break;
}
if (strncmp(szLine, "0::", strlen("0::")) == 0)
{
snprintf(szGroupName, sizeof(szGroupName), "%s",
szLine + strlen("0::"));
char *pszEOL = strchr(szGroupName, '\n');
if (pszEOL)
*pszEOL = '\0';
break;
}
}
fclose(f);
}
fclose(f);
}
if (szGroupName[0])
{
Expand Down
5 changes: 5 additions & 0 deletions port/vsipreload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,11 @@ DIR CPL_DLL *opendir(const char *name)
{
VSIDIRPreload *mydir =
static_cast<VSIDIRPreload *>(malloc(sizeof(VSIDIRPreload)));
if (!mydir)
{
CSLDestroy(papszDir);
return nullptr;
}
mydir->pszDirname = CPLStrdup(name);
mydir->papszDir = papszDir;
mydir->nIter = 0;
Expand Down

0 comments on commit 8c7e61e

Please sign in to comment.