JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries


IO.WRITERO : リードオンリーファイルに書き込み

要旨

リードオンリーでオープンされたファイルを FILE* を使用して書き込みしています。

プロパティ

クラス名 Write to Read Only File
日本語クラス名 リードオンリーファイルに書き込み
クラス分類 信頼性 (reliability)
ニーモニック IO.WRITERO
カテゴリー
MisraC2025 MisraC2025:22.4 There shall be no attempt to write to a stream which has been opened as read-only
MisraC2023 MisraC2023:22.4 There shall be no attempt to write to a stream which has been opened as read-only
Misra2012 Misra2012:22.4 There shall be no attempt to write to a stream which has been opened as read-only
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Write to Read Only File"

ワーニングを引き起こす関数

CodeSonar keeps track of files that have been opened as read-only, then triggers a warning if there is an attempt to write to one of these files.

CodeSonar ships with library models that allow it to a number of functions that take a FILE* argument and write to that file. If one of these functions is called with a pointer to a read-only file in the FILE* parameter position, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Write to Read Only File warnings.

Functions that can trigger warnings include...
gcc Builtins __builtin___fprintf_chk(), __builtin_fputc()
libc fprintf(), putc(), write()
Win32 fprintf_s(), _fputc_nolock() _fwrite_nolock()

リードオンリーとしてファイルをオープンする関数

CodeSonar ships with library models that allow it to recognize functions such as libc fopen() and Win32 fopen_s() as opening files as read-only when called with string "r" or "rb" as the mode argument.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be recognized as opening files as read-only in the same circumstances.

#include <stdlib.h>
#include <stdio.h>

int writero(void){
    FILE* f;
    char* str = "r";
    int w;

    f = fopen("A.txt","r");
    if (!f){return -1;}
    w = fputc('A', f);           /* 'Write to Read Only File' warning issued here */
    fclose(f);
    if (w==EOF){return -1;}

    f = fopen("B.txt","rb");
    if (!f){return -1;}
    w = fputc('B', f);           /* 'Write to Read Only File' warning issued here */
    fclose(f);
    if (w==EOF){return -1;}

    f = fopen("C.txt","r+");
    if (!f){return -1;}
    w = fputc('C', f);                          /* f not opened as read-only */
    fclose(f);
    if (w==EOF){return -1;}

    f = fopen("D.txt","w+");
    if (!f){return -1;}
    w = fputc('D', f);                          /* f not opened as read-only */
    fclose(f);
    if (w==EOF){return -1;}

    f = fopen("E.txt", str);
    if (!f){return -1;}
    w = fputc('E', f);           /* 'Write to Read Only File' warning issued here */
    fclose(f);
    if (w==EOF){return -1;}
    return 1;
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.