---
# System prepended metadata

title: Untitled

---

<center><font color = 000000 size = 64> 安全程式rules講解 </font></center>

<center><h2>
資工二 110590053 許恩誠
</h2></center>
<h2>  
    
    DCL37-C. Do not declare or define a reserved identifier
</h2>

<font color = "purple" size = 5> 重點性質 </font>

(1) All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

(2) All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

(3) Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included, unless explicitly stated otherwise.

(4) All identifiers with external linkage (including future library directions) and errno are always reserved for use as identifiers with external linkage.

(5) Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
___
<font color = "#E00000" size = 5> 不合規代碼範例一(Include Guard)</font>
```c
#ifndef _MY_HEADER_H_
#define _MY_HEADER_H_
 
/* Contents of <my_header.h> */
 
#endif /* _MY_HEADER_H_ */
```
<font color = "#0000A8" size = 5> 合規代碼範例一(Include Guard) </font>
```c
#ifndef MY_HEADER_H
#define MY_HEADER_H
 
/* Contents of <my_header.h> */
 
#endif /* MY_HEADER_H */
```
___
<font color = "#E00000" size = 5> 不合規代碼範例二(File Scope Objects) </font>
```c
#include <stddef.h>
 
static const size_t _max_limit = 1024;
size_t _limit = 100;
 
unsigned int getValue(unsigned int count) {
  return count < _limit ? count : _limit;
}
```
<font color = "#0000A8" size = 5> 合規代碼範例二(File Scope Objects) </font>
```c
#include <stddef.h>
 
static const size_t max_limit = 1024;
size_t limit = 100;
 
unsigned int getValue(unsigned int count) {
  return count < limit ? count : limit;
}
```
___
<font color = "#E00000" size = 5> 不合規代碼範例三(Reserved Macros)</font>
```c
#include <inttypes.h>
#include <stdio.h>
 
static const int_fast16_t INTFAST16_LIMIT_MAX = 12000;
 
void print_fast16(int_fast16_t val) {
  enum { SIZE_MAX = 80 };
  char buf[SIZE_MAX];
  if (INTFAST16_LIMIT_MAX < val) {
    sprintf(buf, "The value is too large");
  } else {
    snprintf(buf, SIZE_MAX, "The value is %" PRIdFAST16, val);
  }
}
```
<font color = "#0000A8" size = 5> 合規代碼範例三(Reserved Macros) </font>
```c
#include <inttypes.h>
#include <stdio.h>
  
static const int_fast16_t MY_INTFAST16_UPPER_LIMIT = 12000;
 
void print_fast16(int_fast16_t val) {
  enum { BUFSIZE = 80 };
  char buf[BUFSIZE];
  if (MY_INTFAST16_UPPER_LIMIT < val) {
    sprintf(buf, "The value is too large");
  } else {
    snprintf(buf, BUFSIZE, "The value is %" PRIdFAST16, val);
  }
}
```
___
<font color = "#E00000" size = 5> 不合規代碼範例四(Identifiers with External Linkage</font>
```c
#include <stddef.h>
  
void *malloc(size_t nbytes) {
  void *ptr;
  /* Allocate storage from own pool and set ptr */
  return ptr;
}
 
void free(void *ptr) {
  /* Return storage to own pool */
}
```
<font color = "#0000A8" size = 5> 合規代碼範例四(Identifiers with External Linkage) </font>
```c
#include <stddef.h>
 
void *my_malloc(size_t nbytes) {
  void *ptr;
  /* Allocate storage from own pool and set ptr */
  return ptr;
}
 
void *my_aligned_alloc(size_t alignment, size_t size) {
  void *ptr;
  /* Allocate storage from own pool, align properly, set ptr */
  return ptr;
}
 
void *my_calloc(size_t nelems, size_t elsize) {
  void *ptr;
  /* Allocate storage from own pool, zero memory, and set ptr */
  return ptr;
}
 
void *my_realloc(void *ptr, size_t nbytes) {
  /* Reallocate storage from own pool and set ptr */
  return ptr;
}
 
void my_free(void *ptr) {
  /* Return storage to own pool */
}
```
___
<font color = "#E00000" size = 5> 不合規代碼範例五(errno)</font>
```c
extern int errno;
```
<font color = "#0000A8" size = 5> 合規代碼範例五(errno)</font>
```c
#include <errno.h>
```
___

<font color = "purple" size = 5> 風險評估(Risk Assessment) </font>


| Rule | Servity(嚴重性) | Likelihood(可能性) | Remediation Cost(修復成本)|Priority(優先事項)| Level |
| -------- | -------- | -------- | -------| --------| --------|
| DCL37-C    | Low    | Unlikely | <center>Low</center>|P3| L3|

<br>

<h2>  

    DCL38-C. Use the correct syntax when declaring a flexible array member
</h2>

<font color = "purple" size = 5> flexible array member(彈性陣列成員)特性</font>

(1)flexible array member說明

(2)flexible array member存取
___
<font color = "purple" size = 5> flexible array member 使用限制 </font>

(1)The incomplete array type must be the last element within the structure.

(2)There cannot be an array of structures that contain a flexible array member.

(3)Structures that contain a flexible array member cannot be used as a member of another structure.

(4)The structure must contain at least one named member in addition to the flexible array member.
___
<font color = "#E00000" size = 5> 不合規代碼範例 </font>
```c
#include <stdlib.h>
  
struct flexArrayStruct {
  int num;
  int data[1];
};
 
void func(size_t array_size) {
  /* Space is allocated for the struct */
  struct flexArrayStruct *structP
    = (struct flexArrayStruct *)
     malloc(sizeof(struct flexArrayStruct)
          + sizeof(int) * (array_size - 1));
  if (structP == NULL) {
    /* Handle malloc failure */
  }
   
  structP->num = array_size;
 
  /*
   * Access data[] as if it had been allocated
   * as data[array_size].
   */
  for (size_t i = 0; i < array_size; ++i) {
    structP->data[i] = 1;
  }
}
```
___

<font color = "#0000A8" size = 5> 合規代碼範例 </font>
```c
#include <stdlib.h>
  
struct flexArrayStruct{
  int num;
  int data[];
};
 
void func(size_t array_size) {
  /* Space is allocated for the struct */
  struct flexArrayStruct *structP
    = (struct flexArrayStruct *)
    malloc(sizeof(struct flexArrayStruct)
         + sizeof(int) * array_size);
  if (structP == NULL) {
    /* Handle malloc failure */
  }
 
  structP->num = array_size;
 
  /*
   * Access data[] as if it had been allocated
   * as data[array_size].
   */
  for (size_t i = 0; i < array_size; ++i) {
    structP->data[i] = 1;
  }
}
```
___

<font color = "purple" size = 5> 風險評估(Risk Assessment) </font>


| Rule | Servity(嚴重性) | Likelihood(可能性) | Remediation Cost(修復成本)|Priority(優先事項)| Level |
| -------- | -------- | -------- | -------| --------| --------|
| DCL38-C    | Low    | Unlikely | <center>Low</center>|P3| L3|

