Skip to content

Commit 28e367a

Browse files
joe-lawrencejpoimboe
authored andcommitted
objtool/klp: fix mkstemp() failure with long paths
The elf_create_file() function fails with EINVAL when the build directory path is long enough to truncate the "XXXXXX" suffix in the 256-byte tmp_name buffer. Simplify the code to remove the unnecessary dirname()/basename() split and concatenation. Instead, allocate the exact number of bytes needed for the path. Acked-by: Song Liu <song@kernel.org> Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com> Link: https://patch.msgid.link/20260310203751.1479229-3-joe.lawrence@redhat.com Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
1 parent 2f2600d commit 28e367a

1 file changed

Lines changed: 3 additions & 20 deletions

File tree

tools/objtool/elf.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <string.h>
1717
#include <unistd.h>
1818
#include <errno.h>
19-
#include <libgen.h>
2019
#include <ctype.h>
2120
#include <linux/align.h>
2221
#include <linux/kernel.h>
@@ -1189,7 +1188,7 @@ struct elf *elf_open_read(const char *name, int flags)
11891188
struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
11901189
{
11911190
struct section *null, *symtab, *strtab, *shstrtab;
1192-
char *dir, *base, *tmp_name;
1191+
char *tmp_name;
11931192
struct symbol *sym;
11941193
struct elf *elf;
11951194

@@ -1203,29 +1202,13 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
12031202

12041203
INIT_LIST_HEAD(&elf->sections);
12051204

1206-
dir = strdup(name);
1207-
if (!dir) {
1208-
ERROR_GLIBC("strdup");
1209-
return NULL;
1210-
}
1211-
1212-
dir = dirname(dir);
1213-
1214-
base = strdup(name);
1215-
if (!base) {
1216-
ERROR_GLIBC("strdup");
1217-
return NULL;
1218-
}
1219-
1220-
base = basename(base);
1221-
1222-
tmp_name = malloc(256);
1205+
tmp_name = malloc(strlen(name) + 8);
12231206
if (!tmp_name) {
12241207
ERROR_GLIBC("malloc");
12251208
return NULL;
12261209
}
12271210

1228-
snprintf(tmp_name, 256, "%s/%s.XXXXXX", dir, base);
1211+
sprintf(tmp_name, "%s.XXXXXX", name);
12291212

12301213
elf->fd = mkstemp(tmp_name);
12311214
if (elf->fd == -1) {

0 commit comments

Comments
 (0)