Skip to content

Commit 886ea28

Browse files
committed
better handling of special paths
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, so we'll make a copy of the path if it is one of these special cases, simplifying cleanup (always deallocate __newpath)
1 parent d72690b commit 886ea28

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

darshan-runtime/lib/darshan-stdio.c

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

218+
/* 'darshan_clean_file_path' normally returns a newly allocated string, but it
219+
* might return NULL if __path is a special case (e.g. '<STDIN>'). If we see
220+
* that case, we'll make a copy of __path. Now we don't need any special
221+
* checks and _newpath can always bedeallocated in cleanup */
218222
#define STDIO_RECORD_OPEN(__ret, __path, __tm1, __tm2) do { \
219223
darshan_record_id __rec_id; \
220224
struct stdio_file_record_ref *__rec_ref; \
@@ -223,16 +227,16 @@ extern int __real_fileno(FILE *stream);
223227
(void)__darshan_disabled; \
224228
if(!__ret || !__path) break; \
225229
__newpath = darshan_clean_file_path(__path); \
226-
if(!__newpath) __newpath = (char*)__path; \
230+
if(!__newpath) __newpath = strdup((__path)); \
227231
__rec_id = darshan_core_gen_record_id(__newpath); \
228232
__rec_ref = darshan_lookup_record_ref(stdio_runtime->rec_id_hash, &__rec_id, sizeof(darshan_record_id)); \
229233
if(!__rec_ref) __rec_ref = stdio_track_new_file_record(__rec_id, __newpath); \
230234
if(!__rec_ref) { \
231-
if(__newpath != (char*)__path) free(__newpath); \
235+
free(__newpath); \
232236
break; \
233237
} \
234238
_STDIO_RECORD_OPEN(__ret, __rec_ref, __tm1, __tm2, 1, -1); \
235-
if(__newpath != (char*)__path) free(__newpath); \
239+
free(__newpath); \
236240
/* LDMS to publish realtime open tracing information to daemon*/ \
237241
if(dC.ldms_lib)\
238242
if(dC.stdio_enable_ldms)\
@@ -1065,6 +1069,11 @@ static void stdio_runtime_initialize()
10651069
.mod_cleanup_func = &stdio_cleanup
10661070
};
10671071

1072+
/* some compilers will complain about comparing string literals */
1073+
const char * stdin_str = "<STDIN>";
1074+
const char * stdout_str = "<STDOUT>";
1075+
const char * stderr_str = "<STDERR>";
1076+
10681077
/* if this attempt at initializing fails, we won't try again */
10691078
stdio_runtime_init_attempted = 1;
10701079

@@ -1091,9 +1100,9 @@ static void stdio_runtime_initialize()
10911100
memset(stdio_runtime, 0, sizeof(*stdio_runtime));
10921101

10931102
/* 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);
1103+
STDIO_RECORD_OPEN(stdin, stdin_str, 0, 0);
1104+
STDIO_RECORD_OPEN(stdout, stdout_str, 0, 0);
1105+
STDIO_RECORD_OPEN(stderr, stderr_str, 0, 0);
10971106

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

0 commit comments

Comments
 (0)