Skip to content

Commit 072a227

Browse files
fujimotosedsiper
authored andcommitted
in_tail: fix basename(3) overwrite bug
Our current logic is unsafe, as the second call of basename() can silently overwrite the previous result. a_base = basename(a); b_base = basename(b); // can overwrite a_base This occurs because basename(3) is allowed to use a static buffer for the return value (and it indeed does so on macOS). These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. -- Man page of basename(3) A true portable way to use basename() is to call strdup() twice. a_copy = strdup(a); a_base = strdup(basename(a_copy)); This commit fixes it thusly. Signed-off-by: Fujimoto Seiji <[email protected]>
1 parent 1ec8307 commit 072a227

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

plugins/in_tail/tail_file.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static inline int flb_tail_file_name_cmp(char *name,
5252
a = flb_strdup(name);
5353
b = flb_strdup(file->name);
5454

55-
a_base = basename(a);
55+
a_base = flb_strdup(basename(a));
5656
b_base = basename(b);
5757
struct flb_tail_config *ctx = file->config;
5858

@@ -68,6 +68,7 @@ static inline int flb_tail_file_name_cmp(char *name,
6868

6969
flb_free(a);
7070
flb_free(b);
71+
flb_free(a_base);
7172
return ret;
7273
}
7374

@@ -81,7 +82,7 @@ static inline int flb_tail_target_file_name_cmp(char *name,
8182
char *base_b;
8283

8384
name_a = flb_strdup(name);
84-
base_a = basename(name_a);
85+
base_a = flb_strdup(basename(name_a));
8586

8687
#if defined(__linux__) && defined(FLB_HAVE_INOTIFY)
8788
name_b = flb_strdup(file->name);
@@ -99,6 +100,7 @@ static inline int flb_tail_target_file_name_cmp(char *name,
99100

100101
flb_free(name_a);
101102
flb_free(name_b);
103+
flb_free(base_a);
102104

103105
return ret;
104106
}

0 commit comments

Comments
 (0)