본문 바로가기
MISRA-C 2023

MISRA-C:2023 Mandatory 규칙 정리 모음

by Autosar 2026. 5. 30.
반응형

MISRA C:2023에서는 안전성과 신뢰성 확보를 위해 반드시 준수해야 하는 규칙들을 Mandatory 규칙으로 분류하고 있다.

 

Mandatory 규칙은 단순 권고 수준이 아니라 프로젝트에서 강제되는 경우가 많으며,

특히 자동차 ECU 및 안전 관련 임베디드 시스템에서는 Static Analysis Tool과 함께 매우 엄격하게 관리된다.

 

이번 글에서는 MISRA C:2023 Mandatory 규칙들을 간단한 설명과 함께 정리해보려 한다.

 

Rule 7.5 - The argument of an integer-constant macro shall have an appropriate form

정수 상수 매크로의 인수는 적절한 형태로 작성되어야 한다.

→ 정수 상수 매크로 사용 시 타입 및 표현 형태의 모호성을 방지하기 위한 규칙이다.

 

Rule 9.1 - The value of an object with automatic storage duration shall not be read before it has been set.

자동 저장 영역 객체는 값이 설정되기 전에 읽혀서는 안 된다.

→ 초기화되지 않은 지역 변수 사용을 금지한다.

 

Rule 9.7 - Atomic objects shall be appropriately initialized before being accessed

Atomic 객체는 접근 전에 적절히 초기화되어야 한다.
→ 초기화되지 않은 Atomic 객체 사용으로 인한 예측 불가능한 동작을 방지한다.

 

Rule 12.5 - The sizeof operator shall not have an operand which is a function parameter declared as 'array of type'.

sizeof 연산자는 배열 형태로 선언된 함수 파라미터에 사용되어서는 안 된다.

→ 함수 파라미터의 배열 선언은 실제로 포인터로 처리되므로 sizeof 결과 오해를 방지하기 위한 규칙이다.

 

Rule 17.3 - A function shall not be declared implicitly.

함수는 암시적으로 선언되어서는 안 된다.

→ 함수 원형 선언 없이 함수 호출하는 것을 금지한다.

 

Rule 17.4 - All exit paths from a function with non-void return type shall have an explicit return statement with an expression.

반환형이 void가 아닌 함수의 모든 종료 경로에는 명시적인 return 문이 존재해야 한다.

→ 모든 실행 경로에서 반환값이 명확해야 한다.

 

Rule 17.6 - The declaration of an array parameter shall not contain the static keyword between the [ ].

배열 파라미터 선언의 [] 내부에 static 키워드를 사용해서는 안 된다.
→ 함수 파라미터 배열 선언 시 static 사용을 금지하여 혼동 가능성을 줄이기 위한 규칙이다.

 

Rule 17.9 - A function declared with a _Noreturn function specifier shall not return to its caller

_Noreturn 지정 함수는 호출자에게 반환되어서는 안 된다.

→ 반환되지 않아야 하는 함수의 제어 흐름 일관성을 보장하기 위한 규칙이다.

 

Rule 18.1 - Pointers to variably-modified array types shall not be used

가변 길이 배열(VLA)에 대한 포인터는 사용되어서는 안 된다.
→ 가변 길이 배열(VLA) 사용으로 인한 Stack 사용량 증가 및 예측 어려운 메모리 사용을 방지한다.

 

Rule 19.1 - An object shall not be assigned or copied to an overlapping object.

겹치는 메모리 영역에 객체 복사 또는 대입을 수행해서는 안 된다.
→ 겹치는 메모리 영역에 대한 복사 또는 대입으로 인한 Undefined Behavior를 방지한다.

 

Rule 21.13 - Any value passed to a function in <ctype.h> shall be representable as an unsigned char or be the value EOF.

<ctype.h> 함수에 전달되는 값은 unsigned char 범위 또는 EOF 값이어야 한다.

→ 음수 값 전달 시 Undefined Behavior 발생 가능성을 방지한다.

 

