Skip to content

Commit 4c4cf49

Browse files
committed
Add headers, Add defininitions to headers, remove cases without null terminator, remove file pointer cases
1. Add headers, Adding missing headers: For obvious reasons. 2. Remove cases without null terminator: Both clang and g++ do not permit strings to be allocated that are declared to be shorter than the actual initializing expression. Since this is a C++ rule, we rule them out. 3. File pointer manipulation functions (e.g. fgets): Not required by the rule.
1 parent 356bbf2 commit 4c4cf49

File tree

4 files changed

+19
-220
lines changed

4 files changed

+19
-220
lines changed

cpp/common/test/includes/standard-library/stdlib.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@ long double strtold(const char *str, char **endptr);
3636

3737
int rand(void);
3838

39-
#endif // _GHLIBCPP_STDLIB
39+
int mblen (const char *, size_t);
40+
int mbtowc (wchar_t *__restrict, const char *__restrict, size_t);
41+
int wctomb (char *, wchar_t);
42+
size_t mbstowcs (wchar_t *__restrict, const char *__restrict, size_t);
43+
size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
44+
45+
#endif // _GHLIBCPP_STDLIB

cpp/common/test/includes/standard-library/string.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ void *memcpy(void *dest, const void *src, size_t count);
4343
void *memset(void *dest, int ch, size_t count);
4444
void *memmove(void *dest, const void *src, size_t count);
4545
int memcmp(const void *lhs, const void *rhs, size_t count);
46+
void *memchr (const void *, int, size_t);
4647

47-
#endif // _GHLIBCPP_STRINGH
48+
#endif // _GHLIBCPP_STRINGH

cpp/common/test/includes/standard-library/wchar.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ double wcstod(const wchar_t *str, wchar_t **endptr);
1717
float wcstof(const wchar_t *str, wchar_t **endptr);
1818
long double wcstold(const wchar_t *str, wchar_t **endptr);
1919

20+
size_t wcsftime (wchar_t *__restrict, size_t, const wchar_t *__restrict, const struct tm *__restrict);
21+
size_t wcsxfrm (wchar_t *__restrict, const wchar_t *__restrict, size_t);
22+
2023
// Character classification and conversion types
2124
typedef struct {
2225
int __count;
@@ -26,4 +29,4 @@ typedef struct {
2629
} __value;
2730
} mbstate_t;
2831

29-
#endif // _GHLIBCPP_WCHAR
32+
#endif // _GHLIBCPP_WCHAR

cpp/misra/test/rules/RULE-8-7-1/test.cpp

Lines changed: 6 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cstdlib>
22
#include <cstring>
33
#include <ctime>
4+
#include <cwchar>
45

