2018-01-23 17:18:47 +01:00
|
|
|
// To test:
|
|
|
|
// ~/cppcheck/cppcheck --dump cert-test.c && python ../cert.py -verify cert-test.c.dump
|
2020-11-15 12:46:12 +01:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2018-01-23 17:18:47 +01:00
|
|
|
struct S {
|
|
|
|
short a;
|
|
|
|
short b;
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack()
|
|
|
|
struct PackedStruct {
|
|
|
|
short a;
|
|
|
|
short b;
|
|
|
|
};
|
|
|
|
|
2019-07-24 10:39:31 +02:00
|
|
|
void api01()
|
|
|
|
{
|
|
|
|
const size_t String_Size = 20;
|
|
|
|
struct bad_node_s
|
|
|
|
{
|
|
|
|
char name[String_Size];
|
|
|
|
struct bad_node_s* next; // cert-API01-C
|
2020-11-15 12:46:12 +01:00
|
|
|
};
|
2019-07-24 10:39:31 +02:00
|
|
|
struct good_node_s
|
|
|
|
{
|
2019-11-21 06:46:46 +01:00
|
|
|
struct good_node_s* next;
|
2019-07-24 10:39:31 +02:00
|
|
|
char name[String_Size];
|
2020-11-15 12:46:12 +01:00
|
|
|
};
|
2019-07-24 10:39:31 +02:00
|
|
|
struct also_good_node_s
|
|
|
|
{
|
2019-11-21 06:46:46 +01:00
|
|
|
struct also_good_node_s* next;
|
2019-07-24 10:39:31 +02:00
|
|
|
char *name;
|
2020-11-15 12:46:12 +01:00
|
|
|
};
|
2019-07-24 10:39:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 13:35:19 +02:00
|
|
|
void dostuff(int *data);
|
|
|
|
|
|
|
|
void exp05()
|
|
|
|
{
|
|
|
|
const int x = 42;
|
2018-04-03 15:12:01 +02:00
|
|
|
int y = (int)x;
|
|
|
|
|
2018-04-03 13:35:19 +02:00
|
|
|
int *p;
|
|
|
|
p = (int *)&x; // cert-EXP05-C
|
|
|
|
|
|
|
|
const int data[] = {1,2,3,4};
|
|
|
|
dostuff(data); // cert-EXP05-C
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:23:50 +02:00
|
|
|
void print(const char *p);
|
|
|
|
void exp05_fp() {
|
|
|
|
print("hello");
|
|
|
|
}
|
|
|
|
|
2018-01-23 17:18:47 +01:00
|
|
|
void exp42()
|
|
|
|
{
|
|
|
|
struct S s1 = {1,2};
|
|
|
|
struct S s2 = {1,2};
|
|
|
|
memcmp(&s1, &s2, sizeof(struct S)); // cert-EXP42-C
|
|
|
|
|
|
|
|
struct PackedStruct s3 = {1,2};
|
|
|
|
struct PackedStruct s4 = {1,2};
|
|
|
|
memcmp(&s3, &s4, sizeof(struct S));
|
|
|
|
}
|
|
|
|
|
2020-11-15 12:46:12 +01:00
|
|
|
void exp46(int x, int y, int z)
|
2018-01-23 22:43:29 +01:00
|
|
|
{
|
|
|
|
if ((x == y) & z) {} // cert-EXP46-c
|
|
|
|
}
|
|
|
|
|
2022-01-20 21:09:39 +01:00
|
|
|
void int31(int x)
|
2018-01-23 17:18:47 +01:00
|
|
|
{
|
|
|
|
x = (unsigned char)1000; // cert-INT31-c
|
|
|
|
x = (signed char)0xff; // cert-INT31-c
|
|
|
|
x = (unsigned char)-1; // cert-INT31-c
|
|
|
|
x = (unsigned long long)-1; // cert-INT31-c
|
2022-01-20 21:09:39 +01:00
|
|
|
unsigned char c;
|
|
|
|
c = 256;
|
|
|
|
c = -1;
|
2022-02-05 14:02:01 +01:00
|
|
|
|
|
|
|
// issue #10782
|
|
|
|
uint16_t * ptr;
|
|
|
|
uint32_t var = 65536;
|
|
|
|
ptr = (uint16_t *)var;
|
2018-01-23 17:18:47 +01:00
|
|
|
}
|
2018-08-24 15:05:50 +02:00
|
|
|
|
2019-11-21 06:46:46 +01:00
|
|
|
void env33()
|
|
|
|
{
|
|
|
|
system("chmod -x $(which chmod)"); // cert-ENV33-C
|
2019-11-24 10:31:08 +01:00
|
|
|
system(""); // cert-ENV33-C
|
|
|
|
system(NULL); // no-warning
|
|
|
|
system(0); // no-warning
|
|
|
|
const int *np = NULL;
|
|
|
|
system(np); // no-warning
|
2019-11-21 06:46:46 +01:00
|
|
|
int system;
|
|
|
|
}
|
|
|
|
|
2019-07-04 12:19:52 +02:00
|
|
|
void msc24()
|
|
|
|
{
|
|
|
|
struct S {
|
|
|
|
int x; int fopen;
|
|
|
|
};
|
2019-11-21 06:46:46 +01:00
|
|
|
|
2019-07-04 12:19:52 +02:00
|
|
|
struct S s;
|
|
|
|
time_t rawtime;
|
|
|
|
struct tm *timeinfo;
|
|
|
|
char buffer[256];
|
|
|
|
int i;
|
|
|
|
long int li;
|
|
|
|
long long int lli;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
s.fopen = 123;
|
|
|
|
|
|
|
|
f = fopen ("myfile.txt","w+"); //cert-MSC24-C
|
2020-11-15 12:46:12 +01:00
|
|
|
setbuf ( f , buffer ); //cert-MSC24-C
|
2019-07-04 12:19:52 +02:00
|
|
|
for ( i='A' ; i<='Z' ; i++)
|
2020-11-15 12:46:12 +01:00
|
|
|
fputc ( i, f);
|
2019-07-04 12:19:52 +02:00
|
|
|
rewind (f); //cert-MSC24-C
|
|
|
|
fclose (f);
|
|
|
|
|
|
|
|
time ( &rawtime );
|
|
|
|
timeinfo = localtime ( &rawtime );
|
|
|
|
printf ( "The current date/time is: %s", asctime (timeinfo) ); //cert-MSC24-C
|
|
|
|
|
2020-11-15 12:46:12 +01:00
|
|
|
float n = atof (buffer); //cert-MSC24-C
|
|
|
|
float m = sin (n*M_PI/180);
|
2019-07-04 12:19:52 +02:00
|
|
|
|
|
|
|
i = atoi (buffer); //cert-MSC24-C
|
|
|
|
|
|
|
|
li = atol(buffer); //cert-MSC24-C
|
|
|
|
|
|
|
|
lli = atoll(buffer); //cert-MSC24-C
|
|
|
|
|
|
|
|
time (&rawtime);
|
|
|
|
printf ("The current local time is: %s", ctime (&rawtime)); //cert-MSC24-C
|
|
|
|
|
|
|
|
freopen ("myfile.txt","w",stdout); //cert-MSC24-C
|
|
|
|
printf ("This sentence is redirected to a file.");
|
|
|
|
fclose (stdout);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:05:50 +02:00
|
|
|
void msc30()
|
|
|
|
{
|
|
|
|
unsigned int num = rand(); // cert-MSC30-c
|
|
|
|
int rand = 5;
|
|
|
|
int a = rand;
|
|
|
|
}
|
2019-06-17 20:42:23 +02:00
|
|
|
|
2019-06-26 18:49:47 +02:00
|
|
|
void exp15()
|
|
|
|
{
|
|
|
|
int x=5, y=7;
|
|
|
|
|
|
|
|
if(x==y); //cert-EXP15-C
|
|
|
|
{
|
|
|
|
printf("not working\n");
|
|
|
|
}
|
|
|
|
if(x)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:41:43 +02:00
|
|
|
void str03()
|
|
|
|
{
|
|
|
|
char *string_data=(char*)malloc(16);
|
|
|
|
char a[16];
|
|
|
|
int d;
|
|
|
|
strncpy(a, string_data, sizeof(a)); //cert-STR03-C
|
|
|
|
strncpy(a, string_data, 5); d=sizeof(int);
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:42:23 +02:00
|
|
|
void str05()
|
|
|
|
{
|
2019-06-26 18:49:47 +02:00
|
|
|
int x=5, y=7;
|
|
|
|
|
|
|
|
if(x==y); //cert-EXP15-C
|
|
|
|
{
|
|
|
|
printf("not working\n");
|
|
|
|
}
|
|
|
|
if(x)
|
|
|
|
;
|
2019-06-17 20:42:23 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 13:59:24 +02:00
|
|
|
void str07(char *buf, const char *newBuf)
|
|
|
|
{
|
2020-11-15 12:46:12 +01:00
|
|
|
const char *str = "test";
|
2019-06-23 13:59:24 +02:00
|
|
|
strcat(buf,"bla");
|
|
|
|
strcat(buf, str); //cert-STR07-C
|
|
|
|
strcat(buf, newBuf); //cert-STR07-C
|
2020-11-15 12:46:12 +01:00
|
|
|
strcpy(buf, newBuf); //cert-STR07-C
|
2019-06-23 13:59:24 +02:00
|
|
|
}
|
2019-07-02 11:44:24 +02:00
|
|
|
|
|
|
|
void str11()
|
|
|
|
{
|
|
|
|
const char str[3]="abc"; //cert-STR11-C
|
|
|
|
const char *x[10]; x[3]="def";
|
|
|
|
}
|
|
|
|
|