Rule 21.17 - Use of the string handling functions from <string.h> shall not result in accesses beyond the bounds of the objects referenced by their pointer parameters.

<string.h> 함수 사용 시 객체 범위를 초과하는 접근이 발생해서는 안 된다.

→ Buffer Overflow 방지를 위한 규칙이다.

 

Rule 21.18 - The size_t argument passed to any function in <string.h> shall have an appropriate value.

<string.h> 함수에 전달되는 size_t 인수는 적절한 값을 가져야 한다.

→ 문자열 처리 함수 호출 시 잘못된 길이 값 사용으로 인한 Buffer Overflow를 방지한다.

 

Rule 21.19 - The pointers returned by the Standard Library functions localeconv, getenv, setlocale or strerror shall only be used as if they have pointer to const-qualified type.

특정 표준 라이브러리 함수가 반환한 포인터는 const 포인터처럼 사용되어야 한다.

→ 내부 관리 메모리 수정 방지를 위한 규칙이다.

 

Rule 21.2 - The pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale, or strerror shall not be used following a subsequent call to the same function.

특정 표준 라이브러리 함수가 반환한 포인터는 동일 함수의 다음 호출 이후 사용되어서는 안 된다.
→ 내부 static buffer 재사용으로 인한 데이터 변경 문제를 방지한다.

 

Rule 21.22 - All operand arguments to any type-generic macros declared in <tgmath.h> shall have an appropriate essential type

<tgmath.h> 타입 제네릭 매크로 인수는 적절한 essential type을 가져야 한다.

→ 타입 불일치 및 예측 불가능한 동작을 방지한다.

 

Rule 22.12 - Thread objects, thread synchronization objects, and thread-specific storage pointers shall only be accessed by the appropriate Standard Library functions

Thread 객체 및 동기화 객체는 적절한 표준 라이브러리 함수로만 접근해야 한다.
→ Thread 관련 객체를 표준 API를 통해서만 안전하게 접근하도록 제한한다.

 

Rule 22.14 - Thread synchronization objects shall be initialized before being accessed

Thread 동기화 객체는 사용 전에 초기화되어야 한다.

→ Mutex 및 Synchronization 객체 초기화 누락을 방지한다.

 

Rule 22.2 - A block of memory shall only be freed if it was allocated by means of a Standard Library function.

메모리는 표준 라이브러리 함수로 할당된 경우에만 해제되어야 한다.

→ 잘못된 free 호출을 방지한다.

 

Rule 22.4 - There shall be no attempt to write to a stream which has been opened as read-only.

읽기 전용으로 열린 stream에 쓰기를 시도해서는 안 된다.

→ 파일 접근 모드 오류를 방지한다.

 

Rule 22.5 - A pointer to a FILE object shall not be dereferenced.

FILE 객체 포인터를 직접 역참조해서는 안 된다.

→ FILE 내부 구조체는 구현 의존적이므로 직접 접근을 금지한다.

 

Rule 22.6 - The value of a pointer to a FILE shall not be used after the associated stream has been closed.

Stream이 닫힌 이후 FILE 포인터를 사용해서는 안 된다.
→ 닫힌 파일 핸들 접근으로 인한 Undefined Behavior를 방지한다.

 

정리

 

MISRA C:2023 Mandatory 규칙들은 단순 코딩 스타일 제한이 아니다.

 

실제로는

  • Undefined Behavior 방지
  • Memory Corruption 방지
  • Data Race 방지
  • Buffer Overflow 방지
  • Compiler 의존성 감소
  • 유지보수성 향상

등을 목표로 하는 안전 중심 규칙들이다.

 

특히 AUTOSAR 및 ECU 프로젝트에서는 Mandatory 규칙 위반을 Static Analysis Tool로 지속적으로 검사하며, 일부 프로젝트에서는 Mandatory 위반 발생 시 Build 자체를 실패 처리하기도 한다.

반응형