Skip to content

Commit 9beb100

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 9beb100

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

darshan-runtime/lib/darshan-stdio.c

Lines changed: 7 additions & 3 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)\

0 commit comments

Comments
 (0)