Skip to content

Commit 58ee583

Browse files
authored
Fix #15092: False positive: uninitvar with Microsoft CRT localtime_s. (#8924)
1 parent 61d2460 commit 58ee583

5 files changed

Lines changed: 36 additions & 60 deletions

File tree

‎cfg/std.cfg‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,16 +3238,15 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
32383238
<not-uninit/>
32393239
</arg>
32403240
</function>
3241-
<!-- struct tm *localtime_s(const time_t *restrict time, struct tm *restrict result) -->
3242-
<function name="localtime_s,std::localtime_s">
3243-
<returnValue type="struct tm *"/>
3241+
<!-- struct tm *localtime_s(const time_t *restrict time, struct tm *restrict result)
3242+
Order of arguments and return type differs between implementations, define a common baseline here. -->
3243+
<function name="localtime_s">
32443244
<noreturn>false</noreturn>
32453245
<leak-ignore/>
3246-
<arg nr="1" direction="in">
3246+
<arg nr="1">
32473247
<not-null/>
3248-
<not-uninit/>
32493248
</arg>
3250-
<arg nr="2" direction="out">
3249+
<arg nr="2">
32513250
<not-null/>
32523251
</arg>
32533252
</function>

‎cfg/windows.cfg‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4660,9 +4660,10 @@ HFONT CreateFont(
46604660
<not-uninit/>
46614661
</arg>
46624662
</function>
4663-
<!-- errno_t _localtime32_s( struct tm* _tm, const __time32_t *time);
4663+
<!-- errno_t localtime_s( struct tm* const tmDest, time_t const* const sourceTime);
4664+
errno_t _localtime32_s( struct tm* _tm, const __time32_t *time);
46644665
errno_t _localtime64_s( struct tm* _tm, const __time64_t *time); -->
4665-
<function name="_localtime32_s,_localtime64_s">
4666+
<function name="localtime_s,_localtime32_s,_localtime64_s">
46664667
<returnValue type="errno_t"/>
46674668
<noreturn>false</noreturn>
46684669
<leak-ignore/>

‎test/cfg/std.c‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,26 +174,6 @@ void nullPointer_qsort(void *base, size_t n, size_t size, int (*cmp)(const void
174174
qsort(base, n, size, qsort_cmpfunc);
175175
}
176176

177-
// As with all bounds-checked functions, localtime_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including time.h.
178-
#ifdef __STDC_LIB_EXT1__
179-
void uninitvar_localtime_s(const time_t *restrict time, struct tm *restrict result)
180-
{
181-
const time_t *restrict Time;
182-
// cppcheck-suppress uninitvar
183-
(void)localtime_s(Time, result);
184-
(void)localtime_s(time, result);
185-
}
186-
187-
void nullPointer_localtime_s(const time_t *restrict time, struct tm *restrict result)
188-
{
189-
// cppcheck-suppress nullPointer
190-
(void)localtime_s(NULL, result);
191-
// cppcheck-suppress nullPointer
192-
(void)localtime_s(time, NULL);
193-
(void)localtime_s(time, result);
194-
}
195-
#endif // __STDC_LIB_EXT1__
196-
197177
size_t bufferAccessOutOfBounds_wcsrtombs(char * dest, const wchar_t ** src, size_t len, mbstate_t * ps)
198178
{
199179
char buf[42];

‎test/cfg/std.cpp‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -689,38 +689,6 @@ void *bufferAccessOutOfBounds_memchr(void *s, int c, size_t n)
689689
return std::memchr(s,c,n);
690690
}
691691

692-
// As with all bounds-checked functions, localtime_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including time.h.
693-
#ifdef __STDC_LIB_EXT1__
694-
void uninitvar_localtime_s(const std::time_t *restrict time, struct tm *restrict result)
695-
{
696-
// cppcheck-suppress valueFlowBailoutIncompleteVar
697-
const std::time_t *restrict Time;
698-
// TODO cppcheck-suppress uninitvar
699-
(void)std::localtime_s(Time, result);
700-
(void)std::localtime_s(time, result);
701-
}
702-
703-
void nullPointer_localtime_s(const std::time_t *restrict time, struct tm *restrict result)
704-
{
705-
// cppcheck-suppress nullPointer
706-
(void)std::localtime_s(NULL, result);
707-
// cppcheck-suppress nullPointer
708-
(void)std::localtime_s(time, NULL);
709-
(void)std::localtime_s(time, result);
710-
}
711-
712-
void memleak_localtime_s(const std::time_t *restrict time, struct tm *restrict result) // #9258
713-
{
714-
const time_t t = time(0);
715-
const struct tm* const now = new tm();
716-
if (localtime_s(now, &t) == 0) {
717-
// cppcheck-suppress valueFlowBailoutIncompleteVar
718-
std::cout << now->tm_mday << std::endl;
719-
}
720-
// cppcheck-suppress memleak
721-
}
722-
#endif // __STDC_LIB_EXT1__
723-
724692
size_t nullPointer_strftime(char *s, size_t max, const char *fmt, const struct tm *p)
725693
{
726694
// cppcheck-suppress nullPointer

‎test/cfg/windows.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,3 +1260,31 @@ void constParameterPointer_SetupDiGetDeviceInstanceId(HDEVINFO info, SP_DEVINFO_
12601260
TCHAR buffer[buffer_size];
12611261
SetupDiGetDeviceInstanceId(info, data, buffer, buffer_size, NULL);
12621262
}
1263+
1264+
void uninitvar_localtime_s(const time_t *time, struct tm *result)
1265+
{
1266+
time_t uninit_time;
1267+
struct tm uninit_result;
1268+
// cppcheck-suppress uninitvar
1269+
(void)localtime_s(result, &uninit_time);
1270+
(void)localtime_s(&uninit_result, time);
1271+
(void)localtime_s(result, time);
1272+
}
1273+
1274+
void nullPointer_localtime_s(const time_t *time, struct tm *result)
1275+
{
1276+
// cppcheck-suppress nullPointer
1277+
(void)localtime_s(result, NULL);
1278+
// cppcheck-suppress nullPointer
1279+
(void)localtime_s(NULL, time);
1280+
(void)localtime_s(result, time);
1281+
}
1282+
1283+
void memleak_localtime_s(void) // #9258
1284+
{
1285+
const time_t t = time(0);
1286+
struct tm *const now = new tm();
1287+
if (localtime_s(now, &t) == 0)
1288+
printf("%d\n", now->tm_mday);
1289+
// cppcheck-suppress memleak
1290+
}

0 commit comments

Comments
 (0)