56
void stack_allocated_single_dimensional_pointer_arithmetic(int *array) {
67
/* 1. Pointer formed from performing arithmetic */
@@ -215,56 +216,34 @@ void test_strings_loop(void) {
215216

216217
void test_strings(int flow, int unk_size) {
217218
char ca5_good[5] = "test"; // ok
218-
char ca5_bad[5] = "test1"; // no null terminator
219219
char ca6_good[6] = "test1"; // ok
220-
char ca6_bad[6] = "test12"; // no null terminator
221220

222-
wchar_t wa5_good[5] = L"test"; // ok
223-
wchar_t wa5_bad[5] = L"test1"; // no null terminator
224-
wchar_t wa6_good[6] = L"test"; // ok
225-
wchar_t wa6_bad[6] = L"test12"; // no null terminator
221+
wchar_t wa5_good[5] = L"test"; // ok
222+
wchar_t wa6_good[6] = L"test"; // ok
226223

227224
// strchr
228225
strchr(ca5_good, 't'); // COMPLIANT
229-
strchr(ca5_bad, 't'); // NON_COMPLIANT
230226
strchr(ca5_good + 4, 't'); // COMPLIANT
231227
strchr(ca5_good + 5, 't'); // NON_COMPLIANT
232228

233229
if (flow) {
234230
// strcpy from literal
235231
strcpy(ca5_good, "test1"); // NON_COMPLIANT
236-
strcpy(ca5_bad, "test"); // COMPLIANT
237232
}
238233

239234
if (flow) {
240235
// strcpy to char buffer indirect
241236
strcpy(get_ca_5(), ca5_good); // COMPLIANT
242-
strcpy(get_ca_5(), ca5_bad); // NON_COMPLIANT
243237
strcpy(get_ca_5(), ca6_good); // NON_COMPLIANT
244238
}
245239

246240
// strcpy between string buffers (must be null-terminated)
247241
if (flow) {
248242
strcpy(ca5_good, ca6_good);
249243
} // NON_COMPLIANT
250-
if (flow) {
251-
strcpy(ca5_good, ca6_bad);
252-
} // NON_COMPLIANT
253-
if (flow) {
254-
strcpy(ca5_bad, ca6_good);
255-
} // NON_COMPLIANT
256-
if (flow) {
257-
strcpy(ca6_bad, ca5_good);
258-
} // COMPLIANT
259-
if (flow) {
260-
strcpy(ca6_bad, ca5_bad);
261-
} // NON_COMPLIANT
262244
if (flow) {
263245
strcpy(get_ca_5(), ca5_good);
264246
} // COMPLIANT
265-
if (flow) {
266-
strcpy(get_ca_5(), ca5_bad);
267-
} // NON_COMPLIANT
268247
if (flow) {
269248
strcpy(get_ca_5(), ca6_good);
270249
} // NON_COMPLIANT
@@ -279,24 +258,9 @@ void test_strings(int flow, int unk_size) {
279258
if (flow) {
280259
strncpy(ca5_good, ca6_good, 5);
281260
} // COMPLIANT
282-
if (flow) {
283-
strncpy(ca5_good, ca6_bad, 4);
284-
} // COMPLIANT
285261
if (flow) {
286262
strncpy(ca5_good, ca5_good, 5);
287263
} // COMPLIANT
288-
if (flow) {
289-
strncpy(ca5_bad, ca5_bad, 5);
290-
} // COMPLIANT
291-
if (flow) {
292-
strncpy(ca5_bad, ca5_good, 6);
293-
} // NON_COMPLIANT
294-
if (flow) {
295-
strncpy(ca6_bad, ca5_good, 5);
296-
} // COMPLIANT
297-
if (flow) {
298-
strncpy(ca6_bad, ca5_good, 6);
299-
} // COMPLIANT
300264
if (flow) {
301265
strncpy(ca5_good + 1, ca5_good + 2, 3);
302266
} // COMPLIANT
@@ -305,8 +269,8 @@ void test_strings(int flow, int unk_size) {
305269
} // COMPLIANT
306270

307271
// wrong allocation size
308-
char *p1 = malloc(strlen(ca5_good) + 1);
309-
char *p2 = malloc(strlen(ca5_good));
272+
char *p1 = (char *)malloc(strlen(ca5_good) + 1);
273+
char *p2 = (char *)malloc(strlen(ca5_good));
310274

311275
// memcpy with strings and strlen
312276
if (flow) {
@@ -349,74 +313,15 @@ void test_strings(int flow, int unk_size) {
349313
strcat(get_ca_5() + 1, "1234"); // NON_COMPLIANT
350314
}
351315

352-
// wcsncat
353-
if (flow) {
354-
wchar_t buf0[10]; // memset after first use
355-
wchar_t buf1[10]; // no memset
356-
wchar_t buf2[10]; // memset before first use
357-
wchar_t buf3[10] = {L'\0'};
358-
wchar_t buf4[10] = L"12345";
359-
360-
wcsncat(buf0, L" ",
361-
1); // NON_COMPLIANT[FALSE_NEGATIVE] - not null terminated at
362-
// initialization
363-
364-
memset(buf0, 0, sizeof(buf0)); // COMPLIANT
365-
memset(buf2, 0, sizeof(buf2)); // COMPLIANT
366-
367-
wcsncat(buf1, L" ", 1); // NON_COMPLIANT - not null terminated
368-
wcsncat(buf2, L" ", 1); // COMPLIANT
369-
wcsncat(buf3, L" ", 1); // COMPLIANT
370-
wcsncat(buf4, L"12345", 5); // NON_COMPLIANT[FALSE_NEGATIVE]
371-
372-
wcsncat(get_ca_5(), L"12345", 5); // NON_COMPLIANT
373-
wcsncat(get_ca_5(), L"1234", 4); // NON_COMPLIANT
374-
wcsncat(get_ca_5() + 1, L"1234", 4); // NON_COMPLIANT
375-
wcsncat(get_ca_5(), L"12", 2); // NON_COMPLIANT
376-
}
377-
378316
// strcmp
379317
if (flow) {
380-
strcmp(ca5_good, ca5_bad); // NON_COMPLIANT
381318
strcmp(ca5_good, ca5_good); // COMPLIANT
382-
strcmp(ca5_bad, ca5_good); // NON_COMPLIANT
383319
strcmp(ca5_good, ca6_good); // COMPLIANT
384320
strcmp(ca6_good, ca5_good); // COMPLIANT
385321
}
386-
387-
// strncmp
388-
if (flow) {
389-
strncmp(ca5_good, ca5_bad, 4); // COMPLIANT
390-
strncmp(ca5_good, ca5_bad, 5); // COMPLIANT
391-
strncmp(ca5_good, ca5_bad, 6); // NON_COMPLIANT
392-
}
393322
}
394323

395324
void test_wrong_buf_size(void) {
396-
397-
// fgets
398-
{
399-
char buf[128];
400-
fgets(buf, sizeof(buf), stdin); // COMPLIANT
401-
fgets(buf, sizeof(buf) - 1, stdin); // COMPLIANT
402-
fgets(buf, sizeof(buf) + 1, stdin); // NON_COMPLIANT
403-
fgets(buf, 0, stdin); // COMPLIANT
404-
fgets(buf + 1, sizeof(buf) - 1, stdin); // COMPLIANT
405-
fgets(buf + 1, sizeof(buf), stdin); // NON_COMPLIANT
406-
}
407-
408-
// fgetws
409-
{
410-
wchar_t wbuf[128];
411-
fgetws(wbuf, sizeof(wbuf), stdin); // NON_COMPLIANT
412-
fgetws(wbuf, sizeof(wbuf) / sizeof(*wbuf), stdin); // COMPLIANT
413-
fgetws(wbuf, sizeof(wbuf) / sizeof(*wbuf) - 1, stdin); // COMPLIANT
414-
fgetws(wbuf, sizeof(wbuf) / sizeof(*wbuf) + 1, stdin); // NON_COMPLIANT
415-
fgetws(wbuf, 0, stdin); // COMPLIANT
416-
fgetws(wbuf + 1, sizeof(wbuf) / sizeof(*wbuf) - 2, stdin); // COMPLIANT
417-
fgetws(wbuf + 1, sizeof(wbuf) / sizeof(*wbuf), stdin); // NON_COMPLIANT
418-
}
419-
420325
// mbstowcs
421326
{
422327
char buf1[128] = {0};
@@ -482,15 +387,12 @@ void test_wrong_buf_size(void) {
482387
// wcsftime
483388
{
484389
wchar_t wbuf[128] = {0};
485-
wchar_t format_bad[8] = L"%Y-%m-%d";
486390
wcsftime(wbuf, sizeof(wbuf) / sizeof(wchar_t), L"%Y-%m-%d",
487391
NULL); // COMPLIANT
488392
wcsftime(wbuf, sizeof(wbuf) / sizeof(wchar_t) + 2, L"%Y-%m-%d",
489393
NULL); // NON_COMPLIANT
490394
wcsftime(wbuf, sizeof(wbuf) / sizeof(wchar_t) - 2, L"%Y-%m-%d",
491395
NULL); // COMPLIANT
492-
wcsftime(wbuf, sizeof(wbuf) / sizeof(wchar_t), format_bad,
493-
NULL); // NON_COMPLIANT
494396
wcsftime(wbuf + 1, sizeof(wbuf) / sizeof(wchar_t), L"%Y-%m-%d",
495397
NULL); // NON_COMPLIANT
496398
wcsftime(wbuf, sizeof(wbuf), L"%Y-%m-%d", NULL); // NON_COMPLIANT
@@ -527,27 +429,6 @@ void test_wrong_buf_size(void) {
527429
wcsxfrm(wbuf + 1, wbuf2, sizeof(wbuf) / sizeof(wchar_t) - 1); // COMPLIANT
528430
}
529431

530-
// snprintf (and vsnprintf, swprintf, vswprintf)
531-
{
532-
char str_bad[2] = "12";
533-
char buf[64];
534-
snprintf(buf, sizeof(buf), "%s", ""); // COMPLIANT
535-
snprintf(buf, sizeof(buf), "%s",
536-
str_bad); // NON_COMPLIANT[FALSE_NEGATIVE] - not checked
537-
snprintf(buf, sizeof(buf) + 1, "test"); // NON_COMPLIANT
538-
}
539-
540-
// setvbuf
541-
{
542-
FILE *f;
543-
char buf[64];
544-
setvbuf(f, buf, _IOFBF, sizeof(buf)); // COMPLIANT
545-
setvbuf(f, buf, _IOFBF, sizeof(buf) + 1); // NON_COMPLIANT
546-
setvbuf(f, buf, _IOFBF, sizeof(buf) - 1); // COMPLIANT
547-
setvbuf(f, buf + 1, _IOFBF, sizeof(buf)); // NON_COMPLIANT
548-
setvbuf(f, NULL, _IOFBF, 0); // COMPLIANT - exception
549-
}
550-
551432
// "memcpy", "wmemcpy", "memmove", "wmemmove", "memcmp", "wmemcmp"
552433

553434
// memcpy
@@ -563,102 +444,10 @@ void test_wrong_buf_size(void) {
563444
memcpy(buf + 1, buf2, sizeof(buf)); // NON_COMPLIANT
564445
memcpy(buf, buf2 + 1, sizeof(buf) * 2); // NON_COMPLIANT
565446
}
566-
567-
// wmemcpy
568-
{
569-
wchar_t wbuf128[128];
570-
wchar_t wbuf64[64];
571-
572-
wmemcpy(wbuf128, wbuf64, sizeof(wbuf64) / sizeof(wchar_t)); // COMPLIANT
573-
wmemcpy(wbuf128, wbuf64,
574-
sizeof(wbuf128) / sizeof(wchar_t)); // NON_COMPLIANT
575-
wmemcpy(wbuf128, wbuf64, sizeof(wbuf64) / sizeof(wchar_t) - 1); // COMPLIANT
576-
wmemcpy(wbuf64 + 1, wbuf64,
577-
sizeof(wbuf64) / sizeof(wchar_t)); // NON_COMPLIANT
578-
wmemcpy(wbuf64 + 1, wbuf64 + 1,
579-
sizeof(wbuf64) / sizeof(wchar_t)); // NON_COMPLIANT
580-
wmemcpy(wbuf64 + 1, wbuf64 + 1,
581-
sizeof(wbuf64) / sizeof(wchar_t) - 1); // NON_COMPLIANT
582-
wmemcpy(wbuf64 + 1, wbuf64 + 1,
583-
sizeof(wbuf64) / sizeof(wchar_t) - 2); // COMPLIANT
584-
}
585-
586-
// bsearch
587-
{
588-
int arr[10];
589-
int key = 0;
590-
bsearch(&key, arr, sizeof(arr) / sizeof(int), sizeof(int),
591-
compare); // COMPLIANT
592-
bsearch(&key, arr, sizeof(arr) / sizeof(int) + 1, sizeof(int),
593-
compare); // NON_COMPLIANT
594-
bsearch(&key, arr, sizeof(arr) / sizeof(int) - 1, sizeof(int),
595-
compare); // COMPLIANT
596-
bsearch(&key, arr + 1, sizeof(arr) / sizeof(int) - 1, sizeof(int),
597-
compare); // NON_COMPLIANT
598-
bsearch(NULL, arr, sizeof(arr) / sizeof(int), sizeof(int),
599-
compare); // NON_COMPLIANT
600-
bsearch(&key, NULL, sizeof(arr) / sizeof(int), sizeof(int),
601-
compare); // NON_COMPLIANT
602-
bsearch(&key, arr, sizeof(arr) / sizeof(int), sizeof(int),
603-
NULL); // NON_COMPLIANT
604-
}
605-
606-
// qsort
607-
{
608-
int arr[10];
609-
qsort(arr, sizeof(arr) / sizeof(int), sizeof(int), compare); // COMPLIANT
610-
qsort(arr, sizeof(arr) / sizeof(int) + 1, sizeof(int),
611-
compare); // NON_COMPLIANT
612-
qsort(arr, sizeof(arr) / sizeof(int) - 1, sizeof(int),
613-
compare); // COMPLIANT
614-
qsort(arr + 1, sizeof(arr) / sizeof(int) - 1, sizeof(int),
615-
compare); // NON_COMPLIANT
616-
qsort(arr, sizeof(arr) / sizeof(int), sizeof(int), NULL); // NON_COMPLIANT
617-
}
618-
}
619-
620-
void test_fread_fwrite_static(char *file_name) {
621-
FILE *f = fopen(file_name, "r");
622-
char buf[64];
623-
fread(buf, sizeof(buf), 1, f); // COMPLIANT
624-
fread(buf, sizeof(buf) + 1, 1, f); // NON_COMPLIANT
625-
fread(buf, sizeof(buf) - 1, 1, f); // COMPLIANT
626-
fread(buf + 1, sizeof(buf), 1, f); // NON_COMPLIANT
627-
fread(buf, sizeof(buf) * 2, 1, f); // NON_COMPLIANT
628-
fwrite(buf, sizeof(buf), 1, f); // COMPLIANT
629-
fwrite(buf, sizeof(buf) + 1, 1, f); // NON_COMPLIANT
630-
fwrite(buf, sizeof(buf) - 1, 1, f); // COMPLIANT
631-
fwrite(buf + 1, sizeof(buf), 1, f); // NON_COMPLIANT
632-
fwrite(buf, sizeof(buf) * 2, 1, f); // NON_COMPLIANT
633-
fclose(f);
634-
}
635-
636-
void test_read_file(const char *file_name) {
637-
FILE *f = fopen(file_name, "rb");
638-
639-
fseek(f, 0, SEEK_END);
640-
long len = ftell(f);
641-
rewind(f);
642-
643-
char *buf = malloc(len + 1);
644-
645-
// not correct behaviour below but suffices to test overflow
646-
rewind(f);
647-
fread(buf + 1, len - 1, 1, f); // COMPLIANT
648-
rewind(f);
649-
fread(buf + 1, len, 1, f); // COMPLIANT
650-
rewind(f);
651-
fread(buf + 1, len + 1, 1, f); // COMPLIANT
652-
rewind(f);
653-
fread(buf + 1, len + 2, 1, f); // COMPLIANT
654-
rewind(f);
655-
fread(buf + 1, len + 3, 1, f); // NON_COMPLIANT
656-
657-
fclose(f);
658447
}
659448

660449
void test_equivalent_expressions(void *in, int x, int y, int a, int b) {
661-
short *p = malloc(x * y * sizeof(short));
450+
short *p = (short *)malloc(x * y * sizeof(short));
662451
memcpy(p, in, x * y * sizeof(short)); // COMPLIANT
663452
memcpy(p, in, x * y * sizeof(short) + 1); // NON_COMPLIANT
664453
memcpy(p, in, x * y * sizeof(short) - 1); // COMPLIANT

0 commit comments

Comments
 (0)