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

main

parents
#include <stdint.h>
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f ( uint8_t s1[ 8 ], uint16_t s2[ 8 ] )
{
( void ) memcpy ( s1, s2, 8 ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
typedef _Bool bool_t;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
struct S;
bool_t f1 ( struct S *s1, struct S *s2 )
{
return ( memcmp ( s1, s2, sizeof ( struct S ) ) != 0 ); /* Non-compliant */
}
union U
{
uint32_t range;
uint32_t height;
};
bool_t f2 ( union U *u1, union U *u2 )
{
return ( memcmp ( u1, u2, sizeof ( union U ) ) != 0 ); /* Non-compliant */
}
const char a[ 6 ] = "task";
bool_t f3 ( const char b[ 6 ] )
{
return ( memcmp ( a, b, 6 ) != 0 ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
char string[ ] = "Short";
void f1 ( const char *str )
{
( void ) strcpy ( string, "Too long to fit" ); /* Non-compliant */
if ( strlen ( str ) < ( sizeof ( string ) - 1u ) ) /* Compliant */
{
( void ) strcpy ( string, str );
}
}
size_t f2 ( void )
{
char text[ 5 ] = "Token";
return strlen ( text ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
char buf1[ 5 ] = "12345";
char buf2[ 10 ] = "1234567890";
void f ( void )
{
if ( memcmp ( buf1, buf2, 5 ) == 0 ) /* Compliant */
{
}
if ( memcmp ( buf1, buf2, 6 ) == 0 ) /* Non-compliant */
{
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <locale.h>
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f1 ( void )
{
char *s1 = setlocale ( LC_ALL, 0 ); /* Non-compliant */
struct lconv *conv = localeconv ( ); /* Non-compliant */
s1[ 1 ] = 'A'; /* Undefined behaviour */
conv->decimal_point = "^"; /* Undefined behaviour */
}
void f2 ( void )
{
char str[ 128 ];
( void ) strcpy ( str,
setlocale ( LC_ALL, 0 ) ); /* Compliant */
const struct lconv *conv = localeconv ( ); /* Compliant */
conv->decimal_point = "^"; /* Constraint violation */
}
void f3 ( void )
{
const struct lconv *conv = localeconv ( ); /* Compliant */
conv->grouping[ 2 ] = 'x'; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
typedef double float64_t;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stddef.h>
/* Non-compliant */
extern void *memcpy ( void *restrict s1, const void *restrict s2, size_t n );
/* Non-compliant */
#define _BUILTIN_sqrt( x ) ( x )
#include <math.h>
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void callSqrt ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
/* sqrt may not behave as defined in the Standard */
float64_t x = sqrt ( ( float64_t ) 2.0 );
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
#include <locale.h>
#include <stdio.h>
#include <string.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f1 ( void )
{
const char *res1;
const char *res2;
char copy[ 128 ];
res1 = setlocale ( LC_ALL, 0 );
( void ) strcpy ( copy, res1 );
res2 = setlocale ( LC_MONETARY, "French" );
printf ( "%s\n", res1 ); /* Non-compliant */
printf ( "%s\n", copy ); /* Compliant */
printf ( "%s\n", res2 ); /* Compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdlib.h>
int main ( void )
{
void *b = malloc ( 40 ); /* Non-compliant */
return 1;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
int main ( void )
{
FILE *fp = fopen ( "tmp", "r" ); /* Non-compliant */
return 1;
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
int main ( void )
{
FILE *fp;
fp = fopen ( "tmp-1", "w" ); /* Non-compliant */
fprintf ( fp, "*" );
fp = fopen ( "tmp-2", "w" );
fprintf ( fp, "!" );
fclose ( fp );
return ( 0 );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <errno.h>
#include <stddef.h>
typedef double float64_t;
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f ( void )
{
float64_t f64;
errno = 0;
f64 = atof ( "A.12" );
if ( 0 == errno ) /* Non-compliant */
{
}
errno = 0;
f64 = strtod ( "A.12", NULL );
if ( 0 == errno ) /* Compliant */
{
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdlib.h>
void fn ( void )
{
int32_t a;
free ( &a ); /* Non-compliant */
}
void g ( void )
{
char *p = ( char * ) malloc ( 512 );
char *q = p;
free ( p );
free ( q ); /* Non-compliant */
p = ( char * ) realloc ( p, 1024 ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
void fn ( void )
{
FILE *fw = fopen ( "tmp", "r+" );
FILE *fr = fopen ( "tmp", "r" ); /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
void fn ( void )
{
FILE *fp = fopen ( "tmp", "r" );
( void ) fprintf ( fp, "What happens now?" ); /* Non-compliant */
( void ) fclose ( fp );
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
/* ------------------ Example code from MISRA C:2012 end ------------------ */
void example ( void )
{
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
FILE *pf1;
FILE *pf2;
FILE f3;
pf2 = pf1; /* Compliant */
f3 = *pf2; /* Non-compliant */
pf1->pos = 0; /* Non-compliant */
/* ------------------ Example code from MISRA C:2012 end ------------------ */
}
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
#include <stdio.h>
void fn ( void )
{
FILE *fp;
void *p;
fp = fopen ( "tmp", "w" );
if ( fp == NULL )
{
error_action ( );
}
fclose ( fp );
fprintf ( fp, "?" ); /* Non-compliant */
p = fp; /* Non-compliant */
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <stdint.h>
#include <stdio.h>
/* ----------------- Example code from MISRA C:2012 begin ----------------- */
void f1 ( void )
{
char ch;
ch = ( char ) getchar ( );
if ( EOF != ( int32_t ) ch ) /* Non-compliant */
{
}
}
void f2 ( void )
{
char ch;
ch = ( char ) getchar ( );
if ( !feof ( stdin ) ) /* Compliant */
{
}
}
void f3 ( void )
{
int32_t i_ch;
i_ch = getchar ( );
if ( EOF != i_ch ) /* Compliant */
{
char ch;
ch = ( char ) i_ch;
}
}
/* ------------------ Example code from MISRA C:2012 end ------------------ */
#include <errno.h>
#include <stdio.h>
void f ( void )
{
FILE *fp;
fpos_t *pos;
fp = fopen ( "file.txt", "w+" );
fgetpos ( fp, pos ); /* Non-compliant */
if ( 0 == errno )
{
fsetpos ( fp, pos ); /* Compliant by exception */
if ( 0 == errno )
{
}
}
else
{
errno = 0;
ftell ( fp ); /* Compliant */
if ( 0 == errno )
{
}
}
}
#include <errno.h>
#include <stdio.h>
void f1 ( void )
{
FILE *fp;
fpos_t *pos;
fp = fopen ( "file.txt", "w+" );
errno = 0;
fgetpos ( fp, pos );
someFunction ( ); /* Non-compliant - function call */
if ( 0 != errno )
{
}
errno = 0;
fsetpos ( fp, pos );
if ( 0 != errno ) /* Compliant */
{
}
}
void f2 ( FILE *f, fpos_t *pos )
{
errno = 0;
if ( fsetpos ( f, pos ) == 0 )
{
/* Compliant by exception - no need to test errno as no out-of-band error
reported. */
}
else
{
/* Something went wrong - errno holds an implementation-defined positive value.
*/
handleError ( errno );
}
}
// Example code from MISRA C:2012 begin
/* some comment, end comment marker accidentally omitted
<<New Page>>
Perform_Critical_Safety_Function ( X );
/* this comment is non-compliant */
// 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