Skip to content

Commit 5be16c3

Browse files
committed
std.cfg: Ensure null pointer input is correctly handled for fgets() and fgetws().
1 parent df84bed commit 5be16c3

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

test/cfg/std.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ void bufferAccessOutOfBounds(void)
8585
free(pAlloc1);
8686
}
8787

88+
wchar_t* nullPointer_fgetws(wchar_t* buffer, int n, FILE* stream)
89+
{
90+
// cppcheck-suppress nullPointer
91+
(void)fgetws(NULL,n,stream);
92+
// cppcheck-suppress nullPointer
93+
(void)fgetws(buffer,n,NULL);
94+
// No warning is expected
95+
return fgetws(buffer, n, stream);
96+
}
97+
98+
char* nullPointer_fgets(char *buffer, int n, FILE *stream)
99+
{
100+
// cppcheck-suppress nullPointer
101+
(void)fgets(NULL,n,stream);
102+
// cppcheck-suppress nullPointer
103+
(void)fgets(buffer,n,NULL);
104+
// No warning is expected
105+
return fgets(buffer, n, stream);
106+
}
107+
88108
void memleak_aligned_alloc(void)
89109
{
90110
// cppcheck-suppress unusedAllocatedMemory

test/cfg/std.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,26 @@ void uninitvar_fsetpos(void)
12641264
(void)std::fsetpos(stream,ptr);
12651265
}
12661266

1267+
wchar_t* nullPointer_fgetws(wchar_t* buffer, int n, FILE* stream)
1268+
{
1269+
// cppcheck-suppress nullPointer
1270+
(void)std::fgetws(NULL,n,stream);
1271+
// cppcheck-suppress nullPointer
1272+
(void)std::fgetws(buffer,n,NULL);
1273+
// No warning is expected
1274+
return std::fgetws(buffer, n, stream);
1275+
}
1276+
1277+
char* nullPointer_fgets(char *buffer, int n, FILE *stream)
1278+
{
1279+
// cppcheck-suppress nullPointer
1280+
(void)std::fgets(NULL,n,stream);
1281+
// cppcheck-suppress nullPointer
1282+
(void)std::fgets(buffer,n,NULL);
1283+
// No warning is expected
1284+
return std::fgets(buffer, n, stream);
1285+
}
1286+
12671287
void uninitvar_fgets(void)
12681288
{
12691289
char *buffer;

0 commit comments

Comments
 (0)