Skip to content

Commit 94f192a

Browse files
committed
comparing string literals undefined
There are places where we were trying to compare pointers vs literals like "<STDIN>". that is undefined behavior since the compiler could well put string literals in special regions of memory.
1 parent d72690b commit 94f192a

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

darshan-runtime/lib/darshan-stdio.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ extern int __real_fileno(FILE *stream);
215215
STDIO_UNLOCK(); \
216216
} while(0)
217217

218+
/* When we do '__newpath != __path' in this macro we really do mean to do a
219+
* bare pointer comparison (not contents). Some compilers will complain about
220+
* comparing 'char *' with a string literal, suggesting a string comparison
221+
* function. Normally that's good advice but not here: we are looking at
222+
* actual pointer addresses to know if we should free the memory returned by
223+
* darshan_clean_file_path
224+
*
225+
* since it's so hard to deal with string literals in a standard-defined way,
226+
* just do us a favor and don't pass string literals to this function */
218227
#define STDIO_RECORD_OPEN(__ret, __path, __tm1, __tm2) do { \
219228
darshan_record_id __rec_id; \
220229
struct stdio_file_record_ref *__rec_ref; \
@@ -1065,6 +1074,11 @@ static void stdio_runtime_initialize()
10651074
.mod_cleanup_func = &stdio_cleanup
10661075
};
10671076

1077+
/* some compilers will complain about comparing string literals */
1078+
const char * stdin_str = "<STDIN>";
1079+
const char * stdout_str = "<STDOUT>";
1080+
const char * stderr_str = "<STDERR>";
1081+
10681082
/* if this attempt at initializing fails, we won't try again */
10691083
stdio_runtime_init_attempted = 1;
10701084

@@ -1091,9 +1105,9 @@ static void stdio_runtime_initialize()
10911105
memset(stdio_runtime, 0, sizeof(*stdio_runtime));
10921106

10931107
/* instantiate records for stdin, stdout, and stderr */
1094-
STDIO_RECORD_OPEN(stdin, "<STDIN>", 0, 0);
1095-
STDIO_RECORD_OPEN(stdout, "<STDOUT>", 0, 0);
1096-
STDIO_RECORD_OPEN(stderr, "<STDERR>", 0, 0);
1108+
STDIO_RECORD_OPEN(stdin, stdin_str, 0, 0);
1109+
STDIO_RECORD_OPEN(stdout, stdout_str, 0, 0);
1110+
STDIO_RECORD_OPEN(stderr, stderr_str, 0, 0);
10971111

10981112
/* register a heatmap */
10991113
stdio_runtime->heatmap_id = heatmap_register("heatmap:STDIO");

0 commit comments

Comments
 (0)