forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd.c
5091 lines (4379 loc) · 109 KB
/
std.c
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
// Test library configuration for std.cfg
//
// Usage:
// $ cppcheck --check-library --library=std --enable=style,information --inconclusive --error-exitcode=1 --inline-suppr test/cfg/std.c
// =>
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
//
// cppcheck-suppress-file valueFlowBailout
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <tgmath.h> // frexp
#include <wchar.h>
#if defined(__STD_UTF_16__) || defined(__STD_UTF_32__)
#include <uchar.h>
#endif
#include <ctype.h>
#include <wctype.h>
#include <fenv.h>
#include <setjmp.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
#include <stdbool.h>
#include <stdint.h>
#ifndef __STDC_NO_THREADS__
#include <threads.h>
#endif
#include <inttypes.h>
#include <float.h>
#include <stdarg.h>
#include <sys/types.h>
#include <assert.h>
#include <alloca.h>
#include <locale.h>
#include <signal.h>
size_t invalidFunctionArgStr_wcslen(void)
{
const wchar_t terminated0[] = L"ABCDEF49620910";
const wchar_t terminated1[3] = { L'a', L'b', L'\0' };
const wchar_t notTerminated[3] = { L'a', L'b', L'c' };
// cppcheck-suppress invalidFunctionArgStr
(void) wcslen(notTerminated);
(void) wcslen(terminated0);
return wcslen(terminated1);
}
int invalidFunctionArgStr_strcpn(void)
{
const char str1[] = "ABCDEF49620910";
const char str2[] = "42";
const char pattern[3] = { -42, -43, -44 };
// cppcheck-suppress invalidFunctionArgStr
(void) strcspn(str1, pattern);
return strcspn(str1, str2);
}
void invalidFunctionArgStr_strncat(void)
{
char str1[20];
strcpy (str1,"test");
const char src = '/';
// No warning is expected for
strncat (str1, &src, 1);
puts (str1);
}
char * invalidFunctionArgStr_strpbrk( const char *p )
{
const char search[] = { -42, -43, -44 };
const char pattern[3] = { -42, -43, -44 };
(void) strpbrk( "abc42", "42" );
// cppcheck-suppress invalidFunctionArgStr
(void) strpbrk( search, "42" );
// cppcheck-suppress invalidFunctionArgStr
(void) strpbrk( search, pattern );
// cppcheck-suppress invalidFunctionArgStr
return strpbrk( p, pattern );
}
int invalidFunctionArgStr_strncmp( const char *p )
{
const char string[] = "foo";
char other[5] = { 0 };
memcpy(other, "foo", 4);
if (strncmp(other, string, 5) != 0) {}
// No warning is expected for:
const char emdash[3] = { -42, -43, -44 };
return strncmp( p, emdash, 3 );
}
float invalidFunctionArg_float_remquo (float x, float y, int* quo )
{
// cppcheck-suppress invalidFunctionArg
(void) remquo(x,0.0f,quo);
// cppcheck-suppress invalidFunctionArg
(void) remquof(x,0.0f,quo);
return remquo(x,y,quo);
}
double invalidFunctionArg_double_remquo (double x, double y, int* quo )
{
// cppcheck-suppress invalidFunctionArg
(void) remquo(x,0.0,quo);
// cppcheck-suppress invalidFunctionArg
(void) remquo(x,0.0f,quo);
// cppcheck-suppress invalidFunctionArg
(void) remquo(x,0.0L,quo);
return remquo(x,y,quo);
}
double invalidFunctionArg_long_double_remquo (long double x, long double y, int* quo )
{
// cppcheck-suppress invalidFunctionArg
(void) remquo(x,0.0L,quo);
// cppcheck-suppress invalidFunctionArg
(void) remquol(x,0.0L,quo);
return remquo(x,y,quo);
}
void invalidFunctionArg_remainderl(long double f1, long double f2)
{
// cppcheck-suppress invalidFunctionArg
(void)remainderl(f1,0.0);
// cppcheck-suppress invalidFunctionArg
(void)remainderl(f1,0.0L);
(void)remainderl(f1,f2);
}
void invalidFunctionArg_remainder(double f1, double f2)
{
// cppcheck-suppress invalidFunctionArg
(void)remainder(f1,0.0);
(void)remainder(f1,f2);
}
void invalidFunctionArg_remainderf(float f1, float f2)
{
// cppcheck-suppress invalidFunctionArg
(void)remainderf(f1,0.0);
// cppcheck-suppress invalidFunctionArg
(void)remainderf(f1,0.0f);
(void)remainderf(f1,f2);
}
int qsort_cmpfunc (const void * a, const void * b) {
return (*(int*)a - *(int*)b);
}
void nullPointer_qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
{
// cppcheck-suppress nullPointer
qsort(NULL, n, size, qsort_cmpfunc);
// cppcheck-suppress nullPointer
qsort(base, n, size, NULL);
qsort(base, n, size, qsort_cmpfunc);
}
// 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.
#ifdef __STDC_LIB_EXT1__
void uninitvar_localtime_s(const time_t *restrict time, struct tm *restrict result)
{
const time_t *restrict Time;
// cppcheck-suppress uninitvar
(void)localtime_s(Time, result);
(void)localtime_s(time, result);
}
void nullPointer_localtime_s(const time_t *restrict time, struct tm *restrict result)
{
// cppcheck-suppress nullPointer
(void)localtime_s(NULL, result);
// cppcheck-suppress nullPointer
(void)localtime_s(time, NULL);
(void)localtime_s(time, result);
}
#endif // __STDC_LIB_EXT1__
size_t bufferAccessOutOfBounds_wcsrtombs(char * dest, const wchar_t ** src, size_t len, mbstate_t * ps)
{
char buf[42];
(void)wcsrtombs(buf,src,42,ps);
// cppcheck-suppress bufferAccessOutOfBounds
(void)wcsrtombs(buf,src,43,ps);
return wcsrtombs(dest,src,len,ps);
}
void bufferAccessOutOfBounds(void)
{
char a[5];
// cppcheck-suppress valueFlowBailoutIncompleteVar
fgets(a,5,stdin);
// cppcheck-suppress bufferAccessOutOfBounds
fgets(a,6,stdin);
sprintf(a, "ab%s", "cd");
// cppcheck-suppress bufferAccessOutOfBounds
// TODO cppcheck-suppress redundantCopy
sprintf(a, "ab%s", "cde");
// TODO cppcheck-suppress redundantCopy
snprintf(a, 5, "abcde%i", 1);
// TODO cppcheck-suppress redundantCopy
// cppcheck-suppress bufferAccessOutOfBounds
snprintf(a, 6, "abcde%i", 1);
// TODO cppcheck-suppress redundantCopy
strcpy(a,"abcd");
// cppcheck-suppress bufferAccessOutOfBounds
// TODO cppcheck-suppress redundantCopy
strcpy(a, "abcde");
#ifdef __STDC_LIB_EXT1__
// cppcheck-suppress bufferAccessOutOfBounds
strcpy_s(a, 10, "abcdefghij");
#endif
// TODO cppcheck-suppress redundantCopy
// cppcheck-suppress terminateStrncpy
strncpy(a,"abcde",5);
// cppcheck-suppress bufferAccessOutOfBounds
// TODO cppcheck-suppress redundantCopy
strncpy(a,"abcde",6);
// cppcheck-suppress bufferAccessOutOfBounds
// TODO cppcheck-suppress redundantCopy
strncpy(a,"a",6);
// TODO cppcheck-suppress redundantCopy
strncpy(a,"abcdefgh",4);
#ifdef __STDC_LIB_EXT1__
// valid call
strncpy_s(a,5,"abcd",5);
// string will be truncated, error is returned, but no buffer overflow
strncpy_s(a,5,"abcde",6);
// TODO cppcheck-suppress bufferAccessOutOfBounds
strncpy_s(a,5,"a",6);
strncpy_s(a,5,"abcdefgh",4);
// valid call
strncat_s(a,5,"1",2);
// cppcheck-suppress bufferAccessOutOfBounds
strncat_s(a,10,"1",2);
// TODO cppcheck-suppress bufferAccessOutOfBounds
strncat_s(a,5,"1",5);
#endif
fread(a,1,5,stdin);
// cppcheck-suppress bufferAccessOutOfBounds
fread(a,1,6,stdin);
fwrite(a,1,5,stdout);
// cppcheck-suppress bufferAccessOutOfBounds
fread(a,1,6,stdout);
char * pAlloc1 = aligned_alloc(8, 16);
// cppcheck-suppress nullPointerOutOfMemory
memset(pAlloc1, 0, 16);
// cppcheck-suppress bufferAccessOutOfBounds
// cppcheck-suppress nullPointerOutOfMemory
memset(pAlloc1, 0, 17);
free(pAlloc1);
}
wchar_t* nullPointer_fgetws(wchar_t* buffer, int n, FILE* stream)
{
// cppcheck-suppress nullPointer
(void)fgetws(NULL,n,stream);
// cppcheck-suppress nullPointer
(void)fgetws(buffer,n,NULL);
// No warning is expected
return fgetws(buffer, n, stream);
}
char* nullPointer_fgets(char *buffer, int n, FILE *stream)
{
// cppcheck-suppress nullPointer
(void)fgets(NULL,n,stream);
// cppcheck-suppress nullPointer
(void)fgets(buffer,n,NULL);
// No warning is expected
return fgets(buffer, n, stream);
}
void memleak_aligned_alloc(void)
{
// cppcheck-suppress [unusedAllocatedMemory, unreadVariable, constVariablePointer]
char * alignedBuf = aligned_alloc(8, 16);
// cppcheck-suppress memleak
}
void pointerLessThanZero_aligned_alloc(void)
{
char * alignedBuf = aligned_alloc(8, 16);
// cppcheck-suppress pointerLessThanZero
if (alignedBuf < 0) return;
free(alignedBuf);
// no warning is expected for
alignedBuf = aligned_alloc(8, 16);
if (alignedBuf == 0) return;
free(alignedBuf);
// no warning is expected for
alignedBuf = aligned_alloc(8, 16);
if (alignedBuf) free(alignedBuf);
}
void unusedRetVal_aligned_alloc(void)
{
// cppcheck-suppress leakReturnValNotUsed
aligned_alloc(8, 16);
}
void uninitvar_aligned_alloc(size_t alignment, size_t size)
{
size_t uninitVar1, uninitVar2, uninitVar3;
// cppcheck-suppress uninitvar
free(aligned_alloc(uninitVar1, size));
// cppcheck-suppress uninitvar
free(aligned_alloc(alignment, uninitVar2));
// cppcheck-suppress uninitvar
free(aligned_alloc(uninitVar3, uninitVar3));
// no warning is expected
free(aligned_alloc(alignment, size));
}
void bufferAccessOutOfBounds_libraryDirectionConfiguration(void)
{
// This tests whether the argument to isdigit() is configured with direction "in". This allows
// Cppcheck to report the error without marking it as inconclusive.
char arr[10];
char c = 'A';
(void)isdigit(c);
// cppcheck-suppress arrayIndexOutOfBounds
// cppcheck-suppress unreadVariable
arr[c] = 'x';
}
void internalError_libraryDirectionConfiguration(char* str) { // #12824
const char* s = str;
char* end = str;
if (1) {
// cppcheck-suppress unreadVariable
unsigned long val = strtoul(&s[1], &end, 10);
}
}
void arrayIndexOutOfBounds()
{
char * pAlloc1 = aligned_alloc(8, 16);
// cppcheck-suppress nullPointerOutOfMemory
pAlloc1[15] = '\0';
// cppcheck-suppress arrayIndexOutOfBounds
// cppcheck-suppress nullPointerOutOfMemory
pAlloc1[16] = '1';
free(pAlloc1);
char * pAlloc2 = malloc(9);
// cppcheck-suppress nullPointerOutOfMemory
pAlloc2[8] = 'a';
// cppcheck-suppress arrayIndexOutOfBounds
// cppcheck-suppress nullPointerOutOfMemory
pAlloc2[9] = 'a';
// #1379
// cppcheck-suppress memleakOnRealloc
pAlloc2 = realloc(pAlloc2, 8);
pAlloc2[7] = 'b';
// cppcheck-suppress arrayIndexOutOfBounds
pAlloc2[8] = 0;
// cppcheck-suppress memleakOnRealloc
pAlloc2 = realloc(pAlloc2, 20);
pAlloc2[19] = 'b';
// cppcheck-suppress arrayIndexOutOfBounds
pAlloc2[20] = 0;
free(pAlloc2);
char * pAlloc3 = calloc(2,3);
// cppcheck-suppress nullPointerOutOfMemory
pAlloc3[5] = 'a';
// cppcheck-suppress arrayIndexOutOfBounds
// cppcheck-suppress nullPointerOutOfMemory
pAlloc3[6] = 1;
free(pAlloc3);
}
void resourceLeak_tmpfile(void)
{
// cppcheck-suppress [unreadVariable, constVariablePointer]
FILE * fp = tmpfile();
// cppcheck-suppress resourceLeak
}
// memory leak
void ignoreleak(void)
{
char *p = (char *)malloc(10);
memset(&(p[0]), 0, 10);
// cppcheck-suppress memleak
}
// null pointer
void nullpointer(int value)
{
int res = 0;
FILE *fp;
wchar_t *pWcsUninit;
#ifndef __CYGWIN__
// cppcheck-suppress nullPointer
clearerr(0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
feof(0);
#endif
// cppcheck-suppress nullPointer
(void)fgetc(0);
// cppcheck-suppress nullPointer
fclose(0);
#ifndef __CYGWIN__
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
ferror(0);
#endif
// cppcheck-suppress nullPointer
(void)ftell(0);
// cppcheck-suppress nullPointer
puts(0);
// cppcheck-suppress nullPointer
fp=fopen(0,0);
// cppcheck-suppress nullPointerOutOfResources
fclose(fp);
fp = 0;
// No FP
fflush(0); // If stream is a null pointer, all streams are flushed.
// cppcheck-suppress valueFlowBailoutIncompleteVar
fp = freopen(0,"abc",stdin);
fclose(fp);
fp = NULL;
// cppcheck-suppress nullPointer
fputc(0,0);
// cppcheck-suppress nullPointer
fputs(0,0);
// cppcheck-suppress nullPointer
fgetpos(0,0);
// cppcheck-suppress nullPointer
frexp(1.0,0);
// cppcheck-suppress nullPointer
fsetpos(0,0);
#ifdef _WIN32 // not available on non-Windows compilers
// cppcheck-suppress nullPointer
itoa(123,0,10);
#endif
putchar(0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strchr(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcschr(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strlen(0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcslen(0);
// cppcheck-suppress nullPointer
strcpy(0,0);
// cppcheck-suppress nullPointer
wcscpy(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strspn(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcsspn(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strcspn(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcscspn(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strcoll(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcscoll(0,0);
// cppcheck-suppress nullPointer
strcat(0,0);
// cppcheck-suppress nullPointer
wcscat(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strcmp(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcscmp(0,0);
#ifdef __STDC_LIB_EXT1__
// cppcheck-suppress nullPointer
strcpy_s(0,1,1);
// cppcheck-suppress nullPointer
strcpy_s(1,1,0);
#endif
// cppcheck-suppress nullPointer
strncpy(0,0,1);
#ifdef __STDC_LIB_EXT1__
// cppcheck-suppress nullPointer
strncpy_s(0,1,1,1);
// cppcheck-suppress nullPointer
strncpy_s(1,1,0,1);
#endif
// cppcheck-suppress nullPointer
wcsncpy(0,0,1);
// cppcheck-suppress nullPointer
strncat(0,0,1);
#ifdef __STDC_LIB_EXT1__
// cppcheck-suppress nullPointer
strncat_s(0,1,1,1);
// cppcheck-suppress nullPointer
strncat_s(1,1,0,1);
#endif
// cppcheck-suppress nullPointer
wcsncat(0,0,1);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strncmp(0,0,1);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcsncmp(0,0,1);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
strstr(0,0);
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress nullPointer
wcsstr(0,0);
// cppcheck-suppress nullPointer
strtoul(0,0,0);
// cppcheck-suppress nullPointer
wcstoul(0,0,0);
// cppcheck-suppress nullPointer
strtoull(0,0,0);
// cppcheck-suppress nullPointer
wcstoull(0,0,0);
// cppcheck-suppress nullPointer
strtol(0,0,0);
// cppcheck-suppress nullPointer
wcstol(0,0,0);
// #6100 False positive nullPointer - calling mbstowcs(NULL,)
res += mbstowcs(0,"",0);
res += wcstombs(0,L"",0);
strtok(NULL,"xyz");
wcstok(NULL,L"xyz",&pWcsUninit);
strxfrm(0,"foo",0);
// TODO: error message (#6306 and http://trac.cppcheck.net/changeset/d11eb4931aea51cf2cb74faccdcd2a3289b818d6/)
strxfrm(0,"foo",42);
wcsxfrm(0,L"foo",0);
// TODO: error message when arg1==NULL and arg3!=0 #6306: https://trac.cppcheck.net/ticket/6306#comment:2
wcsxfrm(0,L"foo",42);
snprintf(NULL, 0, "someformatstring"); // legal
// cppcheck-suppress nullPointer
snprintf(NULL, 42, "someformatstring"); // not legal
scanf("%i", &res);
// cppcheck-suppress nullPointer
scanf("%i", NULL);
wscanf(L"%i", &res);
// cppcheck-suppress nullPointer
wscanf(L"%i", NULL);
}
void nullPointer_wcsftime(wchar_t* ptr, size_t maxsize, const wchar_t* format, const struct tm* timeptr)
{
// cppcheck-suppress nullPointer
(void)wcsftime(NULL, maxsize, format, timeptr);
// cppcheck-suppress nullPointer
(void)wcsftime(ptr, maxsize, NULL, timeptr);
// cppcheck-suppress nullPointer
(void)wcsftime(ptr, maxsize, format, NULL);
(void)wcsftime(ptr, maxsize, format, timeptr);
}
void bufferAccessOutOfBounds_wcsftime(wchar_t* ptr, size_t maxsize, const wchar_t* format, const struct tm* timeptr)
{
wchar_t buf[42];
(void)wcsftime(buf, 42, format, timeptr);
// TODO cppcheck-suppress bufferAccessOutOfBounds
(void)wcsftime(buf, 43, format, timeptr);
(void)wcsftime(ptr, maxsize, format, timeptr);
}
void bufferAccessOutOfBounds_wcsncpy()
{
wchar_t s[16];
wcsncpy(s, L"abc", 16);
// cppcheck-suppress bufferAccessOutOfBounds
wcsncpy(s, L"abc", 17);
}
int nullPointer_wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n)
{
// cppcheck-suppress nullPointer
(void) wcsncmp(NULL,s2,n);
// cppcheck-suppress nullPointer
(void) wcsncmp(s1,NULL,n);
return wcsncmp(s1,s2,n);
}
wchar_t* nullPointer_wcsncpy(wchar_t *s, const wchar_t *cs, size_t n)
{
// cppcheck-suppress nullPointer
(void) wcsncpy(NULL,cs,n);
// cppcheck-suppress nullPointer
(void) wcsncpy(s,NULL,n);
return wcsncpy(s,cs,n);
}
size_t nullPointer_strlen(const char *s)
{
// cppcheck-suppress nullPointer
(void) strlen(NULL);
return strlen(s);
}
void nullpointerMemchr1(char *p, const char *s)
{
p = memchr(s, 'p', strlen(s));
(void)p;
}
void nullpointerMemchr2(char *p, const char *s)
{
p = memchr(s, 0, strlen(s));
(void)p;
}
void nullPointer_memchr(char *p)
{
const char *s = 0;
// cppcheck-suppress nullPointer
p = memchr(s, 0, strlen(s));
(void)p;
}
void nullPointer_vsnprintf(const char * format, ...)
{
va_list args;
// valid
char buffer[256];
va_start(args, format);
vsnprintf(buffer, 256, format, args);
printf("%s", buffer);
va_end(args);
// valid
va_start(args, format);
vsnprintf(NULL, 0, format, args);
va_end(args);
// invalid
va_start(args, format);
// TODO #9410 cppcheck-suppress nullPointer
vsnprintf(NULL, 10, format, args);
va_end(args);
}
// uninit pointers
void uninitvar_abs(void)
{
int i;
// cppcheck-suppress uninitvar
(void)abs(i);
}
void uninitvar_clearerr(void)
{
FILE *fp;
// cppcheck-suppress uninitvar
clearerr(fp);
}
void uninitvar_fclose(void)
{
// cppcheck-suppress unassignedVariable
FILE *fp;
// cppcheck-suppress uninitvar
fclose(fp);
}
void uninitvar_fopen(void)
{
const char *filename, *mode;
FILE *fp;
// cppcheck-suppress uninitvar
fp = fopen(filename, "rt");
// cppcheck-suppress nullPointerOutOfResources
fclose(fp);
// cppcheck-suppress uninitvar
fp = fopen("filename.txt", mode);
// cppcheck-suppress nullPointerOutOfResources
fclose(fp);
}
void uninitvar_feof(void)
{
FILE *fp1, *fp2;
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress uninitvar
feof(fp1);
// cppcheck-suppress uninitvar
(void)feof(fp2);
}
void uninitvar_ferror(void)
{
FILE *fp1, *fp2;
// cppcheck-suppress ignoredReturnValue
// cppcheck-suppress uninitvar
ferror(fp1);
// cppcheck-suppress uninitvar
(void)ferror(fp2);
}
void uninitvar_fflush(void)
{
FILE *fp;
// cppcheck-suppress uninitvar
fflush(fp);
}
void uninitvar_fgetc(void)
{
FILE *fp;
// cppcheck-suppress uninitvar
(void)fgetc(fp);
}
void uninitvar_fgetpos(void)
{
FILE *fp;
fpos_t pos;
fpos_t *ppos;
// cppcheck-suppress uninitvar
fgetpos(fp,&pos);
fp = fopen("filename","rt");
// cppcheck-suppress uninitvar
// cppcheck-suppress nullPointerOutOfResources
fgetpos(fp,ppos);
// cppcheck-suppress nullPointerOutOfResources
fclose(fp);
}
void uninitvar_fsetpos(void)
{
FILE *fp;
fpos_t pos;
const fpos_t *ppos;
// cppcheck-suppress uninitvar
fsetpos(fp,&pos);
fp = fopen("filename","rt");
// cppcheck-suppress uninitvar
// cppcheck-suppress nullPointerOutOfResources
fsetpos(fp,ppos);
// cppcheck-suppress nullPointerOutOfResources
fclose(fp);
}
void uninitvar_fgets(void)
{
FILE *fp;
char buf[10];
char *str;
int n;
// cppcheck-suppress valueFlowBailoutIncompleteVar
fgets(buf,10,stdin);
// cppcheck-suppress uninitvar
fgets(str,10,stdin);
// cppcheck-suppress uninitvar
fgets(buf,10,fp);
// cppcheck-suppress uninitvar
fgets(buf,n,stdin);
}
void uninitvar_fputc(void)
{
int i;
FILE *fp;
// cppcheck-suppress valueFlowBailoutIncompleteVar
fputc('a', stdout);
// cppcheck-suppress uninitvar
fputc(i, stdout);
// cppcheck-suppress uninitvar
fputc('a', fp);
}
void uninitvar_fputs(void)
{
const char *s;
FILE *fp;
// cppcheck-suppress valueFlowBailoutIncompleteVar
fputs("a", stdout);
// cppcheck-suppress uninitvar
fputs(s, stdout);
// cppcheck-suppress uninitvar
fputs("a", fp);
}
void uninitvar_ftell(void)
{
FILE *fp;
// cppcheck-suppress uninitvar
(void)ftell(fp);
}
void uninitvar_puts(void)
{
const char *s;
// cppcheck-suppress uninitvar
puts(s);
}
void uninitvar_putchar(void)
{
char c;
// cppcheck-suppress uninitvar
putchar(c);
}
void uninitvar_cproj(void) // #6939
{
float complex fc;
// cppcheck-suppress uninitvar
(void)cprojf(fc);
double complex dc;
// cppcheck-suppress uninitvar
(void)cproj(dc);
long double complex ldc;
// cppcheck-suppress uninitvar
(void)cprojl(ldc);
}
void uninitvar_creal(void)
{
float complex fc;
// cppcheck-suppress uninitvar
(void)crealf(fc);
double complex dc;
// cppcheck-suppress uninitvar
(void)creal(dc);
long double complex ldc;
// cppcheck-suppress uninitvar
(void)creall(ldc);
}
void uninitvar_acos(void)
{
float f;
// cppcheck-suppress uninitvar
(void)acosf(f);
double d;
// cppcheck-suppress uninitvar
(void)acos(d);
long double ld;
// cppcheck-suppress uninitvar
(void)acosl(ld);
}
void uninitvar_acosh(void)
{
float f;
// cppcheck-suppress uninitvar
(void)acoshf(f);
double d;
// cppcheck-suppress uninitvar
(void)acosh(d);
long double ld;
// cppcheck-suppress uninitvar
(void)acoshl(ld);
}
void invalidFunctionArg_acosh(void)
{
float f = .999f;
// cppcheck-suppress invalidFunctionArg
(void)acoshf(f);
f = 1.0f;
(void)acoshf(f);
double d = .999;
// cppcheck-suppress invalidFunctionArg
(void)acosh(d);
d = 1.0;
(void)acosh(d);
long double ld = .999L;
// cppcheck-suppress invalidFunctionArg
(void)acoshl(ld);
ld = 1.0;
(void)acoshl(ld);
}
void invalidFunctionArg_atanh(void)
{
float f = 1.00001f;
// cppcheck-suppress invalidFunctionArg
(void)atanhf(f);
f = 1.0f;
(void)atanhf(f);
f = -1.0f;
(void)atanhf(f);
f = -1.00001f;
// cppcheck-suppress invalidFunctionArg
(void)atanhf(f);
double d = 1.00001;
// cppcheck-suppress invalidFunctionArg
(void)atanh(d);
d = 1.0;
(void)atanh(d);
d = -1.0;
(void)atanh(d);
d = -1.00001;
// cppcheck-suppress invalidFunctionArg
(void)atanh(d);
long double ld = 1.00001L;
// cppcheck-suppress invalidFunctionArg
(void)atanhl(ld);
ld = 1.0L;
(void)atanhl(ld);
ld = -1.0L;
(void)atanhl(ld);
ld = -1.00001L;
// cppcheck-suppress invalidFunctionArg
(void)atanhl(ld);
}
void uninitvar_asctime(void)
{
const struct tm *tm;
// cppcheck-suppress uninitvar
// cppcheck-suppress asctimeCalled
(void)asctime(tm);
}
#ifdef __STDC_LIB_EXT1__
void uninitvar_asctime_s(void)
{
const struct tm *tm;
char buf[26];
// cppcheck-suppress uninitvar
// cppcheck-suppress asctime_sCalled
asctime_s(buf, sizeof(buf), tm);
}
#endif
void uninitvar_assert(void)
{
int i;
// cppcheck-suppress checkLibraryNoReturn
// cppcheck-suppress uninitvar
assert(i);
}
void uninitvar_sqrt(void)
{
float f;
// cppcheck-suppress uninitvar
(void)sqrtf(f);
double d;
// cppcheck-suppress uninitvar
(void)sqrt(d);
long double ld;
// cppcheck-suppress uninitvar
(void)sqrtl(ld);
}