[C-safe-secure-studygroup] Draft example for the delta on MISRA C Rule 11.2
Fulvio Baccaglini
fulvio_baccaglini at programmingresearch.com
Wed Nov 29 16:44:56 GMT 2017
Hi,
Trying to capture the delta... I believe that this example might show
some differences due to Rule 11.2 between MISRA C and CERT C, but still
have to check.
Fulvio
//======== File: Main.c ========
#include <assert.h>
#include <stdio.h>
#include "Incomplete.h"
#include "Basket.h"
int main (void)
{
//~~~~ Case 1: Complete vs Incomplete ~~~~
assert (sizeof (long) == sizeof (double));
// Complete
struct LDC { long l; double d; };
struct DLC { double d; long l; };
struct LDC ldc = { 42, 3.14 };
struct DLC * dlc;
dlc = (struct DLC *) & ldc;
printf ("%f %ld\n",
dlc->d,
dlc->l);
dlc = (struct DLC *) ldi ();
printf ("%f %ld\n",
dlc->d,
dlc->l);
dlc = (struct DLC *) dli ();
printf ("%f %ld\n",
dlc->d,
dlc->l);
//~~~~ Case 2: Apples and Oranges ~~~~
set_oranges (my_basket (), 10);
printf ("%d\n", oranges_in (my_basket ()));
struct Fruit
{
Apples apples;
Oranges oranges;
};
((struct Fruit *) my_basket ())->apples = 42;
printf ("%d\n", oranges_in (my_basket ()));
return 0;
}
//======== File: Incomplete.h ========
struct LDI;
struct DLI;
struct LDI * ldi (void);
struct DLI * dli (void);
//======== File: Incomplete.c ========
#include "Incomplete.h"
struct LDI { long l; double d; };
struct DLI { double d; long l; };
struct LDI * ldi (void)
{
static struct LDI x = { 42, 3.14 };
return & x;
}
struct DLI * dli (void)
{
static struct DLI x = { 3.14, 42 };
return & x;
}
//======== File: Basket.h ========
typedef unsigned int Oranges;
typedef unsigned int Apples;
struct Basket;
struct Basket * my_basket (void);
void set_oranges (struct Basket * basket, Oranges number);
Oranges oranges_in (const struct Basket * basket);
//======== File: Basket.c ========
#include "Basket.h"
struct Basket
{
Oranges oranges;
Apples apples;
};
static struct Basket this_basket = { 0, 0 };
struct Basket * my_basket (void) { return & this_basket; }
void set_oranges (struct Basket * basket, Oranges number) {
basket->oranges = number; }
Oranges oranges_in (const struct Basket * basket) { return
basket->oranges; }
---------------------------------------------------------------------------------------
This email has been scanned for email related threats and delivered safely by Mimecast.
For more information please visit http://www.mimecast.com
---------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.trustable.io/pipermail/c-safe-secure-studygroup/attachments/20171129/74960e94/attachment.html>
More information about the C-safe-secure-studygroup
mailing list