Commit 2a291a73 authored by shengnan hu's avatar shengnan hu
Browse files

main

parents
#include <stdint.h>
typedef _Bool bool_t;
void example ( void )
{
int16_t x;
bool_t flag;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
switch ( x )
{
case 1: /* Compliant */
if ( flag )
{
case 2: /* Non-compliant */
x = 1;
}
break;
default:
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
int16_t x, a, b;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
switch ( x )
{
case 0:
break; /* Compliant */
case 1: /* Compliant */
case 2:
break; /* Compliant */
case 4:
a = b; /* Non-compliant */
case 5:
if ( a == b )
{
++a;
break; /* Non-compliant */
}
default:
; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
typedef _Bool bool_t;
void example ( void )
{
bool_t errorflag;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int16_t x;
switch ( x ) /* Non-compliant */
{
case 0:
++x;
break;
case 1:
case 2:
break;
}
switch ( x ) /* Compliant */
{
case 0:
++x;
break;
case 1:
case 2:
break;
default:
errorflag = 1;
break;
}
enum Colours
{ RED, GREEN, BLUE } colour, next;
switch ( colour ) /* Non-compliant */
{
case RED:
next = GREEN;
break;
case GREEN:
next = BLUE;
break;
case BLUE:
next = RED;
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
int16_t x;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
switch ( x )
{
default: /* Compliant */
case 0:
++x;
break;
case 1:
case 2:
break;
}
switch ( x )
{
case 0:
++x;
break;
default: /* Non-compliant */
x = 0;
break;
case 1:
case 2:
break;
}
switch ( x )
{
case 0:
++x;
break;
case 1:
case 2:
break;
default: /* Compliant */
x = 0;
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
void example ( void )
{
int16_t x, y, z;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
switch ( x )
{
default: /* Non-compliant */
x = 0;
break;
}
switch ( y )
{
case 1:
default: /* Non-compliant */
y = 0;
break;
}
switch ( z )
{
case 1:
z = 2;
break;
default: /* Compliant */
z = 0;
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
#define false 0
void example ( void )
{
int16_t x, y, z;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
switch ( x == 0 ) /* Non-compliant */
{
case false:
y = x;
break;
default:
y = z;
break;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdarg.h>
void h ( va_list ap ) /* Non-compliant */
{
double y;
y = va_arg ( ap, double ); /* Non-compliant */
}
void f ( uint16_t n, ... )
{
uint32_t x;
va_list ap; /* Non-compliant */
va_start ( ap, n ); /* Non-compliant */
x = va_arg ( ap, uint32_t ); /* Non-compliant */
h ( ap );
x = va_arg ( ap, uint32_t ); /* Non-compliant */
}
void g ( void )
{
f ( 1, 2.0, 3.0 );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
extern double power ( double d, int n );
void func ( void )
{
double sql = power ( 1, 2.0 ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
#define V_MAX 4
#define V_MIN 0
uint16_t table[ 5 ];
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int32_t absolute ( int32_t v ) /* Non-compliant */
{
if ( v < 0 )
{
return v;
}
}
uint16_t lookup ( uint16_t v )
{
if ( ( v < V_MIN ) || ( v > V_MAX ) )
{
return; /* Non-compliant */
}
return table[ v ];
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void fn1 ( int32_t array1[ 4 ] );
void fn2 ( int32_t array2[ ] );
void fn ( int32_t *ptr )
{
int32_t arr3[ 3 ] = { 1, 2, 3 };
int32_t arr4[ 4 ] = { 0, 1, 2, 3 };
/* Compliant - size of array matches the prototype */
fn1 ( arr4 );
/* Non-compliant - size of array does not match prototype */
fn1 ( arr3 );
/* Compliant only if ptr points to at least 4 elements */
fn1 ( ptr );
/* Compliant */
fn2 ( arr4 );
/* Compliant */
fn2 ( ptr );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void fn1PtrTest ( void )
{
int32_t arr3[ 3 ] = { 1, 2, 3 };
int32_t arr4[ 4 ] = { 0, 1, 2, 3 };
/* Non-compliant */
fn ( arr3 );
/* Compliant */
fn ( arr4 );
}
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint16_t total ( uint16_t n, uint16_t a[ static 20 ] ) /* Non-compliant */
{
uint16_t i;
uint16_t sum = 0U;
for ( i = 0U; i < n; ++i )
{
sum = sum + a[ i ];
}
return sum;
}
extern uint16_t v1[ 10 ];
extern uint16_t v2[ 20 ];
void g ( void )
{
uint16_t x;
x = total ( 10U, v1 );
x = total ( 20U, v2 );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
uint16_t func ( uint16_t para1 )
{
return para1;
}
uint16_t x;
void discarded ( uint16_t para2 )
{
func ( para2 ); /* Non-compliant */
( void ) func ( para2 ); /* Compliant */
x = func ( para2 ); /* Compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int16_t glob = 0;
void proc ( int16_t para )
{
para = glob; /* Non-compliant */
}
void f ( char *p, char *q )
{
p = q; /* Non-compliant */
*p = *q; /* Compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int32_t f1 ( int32_t * const a1, int32_t a2[ 10 ] )
{
int32_t *p = &a1[ 3 ];
return *( a2 + 9 ); /* Compliant */
}
void f2 ( void )
{
int32_t data = 0;
int32_t b = 0;
int32_t c[ 10 ] = { 0 };
int32_t d[ 5 ][ 2 ] = { 0 };
int32_t *p1 = &c[ 0 ]; /* Compliant */
int32_t *p2 = &c[ 10 ]; /* Compliant */
int32_t *p3 = &c[ 11 ]; /* Non-compliant */
data = *p2; /* Non-compliant */
data = f1 ( &b, c ); /* Non-compliant */
data = f1 ( c, c ); /* Compliant */
p1++; /* Compliant */
c[ -1 ] = 0; /* Non-compliant */
data = c[ 10 ]; /* Non-compliant */
data = *( &data + 0 ); /* Compliant */
d[ 3 ][ 1 ] = 0; /* Compliant */
data = *( *( d + 3 ) + 1 ); /* Compliant */
data = d[ 2 ][ 3 ]; /* Non-compliant */
p1 = d[ 1 ]; /* Compliant */
data = p1[ 1 ]; /* Compliant */
}
struct
{
uint16_t x;
uint16_t y;
uint16_t z;
uint16_t a[ 10 ];
} s;
uint16_t *p;
void f3 ( void )
{
p = &s.x;
++p; /* Compliant */
p[ 0 ] = 1; /* Non-compliant */
p[ 1 ] = 2; /* Non-compliant */
p = &s.a[ 0 ]; /* Compliant */
p = p + 8; /* Compliant */
p = p + 3; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stddef.h>
void f1 ( int32_t *ptr )
{
int32_t a1[ 10 ];
int32_t a2[ 10 ];
int32_t *p1 = &a1[ 1 ];
int32_t *p2 = &a2[ 10 ];
ptrdiff_t diff;
diff = p1 - a1; /* Compliant */
diff = p2 - a2; /* Compliant */
diff = p1 - p2; /* Non-compliant */
diff = ptr - p1; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f1 ( void )
{
int32_t a1[ 10 ];
int32_t a2[ 10 ];
int32_t *p1 = a1;
if ( p1 < a1 ) /* Compliant */
{
}
if ( p1 < a2 ) /* Non-compliant */
{
}
}
struct limits
{
int32_t lwb;
int32_t upb;
};
void f2 ( void )
{
struct limits limits_1 = { 2, 5 };
struct limits limits_2 = { 10, 5 };
if ( &limits_1.lwb <= &limits_1.upb ) /* Compliant */
{
}
if ( &limits_1.lwb > &limits_2.upb ) /* Non-Compliant */
{
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void fn1 ( void )
{
uint8_t a[ 10 ];
uint8_t *ptr;
uint8_t index = 0U;
index = index + 1U; /* Compliant */
a[ index ] = 0U; /* Compliant */
ptr = &a[ 5 ]; /* Compliant */
ptr = a;
ptr++; /* Compliant */
*( ptr + 5 ) = 0U; /* Non-compliant */
ptr[ 5 ] = 0U; /* Compliant */
}
void fn2 ( void )
{
uint8_t array_2_2[ 2 ][ 2 ] = { { 1U, 2U }, { 4U, 5U } };
uint8_t i = 0U;
uint8_t j = 0U;
uint8_t sum = 0U;
for ( i = 0U; i < 2U; i++ )
{
uint8_t *row = array_2_2[ i ];
for ( j = 0U; j < 2U; j++ )
{
sum += row[ j ]; /* Compliant */
}
}
}
void fn3 ( uint8_t *p1, uint8_t p2[ ] )
{
p1++; /* Compliant */
p1 = p1 + 5; /* Non-compliant */
p1[ 5 ] = 0U; /* Compliant */
p2++; /* Compliant */
p2 = p2 + 3; /* Non-compliant */
p2[ 3 ] = 0U; /* Compliant */
}
uint8_t a1[ 16 ];
uint8_t a2[ 16 ];
uint8_t data = 0U;
void fn4 ( void )
{
fn3 ( a1, a2 );
fn3 ( &data, &a2[ 4 ] );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
typedef int8_t * INTPTR;
void function ( int8_t **arrPar[ ] ) /* Non-Compliant */
{
int8_t ** obj2; /* Compliant */
int8_t *** obj3; /* Non-compliant */
INTPTR * obj4; /* Compliant */
INTPTR * const * const obj5; /* Non-compliant */
int8_t ** arr[ 10 ]; /* Compliant */
int8_t ** ( *parr )[ 10 ]; /* Compliant */
int8_t * ( **pparr )[ 10 ]; /* Compliant */
}
struct s
{
int8_t * s1; /* Compliant */
int8_t ** s2; /* Compliant */
int8_t *** s3; /* Non-compliant */
};
struct s * ps1; /* Compliant */
struct s ** ps2; /* Compliant */
struct s *** ps3; /* Non-compliant */
int8_t ** ( *pfunc1 ) ( void ); /* Compliant */
int8_t ** ( **pfunc2 ) ( void ); /* Compliant */
int8_t ** ( ***pfunc3 ) ( void ); /* Non-compliant */
int8_t *** ( **pfunc4 ) ( void ); /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
int8_t *func ( void )
{
int8_t local_auto;
return &local_auto; /* Non-compliant */
}
uint16_t *sp;
void g ( uint16_t *p )
{
sp = p; /* Non-compliant */
}
void f ( uint16_t u )
{
g ( &u );
}
void h ( void )
{
static uint16_t *q;
uint16_t x = 0u;
q = &x; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdlib.h>
struct s
{
uint16_t len;
uint32_t data[ ]; /* Non-compliant */
} str;
struct s *copy ( struct s *s1 )
{
struct s *s2;
s2 = malloc ( sizeof ( struct s ) + ( s1->len * sizeof ( uint32_t ) ) );
*s2 = *s1;
return s2;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment