メインコンテンツ

ISO/IEC TS 17961 [accsig]

Accessing shared objects in signal handlers

説明

ルール定義

信号ハンドラー内の共有オブジェクトへのアクセス。1

Polyspace 実装

このチェッカーは、信号ハンドラー内でのデータ アクセスの共有をチェックします。

すべて展開する

問題

信号ハンドラー内でのデータ アクセスの共有は、信号ハンドラー内の共有オブジェクトへのアクセスまたは変更を行った場合に発生します。

リスク

共有オブジェクトへのアクセスまたは変更を行う信号ハンドラー関数を定義する場合、ハンドラーは信号を受け取ると共有オブジェクトへのアクセスまたは変更を行います。他の関数が既にその共有オブジェクトにアクセス中の場合、その関数が競合状態を発生させ、データが不整合状態になる可能性があります。

修正方法

信号ハンドラー内の共有オブジェクトへのアクセスまたは変更を行うには、オブジェクトがロック制御不要のアトミックであることをチェックするか、オブジェクトが整数の場合は volatile sig_atomic_t として宣言します。

例 - 信号ハンドラー内での int 型変数へのアクセス
#include <signal.h>
#include <stdlib.h>
#include <string.h>

/* declare global variable. */
int e_flag;

void sig_handler(int signum)
{
	/* Signal handler accesses variable that is not
	of type volatile sig_atomic_t. */
    e_flag = signum; 
}

int func(void)
{
    if (signal(SIGINT, sig_handler) == SIG_ERR)
    {
        /* Handle error */
        abort();
    }
    /* Program code */
    if (raise(SIGINT) != 0)
    {
        /* Handle error */
        abort();
    }
    /* More code */
    return 0;
}
        
      

この例では、sig_handlerint 型の変数である e_flag にアクセスしています。別の関数からの同時アクセスによって、e_flag が不整合状態になる可能性があります。

修正 — volatile sig_atomic_t 型の変数を宣言

信号ハンドラーから共有変数にアクセスする前に、変数を int 型ではなく volatile sig_atomic_t 型で宣言します。この型の変数には非同期で安全にアクセスできます。

#include <signal.h>
#include <stdlib.h>
#include <string.h>

/* Declare variable of type volatile sig_atomic_t. */
volatile sig_atomic_t e_flag;
void sig_handler(int signum)
{
	/* Use variable of proper type inside signal handler. */
    e_flag = signum;
    
}

int func(void)
{
    if (signal(SIGINT, sig_handler) == SIG_ERR)
    {
        /* Handle error */
        abort();
    }
    /* Program code */
    if (raise(SIGINT) != 0)
    {
        /* Handle error */
        abort();
    }
    /* More code */
    return 0;
} 

チェック情報

決定可能性:決定不可能

バージョン履歴

R2019a で導入


1 Extracts from the standard "ISO/IEC TS 17961 Technical Specification - 2013-11-15" are reproduced with the agreement of AFNOR. Only the original and complete text of the standard, as published by AFNOR Editions - accessible via the website www.boutique.afnor.org - has normative value.