RESOURCE-write-ronly-file
In this section:
Synopsis
A file opened as read-only is written to.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
A file opened as read-only is written to. This will cause a runtime error in your application, either silently if the file exists, or as a crash if it does not exist. This check is identical to MISRAC2012-Rule-22.4.
Coding standards
- MISRA C:2012 Rule-22.4
(Mandatory) There shall be no attempt to write to a stream which has been opened as read-only
Code examples
The following code example fails the check and will give a warning:
#include <stdio.h>
#include <stdlib.h>
void example(void) {
FILE *f1;
f1 = fopen("test-file.txt", "r");
fprintf(f1, "Hello, World!");
fclose(f1);
}
The following code example passes the check and will not give a warning about this issue:
#include <stdio.h>
#include <stdlib.h>
void example(void) {
FILE *f1;
f1 = fopen("test-file.txt", "r+");
fprintf(f1, "Hello, World!");
fclose(f1);
}