/
checkbufferoverrun.cpp
2059 lines (1766 loc) · 86.6 KB
/
checkbufferoverrun.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2016 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Buffer overrun..
//---------------------------------------------------------------------------
#include "checkbufferoverrun.h"
#include "tokenize.h"
#include "mathlib.h"
#include "symboldatabase.h"
#include "astutils.h"
#include <algorithm>
#include <sstream>
#include <list>
#include <cstdlib>
#include <stack>
#include <tinyxml2.h>
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace {
CheckBufferOverrun instance;
}
//---------------------------------------------------------------------------
// CWE ids used:
static const CWE CWE131(131U); // Incorrect Calculation of Buffer Size
static const CWE CWE170(170U); // Improper Null Termination
static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE682(682U); // Incorrect Calculation
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
static const CWE CWE786(786U); // Access of Memory Location Before Start of Buffer
static const CWE CWE788(788U); // Access of Memory Location After End of Buffer
//---------------------------------------------------------------------------
static void makeArrayIndexOutOfBoundsError(std::ostream& oss, const CheckBufferOverrun::ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
oss << "Array '" << arrayInfo.varname();
for (std::size_t i = 0; i < arrayInfo.num().size(); ++i)
oss << "[" << arrayInfo.num(i) << "]";
if (index.size() == 1)
oss << "' accessed at index " << index[0] << ", which is";
else {
oss << "' index " << arrayInfo.varname();
for (std::size_t i = 0; i < index.size(); ++i)
oss << "[" << index[i] << "]";
}
oss << " out of bounds.";
}
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
std::ostringstream oss;
makeArrayIndexOutOfBoundsError(oss, arrayInfo, index);
reportError(tok, Severity::error, "arrayIndexOutOfBounds", oss.str(), CWE788, false);
}
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<ValueFlow::Value> &index)
{
const Token *condition = nullptr;
for (std::size_t i = 0; i < index.size(); ++i) {
if (condition == nullptr)
condition = index[i].condition;
}
if (condition != nullptr) {
if (!_settings->isEnabled(Settings::WARNING))
return;
std::ostringstream errmsg;
errmsg << ValueFlow::eitherTheConditionIsRedundant(condition) << " or the array '" << arrayInfo.varname();
for (std::size_t i = 0; i < arrayInfo.num().size(); ++i)
errmsg << "[" << arrayInfo.num(i) << "]";
if (index.size() == 1)
errmsg << "' is accessed at index " << index[0].intvalue << ", which is out of bounds.";
else {
errmsg << "' index " << arrayInfo.varname();
for (std::size_t i = 0; i < index.size(); ++i)
errmsg << "[" << index[i].intvalue << "]";
errmsg << " is out of bounds.";
}
std::list<const Token *> callstack;
callstack.push_back(tok);
callstack.push_back(condition);
reportError(callstack, Severity::warning, "arrayIndexOutOfBoundsCond", errmsg.str(), CWE119, false);
} else {
std::ostringstream errmsg;
errmsg << "Array '" << arrayInfo.varname();
for (std::size_t i = 0; i < arrayInfo.num().size(); ++i)
errmsg << "[" << arrayInfo.num(i) << "]";
if (index.size() == 1)
errmsg << "' accessed at index " << index[0].intvalue << ", which is out of bounds.";
else {
errmsg << "' index " << arrayInfo.varname();
for (std::size_t i = 0; i < index.size(); ++i)
errmsg << "[" << index[i].intvalue << "]";
errmsg << " out of bounds.";
}
reportError(tok, Severity::error, "arrayIndexOutOfBounds", errmsg.str(), CWE119, false);
}
}
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
std::ostringstream oss;
makeArrayIndexOutOfBoundsError(oss, arrayInfo, index);
reportError(callstack, Severity::error, "arrayIndexOutOfBounds", oss.str());
}
static std::string bufferOverrunMessage(std::string varnames)
{
varnames.erase(std::remove(varnames.begin(), varnames.end(), ' '), varnames.end());
std::string errmsg("Buffer is accessed out of bounds");
if (!varnames.empty())
errmsg += ": " + varnames;
else
errmsg += ".";
return errmsg;
}
void CheckBufferOverrun::bufferOverrunError(const Token *tok, const std::string &varnames)
{
reportError(tok, Severity::error, "bufferAccessOutOfBounds", bufferOverrunMessage(varnames), CWE788, false);
}
void CheckBufferOverrun::bufferOverrunError(const std::list<const Token *> &callstack, const std::string &varnames)
{
reportError(callstack, Severity::error, "bufferAccessOutOfBounds", bufferOverrunMessage(varnames));
}
void CheckBufferOverrun::possibleBufferOverrunError(const Token *tok, const std::string &src, const std::string &dst, bool cat)
{
if (cat)
reportError(tok, Severity::warning, "possibleBufferAccessOutOfBounds",
"Possible buffer overflow if strlen(" + src + ") is larger than sizeof(" + dst + ")-strlen(" + dst +").\n"
"Possible buffer overflow if strlen(" + src + ") is larger than sizeof(" + dst + ")-strlen(" + dst +"). "
"The source buffer is larger than the destination buffer so there is the potential for overflowing the destination buffer.", CWE398, false);
else
reportError(tok, Severity::warning, "possibleBufferAccessOutOfBounds",
"Possible buffer overflow if strlen(" + src + ") is larger than or equal to sizeof(" + dst + ").\n"
"Possible buffer overflow if strlen(" + src + ") is larger than or equal to sizeof(" + dst + "). "
"The source buffer is larger than the destination buffer so there is the potential for overflowing the destination buffer.", CWE398, false);
}
void CheckBufferOverrun::strncatUsageError(const Token *tok)
{
if (_settings && !_settings->isEnabled(Settings::WARNING))
return;
reportError(tok, Severity::warning, "strncatUsage",
"Dangerous usage of strncat - 3rd parameter is the maximum number of characters to append.\n"
"At most, strncat appends the 3rd parameter's amount of characters and adds a terminating null byte.\n"
"The safe way to use strncat is to subtract one from the remaining space in the buffer and use it as 3rd parameter."
"Source: http://www.cplusplus.com/reference/cstring/strncat/\n"
"Source: http://www.opensource.apple.com/source/Libc/Libc-167/gen.subproj/i386.subproj/strncat.c", CWE119, false);
}
void CheckBufferOverrun::outOfBoundsError(const Token *tok, const std::string &what, const bool show_size_info, const MathLib::bigint &supplied_size, const MathLib::bigint &actual_size)
{
std::ostringstream oss;
oss << what << " is out of bounds";
if (show_size_info)
oss << ": Supplied size " << supplied_size << " is larger than actual size " << actual_size;
oss << '.';
reportError(tok, Severity::error, "outOfBounds", oss.str(), CWE788, false);
}
void CheckBufferOverrun::pointerOutOfBoundsError(const Token *tok, const Token *index, const MathLib::bigint indexvalue)
{
// The severity is portability instead of error since this ub doesn't
// cause bad behaviour on most implementations. people create out
// of bounds pointers by intention.
const std::string expr(tok ? tok->expressionString() : std::string());
std::string errmsg;
if (index && !index->isNumber()) {
errmsg = "Undefined behaviour, when '" +
index->expressionString() +
"' is " +
MathLib::toString(indexvalue) +
" the pointer arithmetic '" + expr + "' is out of bounds";
} else {
errmsg = "Undefined behaviour, pointer arithmetic '" + expr + "' is out of bounds";
}
std::string verbosemsg(errmsg + ". From chapter 6.5.6 in the C specification:\n"
"\"When an expression that has integer type is added to or subtracted from a pointer, ..\" and then \"If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.\"");
reportError(tok, Severity::portability, "pointerOutOfBounds", errmsg + ".\n" + verbosemsg, CWE398, false);
/*
"Undefined behaviour: The result of this pointer arithmetic does not point into
or just one element past the end of the " + object + ".
Further information:
https://www.securecoding.cert.org/confluence/display/seccode/ARR30-C.+Do+not+form+or+use+out+of+bounds+pointers+or+array+subscripts");
*/
}
void CheckBufferOverrun::sizeArgumentAsCharError(const Token *tok)
{
if (_settings && !_settings->isEnabled(Settings::WARNING))
return;
reportError(tok, Severity::warning, "sizeArgumentAsChar", "The size argument is given as a char constant.", CWE682, false);
}
void CheckBufferOverrun::terminateStrncpyError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::warning, "terminateStrncpy",
"The buffer '" + varname + "' may not be null-terminated after the call to strncpy().\n"
"If the source string's size fits or exceeds the given size, strncpy() does not add a "
"zero at the end of the buffer. This causes bugs later in the code if the code "
"assumes buffer is null-terminated.", CWE170, true);
}
void CheckBufferOverrun::cmdLineArgsError(const Token *tok)
{
reportError(tok, Severity::error, "insecureCmdLineArgs", "Buffer overrun possible for long command line arguments.", CWE119, false);
}
void CheckBufferOverrun::bufferNotZeroTerminatedError(const Token *tok, const std::string &varname, const std::string &function)
{
const std::string errmsg = "The buffer '" + varname + "' is not null-terminated after the call to " + function + "().\n"
"The buffer '" + varname + "' is not null-terminated after the call to " + function + "(). "
"This will cause bugs later in the code if the code assumes the buffer is null-terminated.";
reportError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg, CWE170, true);
}
void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &functionName, const std::string &varname)
{
reportError(tok, Severity::warning, "argumentSize", "The array '" + varname + "' is too small, the function '" + functionName + "' expects a bigger one.", CWE398, false);
}
void CheckBufferOverrun::negativeMemoryAllocationSizeError(const Token *tok)
{
reportError(tok, Severity::error, "negativeMemoryAllocationSize",
"Memory allocation size is negative.\n"
"Memory allocation size is negative."
"Negative allocation size has no specified behaviour.", CWE131, false);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Check array usage..
//---------------------------------------------------------------------------
static bool isAddressOf(const Token *tok)
{
const Token *tok2 = tok->astParent();
while (Token::Match(tok2, "%name%|.|::|["))
tok2 = tok2->astParent();
return tok2 && tok2->str() == "&" && !(tok2->astOperand1() && tok2->astOperand2());
}
/**
* bailout if variable is used inside if/else/switch block or if there is "break"
* @param tok token for "if" or "switch"
* @param varid variable id
* @return is bailout recommended?
*/
static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
{
const Token* end = tok->linkAt(1)->linkAt(1);
if (Token::simpleMatch(end, "} else {")) // scan the else-block
end = end->linkAt(2);
if (Token::simpleMatch(end, "{")) // Ticket #5203: Invalid code, bailout
return true;
// Used later to check if the body belongs to a "if"
const bool is_if = tok->str() == "if";
for (; tok && tok != end; tok = tok->next()) {
// If scanning a "if" block then bailout for "break"
if (is_if && (tok->str() == "break" || tok->str() == "continue"))
return true;
// bailout for "return"
else if (tok->str() == "return")
return true;
// bailout if varid is found
else if (tok->varId() == varid)
return true;
}
// No bailout stuff found => return false
return false;
}
//---------------------------------------------------------------------------
static bool checkMinSizes(const std::vector<Library::ArgumentChecks::MinSize> &minsizes, const Token * const ftok, const MathLib::bigint arraySize, const Token **charSizeToken, const Settings * const settings)
{
if (charSizeToken)
*charSizeToken = nullptr;
if (minsizes.empty())
return false;
// All conditions must be true
bool error = true;
for (std::vector<Library::ArgumentChecks::MinSize>::const_iterator minsize = minsizes.begin(); minsize != minsizes.end(); ++minsize) {
if (!error)
return false;
error = false;
const Token *argtok = ftok->tokAt(2);
for (int argnum = 1; argtok && argnum < minsize->arg; argnum++)
argtok = argtok->nextArgument();
if (!argtok)
return false;
switch (minsize->type) {
case Library::ArgumentChecks::MinSize::ARGVALUE:
if (Token::Match(argtok, "%num% ,|)")) {
const MathLib::bigint sz = MathLib::toLongNumber(argtok->str());
if (sz > arraySize)
error = true;
} else if (argtok->tokType() == Token::eChar && Token::Match(argtok->next(), ",|)") && charSizeToken)
*charSizeToken = argtok; //sizeArgumentAsCharError(argtok);
break;
case Library::ArgumentChecks::MinSize::MUL:
// TODO: handle arbitrary arg2
if (minsize->arg2 == minsize->arg+1 && Token::Match(argtok, "%num% , %num% ,|)")) {
const MathLib::bigint sz = MathLib::toLongNumber(argtok->str()) * MathLib::toLongNumber(argtok->strAt(2));
if (sz > arraySize)
error = true;
}
break;
case Library::ArgumentChecks::MinSize::STRLEN: {
if (settings->library.isargformatstr(ftok,minsize->arg)) {
std::list<const Token *> parameters;
const std::string &formatstr(argtok->str());
const Token *argtok2 = argtok;
while ((argtok2 = argtok2->nextArgument()) != nullptr) {
if (Token::Match(argtok2, "%num%|%str% [,)]"))
parameters.push_back(argtok2);
else
parameters.push_back(nullptr);
}
const MathLib::biguint len = CheckBufferOverrun::countSprintfLength(formatstr, parameters);
if (len > arraySize + 2U)
error = true;
} else {
const Token *strtoken = argtok->getValueTokenMaxStrLength();
if (strtoken && Token::getStrLength(strtoken) >= arraySize)
error = true;
}
}
break;
case Library::ArgumentChecks::MinSize::SIZEOF:
if (argtok->tokType() == Token::eString && Token::getStrLength(argtok) >= arraySize)
error = true;
break;
case Library::ArgumentChecks::MinSize::NONE:
return false;
};
}
return error;
}
void CheckBufferOverrun::checkFunctionParameter(const Token &ftok, unsigned int paramIndex, const ArrayInfo &arrayInfo, const std::list<const Token *>& callstack)
{
const std::vector<Library::ArgumentChecks::MinSize> * const minsizes = _settings->library.argminsizes(&ftok, paramIndex);
if (minsizes) {
MathLib::bigint arraySize = arrayInfo.element_size();
if (arraySize == 0)
return;
for (std::size_t i = 0; i < arrayInfo.num().size(); ++i)
arraySize *= arrayInfo.num(i);
// dimension is 0 or unknown => bailout
if (arraySize == 0)
return;
const Token *charSizeToken = nullptr;
if (checkMinSizes(*minsizes, &ftok, arraySize, &charSizeToken, _settings))
bufferOverrunError(callstack, arrayInfo.varname());
if (charSizeToken)
sizeArgumentAsCharError(charSizeToken);
}
// Calling a user function?
// only 1-dimensional arrays can be checked currently
else if (arrayInfo.num().size() == 1) {
const Function* const func = ftok.function();
if (func && func->hasBody()) {
// Get corresponding parameter..
const Variable* const parameter = func->getArgumentVar(paramIndex-1);
// Ensure that it has a compatible size..
if (!parameter || sizeOfType(parameter->typeStartToken()) != arrayInfo.element_size())
return;
// No variable id occur for instance when:
// - Variable function arguments: "void f(...)"
// - Unnamed parameter: "void f(char *)"
if (parameter->declarationId() == 0)
return;
// Check the parameter usage in the function scope..
for (const Token* ftok2 = func->functionScope->classStart; ftok2 != func->functionScope->classEnd; ftok2 = ftok2->next()) {
if (Token::Match(ftok2, "if|for|switch|while (")) {
// bailout if there is buffer usage..
if (bailoutIfSwitch(ftok2, parameter->declarationId())) {
break;
}
// no bailout is needed. skip the if-block
else {
// goto end of if block..
ftok2 = ftok2->linkAt(1)->linkAt(1);
if (Token::simpleMatch(ftok2, "} else {"))
ftok2 = ftok2->linkAt(2);
if (!ftok2)
break;
continue;
}
}
if (ftok2->str() == "}")
break;
if (ftok2->varId() == parameter->declarationId()) {
if (Token::Match(ftok2->previous(), "-- %name%") ||
Token::Match(ftok2, "%name% --"))
break;
if (Token::Match(ftok2->previous(), ";|{|}|%op% %name% [ %num% ]")) {
const MathLib::bigint index = MathLib::toLongNumber(ftok2->strAt(2));
if (index >= 0 && arrayInfo.num(0) > 0 && index >= arrayInfo.num(0)) {
std::list<const Token *> callstack2(callstack);
callstack2.push_back(ftok2);
std::vector<MathLib::bigint> indexes(1, index);
arrayIndexOutOfBoundsError(callstack2, arrayInfo, indexes);
}
}
}
// Calling function..
if (Token::Match(ftok2, "%name% (")) {
ArrayInfo ai(arrayInfo);
ai.declarationId(parameter->declarationId());
checkFunctionCall(ftok2, ai, callstack);
}
}
}
}
// Check 'float x[10]' arguments in declaration
if (_settings->isEnabled(Settings::WARNING)) {
const Function* const func = ftok.function();
// If argument is '%type% a[num]' then check bounds against num
if (func) {
const Variable* const argument = func->getArgumentVar(paramIndex-1);
const Token *nameToken;
if (argument && Token::Match(argument->typeStartToken(), "%type% %var% [ %num% ] [,)[]")
&& (nameToken = argument->nameToken()) != nullptr) {
const Token *tok2 = nameToken->next();
MathLib::bigint argsize = sizeOfType(argument->typeStartToken());
if (argsize == 100) // unknown size
argsize = 0;
do {
argsize *= MathLib::toLongNumber(tok2->strAt(1));
tok2 = tok2->tokAt(3);
} while (Token::Match(tok2, "[ %num% ] [,)[]"));
MathLib::bigint arraysize = arrayInfo.element_size();
if (arraysize == 100) // unknown size
arraysize = 0;
for (std::size_t i = 0; i < arrayInfo.num().size(); i++)
arraysize *= arrayInfo.num(i);
if (Token::Match(tok2, "[,)]") && arraysize > 0 && argsize > arraysize)
argumentSizeError(&ftok, ftok.str(), arrayInfo.varname());
}
}
}
}
void CheckBufferOverrun::checkFunctionCall(const Token *tok, const ArrayInfo &arrayInfo, std::list<const Token *> callstack)
{
// Don't go deeper than 2 levels, the checking can get very slow
// when there is no limit
if (callstack.size() >= 2)
return;
// Prevent recursion
for (std::list<const Token*>::const_iterator it = callstack.cbegin(); it != callstack.cend(); ++it) {
// Same function name => bail out
if (tok->str() == (*it)->str())
return;
}
callstack.push_back(tok);
const unsigned int declarationId = arrayInfo.declarationId();
const Token *argtok = tok->tokAt(2);
unsigned int argnr = 1U;
while (argtok) {
if (Token::Match(argtok, "%varid% ,|)", declarationId))
checkFunctionParameter(*tok, argnr, arrayInfo, callstack);
else if (Token::Match(argtok, "%varid% + %num% ,|)", declarationId)) {
const ArrayInfo ai(arrayInfo.limit(MathLib::toLongNumber(argtok->strAt(2))));
checkFunctionParameter(*tok, argnr, ai, callstack);
}
// goto next parameter..
argtok = argtok->nextArgument();
argnr++;
}
}
void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<const std::string*> &varname, const ArrayInfo &arrayInfo)
{
const MathLib::bigint size = arrayInfo.num(0);
if (size <= 0) // unknown size
return;
if (tok->str() == "return") {
tok = tok->next();
if (!tok)
return;
}
const bool printInconclusive = _settings->inconclusive;
const MathLib::bigint total_size = arrayInfo.element_size() * size;
const unsigned int declarationId = arrayInfo.declarationId();
std::string varnames;
for (std::size_t i = 0; i < varname.size(); ++i)
varnames += (i == 0 ? "" : " . ") + *varname[i];
const int varcount = varname.empty() ? 0 : static_cast<int>((varname.size() - 1) * 2U);
// ValueFlow array index..
if ((declarationId > 0 && Token::Match(tok, "%varid% [", declarationId)) ||
(declarationId == 0 && Token::simpleMatch(tok, (varnames + " [").c_str()))) {
const Token *tok2 = tok->next();
while (tok2->str() != "[")
tok2 = tok2->next();
valueFlowCheckArrayIndex(tok2, arrayInfo);
}
// If the result of pointer arithmetic means that the pointer is
// out of bounds then this flag will be set.
bool pointerIsOutOfBounds = false;
const bool printPortability = _settings->isEnabled(Settings::PORTABILITY);
for (const Token* const end = tok->scope()->classEnd; tok && tok != end; tok = tok->next()) {
if (declarationId != 0 && Token::Match(tok, "%varid% = new|malloc|realloc", declarationId)) {
// Abort
break;
}
// reassign buffer
if (declarationId > 0 && Token::Match(tok, "[;{}] %varid% = %any%", declarationId)) {
// using varid .. bailout
if (tok->tokAt(3)->varId() != declarationId)
break;
pointerIsOutOfBounds = false;
}
// Array index..
if ((declarationId > 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && Token::Match(tok->next(), "%varid% [", declarationId))) ||
(declarationId == 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && (Token::Match(tok->next(), (varnames + " [").c_str()) || Token::Match(tok->next(), (*varname[0] +" [ %num% ] . " + *varname[1] + " [ %num% ]").c_str()))))) {
std::vector<MathLib::bigint> indexes;
const Token *tok2 = tok->tokAt(2 + varcount);
for (; Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) {
const MathLib::bigint index = MathLib::toLongNumber(tok2->strAt(1));
indexes.push_back(index);
}
for (; Token::Match(tok2->tokAt(3), "[ %num% ]"); tok2 = tok2->tokAt(3)) {
const MathLib::bigint index = MathLib::toLongNumber(tok2->strAt(4));
indexes.push_back(index);
}
if (indexes.empty() && arrayInfo.num().size() == 1U && Token::simpleMatch(tok2, "[") && tok2->astOperand2()) {
const ValueFlow::Value *value = tok2->astOperand2()->getMaxValue(false);
if (value) {
indexes.push_back(value->intvalue);
}
}
if (indexes.size() == arrayInfo.num().size()) {
// Check if the indexes point outside the whole array..
// char a[10][10];
// a[0][20] <-- ok.
// a[9][20] <-- error.
// total number of elements of array..
MathLib::bigint totalElements = 1;
// total index..
MathLib::bigint totalIndex = 0;
// calculate the totalElements and totalIndex..
for (std::size_t i = 0; i < indexes.size(); ++i) {
const std::size_t ri = indexes.size() - 1 - i;
totalIndex += indexes[ri] * totalElements;
totalElements *= arrayInfo.num(ri);
if (arrayInfo.num(ri) == -1) {
// unknown size
totalElements = 0;
break;
}
}
// totalElements == 0 => Unknown size
if (totalElements == 0)
continue;
const Token *tok3 = tok->previous();
while (tok3 && Token::Match(tok3->previous(), "%name% ."))
tok3 = tok3->tokAt(-2);
// taking address of 1 past end?
if (totalIndex == totalElements) {
const bool addr = (tok3 && (tok3->str() == "&" ||
Token::simpleMatch(tok3->previous(), "& (")));
if (addr)
continue;
}
// Is totalIndex in bounds?
if (totalIndex > totalElements || totalIndex < 0) {
arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes);
}
// Is any array index out of bounds?
else {
// check each index for overflow
for (std::size_t i = 0; i < indexes.size(); ++i) {
if (indexes[i] >= arrayInfo.num(i)) {
if (indexes.size() == 1U) {
arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes);
break; // only warn about the first one
}
// The access is still within the memory range for the array
// so it may be intentional.
else if (printInconclusive) {
arrayIndexOutOfBoundsError(tok->tokAt(1 + varcount), arrayInfo, indexes);
break; // only warn about the first one
}
}
}
}
}
tok = tok2;
continue;
}
// memset, memcmp, memcpy, strncpy, fgets..
if (declarationId == 0 && size > 0 && Token::Match(tok, "%name% ( !!)")) {
std::list<const Token *> callstack;
callstack.push_back(tok);
const Token* tok2 = tok->tokAt(2);
if (Token::Match(tok2, (varnames + " ,").c_str()))
checkFunctionParameter(*tok, 1, arrayInfo, callstack);
tok2 = tok2->nextArgument();
if (Token::Match(tok2, (varnames + " ,").c_str()))
checkFunctionParameter(*tok, 2, arrayInfo, callstack);
}
if (total_size > 0) {
// Writing data into array..
if ((declarationId > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str%|%var% )", declarationId)) ||
(declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str%|%var% )").c_str()))) {
const Token* lastParamTok = tok->tokAt(varcount + 4);
if (lastParamTok->tokType() == Token::Type::eString) {
const std::size_t len = Token::getStrLength(lastParamTok);
if (len >= total_size) {
bufferOverrunError(tok, declarationId > 0 ? emptyString : varnames);
continue;
}
} else {
const Variable *var = lastParamTok->variable();
if (var && var->isArray() && var->dimensions().size() == 1) {
const MathLib::bigint len = var->dimension(0);
if (len > total_size) {
if (printInconclusive)
possibleBufferOverrunError(tok, tok->strAt(4), tok->strAt(2), tok->str() == "strcat");
continue;
}
}
}
}
// Detect few strcat() calls
const std::string strcatPattern = declarationId > 0 ? std::string("strcat ( %varid% , %str% ) ;") : ("strcat ( " + varnames + " , %str% ) ;");
if (Token::Match(tok, strcatPattern.c_str(), declarationId)) {
std::size_t charactersAppend = 0;
const Token *tok2 = tok;
do {
charactersAppend += Token::getStrLength(tok2->tokAt(4 + varcount));
if (charactersAppend >= static_cast<std::size_t>(total_size)) {
bufferOverrunError(tok2);
break;
}
tok2 = tok2->tokAt(7 + varcount);
} while (Token::Match(tok2, strcatPattern.c_str(), declarationId));
}
// Check function call..
if (Token::Match(tok, "%name% (")) {
// No varid => function calls are not handled
if (declarationId == 0)
continue;
const ArrayInfo arrayInfo1(declarationId, varnames, total_size / size, size);
const std::list<const Token *> callstack;
checkFunctionCall(tok, arrayInfo1, callstack);
}
}
// undefined behaviour: result of pointer arithmetic is out of bounds
if (declarationId && Token::Match(tok, "= %varid% + %num% ;", declarationId)) {
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3));
if (printPortability && index > size)
pointerOutOfBoundsError(tok->tokAt(2));
if (index >= size && Token::Match(tok->tokAt(-2), "[;{}] %varid% =", declarationId))
pointerIsOutOfBounds = true;
}
else if (pointerIsOutOfBounds && Token::Match(tok, "[;{}=] * %varid% [;=]", declarationId)) {
outOfBoundsError(tok->tokAt(2), tok->strAt(2), false, 0, 0);
}
}
}
static std::vector<ValueFlow::Value> valueFlowGetArrayIndexes(const Token * const tok, bool conditional, std::size_t dimensions)
{
unsigned int indexvarid = 0;
const std::vector<ValueFlow::Value> empty;
std::vector<ValueFlow::Value> indexes;
for (const Token *tok2 = tok; indexes.size() < dimensions && Token::simpleMatch(tok2, "["); tok2 = tok2->link()->next()) {
if (!tok2->astOperand2())
return empty;
const ValueFlow::Value *index = tok2->astOperand2()->getMaxValue(conditional);
if (!index)
return empty;
if (indexvarid == 0U)
indexvarid = index->varId;
if (index->varId > 0 && indexvarid != index->varId)
return empty;
if (index->intvalue < 0)
return empty;
indexes.push_back(*index);
}
return indexes;
}
void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const ArrayInfo &arrayInfo)
{
// Declaration in global scope or namespace?
if (tok->scope()->type == Scope::eGlobal || tok->scope()->type == Scope::eNamespace)
return;
/*
{
const Token *parent = tok->astParent();
while (Token::Match(parent, "%name%|::|*|&"))
parent = parent->astParent();
if (parent && !Token::simpleMatch(parent, "="))
return;
}
*/
const bool printInconclusive = _settings->inconclusive;
// Taking address?
const bool addressOf = isAddressOf(tok);
// Look for errors first
for (int warn = 0; warn == 0 || warn == 1; ++warn) {
// Negative index..
for (const Token *tok2 = tok; tok2 && tok2->str() == "["; tok2 = tok2->link()->next()) {
const Token *index = tok2->astOperand2();
if (!index)
continue;
const ValueFlow::Value *value = index->getValueLE(-1LL,_settings);
if (value)
negativeIndexError(index, *value);
}
// Index out of bounds..
const std::vector<ValueFlow::Value> indexes(valueFlowGetArrayIndexes(tok, warn==1, arrayInfo.num().size()));
if (indexes.size() != arrayInfo.num().size())
continue;
// Check if the indexes point outside the whole array..
// char a[10][10];
// a[0][20] <-- ok.
// a[9][20] <-- error.
// total number of elements of array..
const MathLib::bigint totalElements = arrayInfo.numberOfElements();
// total index..
const MathLib::bigint totalIndex = arrayInfo.totalIndex(indexes);
// totalElements <= 0 => Unknown size
if (totalElements <= 0)
continue;
if (addressOf && totalIndex == totalElements)
continue;
// Is totalIndex in bounds?
if (totalIndex >= totalElements) {
arrayIndexOutOfBoundsError(tok, arrayInfo, indexes);
break;
}
// Is any array index out of bounds?
if (printInconclusive) {
// check each index for overflow
for (std::size_t i = 0; i < indexes.size(); ++i) {
if (indexes[i].intvalue >= arrayInfo.num(i)) {
// The access is still within the memory range for the array
// so it may be intentional.
arrayIndexOutOfBoundsError(tok, arrayInfo, indexes);
break; // only warn about the first one
}
}
}
}
}
void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo)
{
bool reassigned = false;
for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) {
if (reassigned && tok->str() == ";")
break;
if (tok->varId() != arrayInfo.declarationId())
continue;
if (tok->strAt(1) == "=") {
reassigned = true;
}
checkScope_inner(tok, arrayInfo);
}
}
void CheckBufferOverrun::checkScope(const Token *tok, std::map<unsigned int, ArrayInfo> arrayInfos)
{
unsigned int reassigned = 0;
for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) {
if (reassigned && tok->str() == ";") {
arrayInfos.erase(reassigned);
reassigned = 0;
}
if (!tok->variable() || tok->variable()->nameToken() == tok)
continue;
std::map<unsigned int, ArrayInfo>::const_iterator arrayInfo = arrayInfos.find(tok->varId());
if (arrayInfo == arrayInfos.cend())
continue;
if (tok->strAt(1) == "=") {
reassigned = tok->varId();
}
checkScope_inner(tok, arrayInfo->second);
}
}
void CheckBufferOverrun::checkScope_inner(const Token *tok, const ArrayInfo &arrayInfo)
{
const bool printPortability = _settings->isEnabled(Settings::PORTABILITY);
const bool printWarning = _settings->isEnabled(Settings::WARNING);
const bool printInconclusive = _settings->inconclusive;
if (tok->strAt(1) == "[") {
valueFlowCheckArrayIndex(tok->next(), arrayInfo);
}
else if (printPortability && !tok->isCast() && tok->astParent() && tok->astParent()->str() == "+") {
// undefined behaviour: result of pointer arithmetic is out of bounds
const Token *index;
if (tok == tok->astParent()->astOperand1())
index = tok->astParent()->astOperand2();
else
index = tok->astParent()->astOperand1();
if (index) {
const ValueFlow::Value *value = index->getValueGE(arrayInfo.num(0) + 1U, _settings);
if (!value)
value = index->getValueLE(-1, _settings);
if (value)
pointerOutOfBoundsError(tok->astParent(), index, value->intvalue);
}
}
else if (printPortability && tok->astParent() && tok->astParent()->str() == "-") {
const Variable *var = symbolDatabase->getVariableFromVarId(arrayInfo.declarationId());
if (var && var->isArray()) {
const Token *index = tok->astParent()->astOperand2();
const ValueFlow::Value *value = index ? index->getValueGE(1,_settings) : nullptr;
if (index && !value)
value = index->getValueLE(-1 - arrayInfo.num(0), _settings);
if (value)
pointerOutOfBoundsError(tok->astParent(), index, value->intvalue);
}
}
if (!tok->scope()->isExecutable()) // No executable code outside of executable scope - continue to increase performance
return;
const Token* tok2 = tok->astParent();
if (tok2) {
while (tok2->astParent() && !Token::Match(tok2->astParent(), "[,(]"))
tok2 = tok2->astParent();
while (tok2->astParent() && tok2->astParent()->str() == ",")
tok2 = tok2->astParent();
if (tok2->astParent() && tok2->astParent()->str() == "(")
tok2 = tok2->astParent();
if (tok2->str() != "(")
return;
tok2 = tok2->previous();
// Check function call..
checkFunctionCall(tok2, arrayInfo, std::list<const Token*>());
const MathLib::biguint total_size = arrayInfo.num(0) * arrayInfo.element_size();
if (printWarning && printInconclusive && Token::Match(tok2, "strncpy|memcpy|memmove ( %varid% , %str% , %num% )", arrayInfo.declarationId())) {
if (Token::getStrLength(tok2->tokAt(4)) >= total_size) {
const MathLib::biguint num = MathLib::toULongNumber(tok2->strAt(6));
if (total_size == num)
bufferNotZeroTerminatedError(tok2, tok2->strAt(2), tok2->str());
}
}
if (printWarning && Token::Match(tok2, "strncpy|strncat ( %varid% ,", arrayInfo.declarationId()) && Token::Match(tok2->linkAt(1)->tokAt(-2), ", %num% )")) {
const Token* param3 = tok2->linkAt(1)->previous();
// check for strncpy which is not terminated
if (tok2->str() == "strncpy") {
// strncpy takes entire variable length as input size
const MathLib::biguint num = MathLib::toULongNumber(param3->str());
// this is currently 'inconclusive'. See TestBufferOverrun::terminateStrncpy3
if (printInconclusive && num >= total_size) {
const Token *tok4 = tok2->next()->link()->next();
for (; tok4; tok4 = tok4->next()) {
const Token* tok3 = tok2->tokAt(2);
if (tok4->varId() == tok3->varId()) {
const Token *eq = nullptr;
if (Token::Match(tok4, "%varid% [", tok3->varId()) && Token::simpleMatch(tok4->linkAt(1), "] ="))
eq = tok4->linkAt(1)->next();
const Token *rhs = eq ? eq->astOperand2() : nullptr;
if (!(rhs && rhs->hasKnownIntValue() && rhs->getValue(0)))
terminateStrncpyError(tok2, tok3->str());
break;
}
}
}
}
// Dangerous usage of strncat..