Skip to content

Commit c31f6ea

Browse files
committed
Merge
2 parents dc69bb7 + 4ccfcc6 commit c31f6ea

9 files changed

Lines changed: 77 additions & 63 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/>

‎lib/tokenize.cpp‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4968,6 +4968,21 @@ static const Token* findInitListLambdaEnd(const Token* tok)
49684968
return findLambdaEndScope(tok);
49694969
}
49704970

4971+
static bool isPointerToArrayDeclarator(const Token* tok, const VariableMap& variableMap)
4972+
{
4973+
if (!Token::Match(tok, "( *|&|&&"))
4974+
return false;
4975+
tok = tok->next();
4976+
while (Token::Match(tok, "*|&|&&|const|volatile"))
4977+
tok = tok->next();
4978+
if (tok && tok->isName()) {
4979+
if (variableMap.hasVariable(tok->str()))
4980+
return false;
4981+
tok = tok->next();
4982+
}
4983+
return Token::simpleMatch(tok, ") [");
4984+
}
4985+
49714986
void Tokenizer::setVarIdPass1(bool incremental)
49724987
{
49734988
const bool cpp = isCPP();
@@ -5249,7 +5264,8 @@ void Tokenizer::setVarIdPass1(bool incremental)
52495264

52505265
if (tok3->isLiteral() ||
52515266
(tok3->isName() && (variableMap.hasVariable(tok3->str()) ||
5252-
(tok3->strAt(-1) == "(" && Token::simpleMatch(tok3->next(), "(") && !Token::simpleMatch(tok3->linkAt(1)->next(), "(")))) ||
5267+
(tok3->strAt(-1) == "(" && Token::simpleMatch(tok3->next(), "(") && !Token::simpleMatch(tok3->linkAt(1)->next(), "(") &&
5268+
!isPointerToArrayDeclarator(tok3->next(), variableMap)))) ||
52535269
tok3->isOp() ||
52545270
tok3->str() == "(" ||
52555271
notstart.find(tok3->str()) != notstart.end()) {

‎lib/valueflow.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,6 +2842,9 @@ static bool isDecayedPointer(const Token *tok)
28422842
return false;
28432843
if (!tok->astParent())
28442844
return false;
2845+
// Subscript 'arr[i]': the element is accessed, the array itself is not used as a pointer
2846+
if (Token::simpleMatch(tok->astParent(), "[") && astIsLHS(tok))
2847+
return false;
28452848
if (Token::simpleMatch(tok->astParent(), "=") && astIsRHS(tok))
28462849
return true;
28472850
if (astIsPointer(tok->astParent()) && !Token::simpleMatch(tok->astParent(), "return"))

‎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+
}

‎test/testvalueflow.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,15 @@ class TestValueFlow : public TestFixture {
836836
ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x ["));
837837
}
838838

839+
{
840+
// array of pointers is not decayed when it is subscripted
841+
const char code[] = "const char* x[10];\n"
842+
"void f() {\n"
843+
" x[0] = \"a\";\n"
844+
"}\n";
845+
ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x ["));
846+
}
847+
839848
{
840849
const char code[] = "void f() {\n"
841850
" std::vector<int> v;\n"

‎test/testvarid.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,12 +1632,22 @@ class TestVarID : public TestFixture {
16321632
"A f(3);\n"
16331633
"A f2(true);\n"
16341634
"A g();\n"
1635-
"A e(int c);\n", dinit(TokenizeOptions, $.cpp = false));
1635+
"A e(int c);\n"
1636+
"A h(T (*p)[16]);\n"
1637+
"int x(g(1)[0]);\n"
1638+
"A u(T (*)[16]);\n"
1639+
"A h3(T (*const p)[16]);\n"
1640+
"A h4(T (**p)[16]);\n", dinit(TokenizeOptions, $.cpp = false));
16361641

16371642
const char expected[] = "1: A f@1 ( 3 ) ;\n"
16381643
"2: A f2@2 ( true ) ;\n"
16391644
"3: A g ( ) ;\n"
1640-
"4: A e ( int c@3 ) ;\n";
1645+
"4: A e ( int c@3 ) ;\n"
1646+
"5: A h ( T ( * p@4 ) [ 16 ] ) ;\n"
1647+
"6: int x@5 ( g ( 1 ) [ 0 ] ) ;\n"
1648+
"7: A u ( T ( * ) [ 16 ] ) ;\n"
1649+
"8: A h3 ( T ( * const p@6 ) [ 16 ] ) ;\n"
1650+
"9: A h4 ( T ( * * p@7 ) [ 16 ] ) ;\n";
16411651

16421652
ASSERT_EQUALS(expected, actual);
16431653
}

0 commit comments

Comments
 (0)