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 |
An attempt to write through a FILE* to a file that was opened as read-only.
| Class Name | Write to Read Only File | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||||||||
| Mnemonic | IO.WRITERO | ||||||||||||
| Categories |
|
||||||||||||
| Availability | Available for C and C++. |
||||||||||||
| Enabling | Checks for this warning class are
disabled by default. To enable them, add the following WARNING_FILTER
rule to the project configuration file.
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 recognize a number of functions that take a FILE* argument and write to that file. Some examples are shown in the table below. 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;
}
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.