For secure creation of temporary files, it is advisable to use functions such as mkstemp() or tmpfile(), and ensure secure permissions by either setting appropriate file modes during creation with open() or fopen(), or by using chmod() afterward.
1#include <stdio.h>
2#include <stdlib.h>
3#include <fcntl.h>
4#include <sys/stat.h>
5#include <string.h>
6#include <unistd.h>
7
8int insecureTemporaryFileorDirectoryNonCompliant(char *tempData) {
9 // Noncompliant: Insecure function used
10 char *path = tmpnam(NULL);
11 FILE* f = fopen(path, "w");
12 fputs(tempData, f);
13 fclose(f);
14}
1#include <stdio.h>
2#include <stdlib.h>
3#include <fcntl.h>
4#include <sys/stat.h>
5#include <string.h>
6#include <unistd.h>
7
8int insecureTemporaryFileorDirectoryCompliant(char *tempData) {
9 // Compliant: The file will be opened in "wb+" mode, and will be automatically removed on normal program exit
10 FILE* f = tmpfile();
11 fputs(tempData, f);
12 fclose(f);
13 return 0;
14}