Skip to content

Commit c97fea2

Browse files
Matt Helsleyrostedt
authored andcommitted
recordmcount: Remove redundant cleanup() calls
Redundant cleanup calls were introduced when transitioning from the old error/success handling via setjmp/longjmp -- the longjmp ensured the cleanup() call only happened once but replacing the success_file()/fail_file() calls with cleanup() meant that multiple cleanup() calls can happen as we return from function calls. In do_file(), looking just before and after the "goto out" jumps we can see that multiple cleanups() are being performed. We remove cleanup() calls from the nested functions because it makes the code easier to review -- the resources being cleaned up are generally allocated and initialized in the callers so freeing them there makes more sense. Other redundant cleanup() calls: mmap_file() is only called from do_file() and, if mmap_file() fails, then we goto out and do cleanup() there too. write_file() is only called from do_file() and do_file() calls cleanup() unconditionally after returning from write_file() therefore the cleanup() calls in write_file() are not necessary. find_secsym_ndx(), called from do_func()'s for-loop, when we are cleaning up here it's obvious that we break out of the loop and do another cleanup(). __has_rel_mcount() is called from two parts of do_func() and calls cleanup(). In theory we move them into do_func(), however these in turn prove redundant so another simplification step removes them as well. Link: http://lkml.kernel.org/r/de197e17fc5426623a847ea7cf3a1560a7402a4b.1564596289.git.mhelsley@vmware.com Signed-off-by: Matt Helsley <mhelsley@vmware.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 2e63152 commit c97fea2

2 files changed

Lines changed: 0 additions & 15 deletions

File tree

‎scripts/recordmcount.c‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,14 @@ static void *mmap_file(char const *fname)
258258
fd_map = open(fname, O_RDONLY);
259259
if (fd_map < 0) {
260260
perror(fname);
261-
cleanup();
262261
return NULL;
263262
}
264263
if (fstat(fd_map, &sb) < 0) {
265264
perror(fname);
266-
cleanup();
267265
goto out;
268266
}
269267
if (!S_ISREG(sb.st_mode)) {
270268
fprintf(stderr, "not a regular file: %s\n", fname);
271-
cleanup();
272269
goto out;
273270
}
274271
file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
@@ -314,29 +311,25 @@ static int write_file(const char *fname)
314311
fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
315312
if (fd_map < 0) {
316313
perror(fname);
317-
cleanup();
318314
return -1;
319315
}
320316
n = write(fd_map, file_map, sb.st_size);
321317
if (n != sb.st_size) {
322318
perror("write");
323-
cleanup();
324319
close(fd_map);
325320
return -1;
326321
}
327322
if (file_append_size) {
328323
n = write(fd_map, file_append, file_append_size);
329324
if (n != file_append_size) {
330325
perror("write");
331-
cleanup();
332326
close(fd_map);
333327
return -1;
334328
}
335329
}
336330
close(fd_map);
337331
if (rename(tmp_file, fname) < 0) {
338332
perror(fname);
339-
cleanup();
340333
return -1;
341334
}
342335
return 0;
@@ -460,7 +453,6 @@ static int do_file(char const *const fname)
460453
default:
461454
fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
462455
ehdr->e_ident[EI_DATA], fname);
463-
cleanup();
464456
goto out;
465457
case ELFDATA2LSB:
466458
if (*(unsigned char const *)&endian != 1) {
@@ -493,7 +485,6 @@ static int do_file(char const *const fname)
493485
w2(ehdr->e_type) != ET_REL ||
494486
ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
495487
fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
496-
cleanup();
497488
goto out;
498489
}
499490

@@ -502,7 +493,6 @@ static int do_file(char const *const fname)
502493
default:
503494
fprintf(stderr, "unrecognized e_machine %u %s\n",
504495
w2(ehdr->e_machine), fname);
505-
cleanup();
506496
goto out;
507497
case EM_386:
508498
reltype = R_386_32;
@@ -546,14 +536,12 @@ static int do_file(char const *const fname)
546536
default:
547537
fprintf(stderr, "unrecognized ELF class %d %s\n",
548538
ehdr->e_ident[EI_CLASS], fname);
549-
cleanup();
550539
goto out;
551540
case ELFCLASS32:
552541
if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
553542
|| w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
554543
fprintf(stderr,
555544
"unrecognized ET_REL file: %s\n", fname);
556-
cleanup();
557545
goto out;
558546
}
559547
if (w2(ehdr->e_machine) == EM_MIPS) {
@@ -569,7 +557,6 @@ static int do_file(char const *const fname)
569557
|| w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
570558
fprintf(stderr,
571559
"unrecognized ET_REL file: %s\n", fname);
572-
cleanup();
573560
goto out;
574561
}
575562
if (w2(ghdr->e_machine) == EM_S390) {

‎scripts/recordmcount.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ static int find_secsym_ndx(unsigned const txtndx,
463463
}
464464
fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
465465
txtndx, txtname);
466-
cleanup();
467466
return -1;
468467
}
469468

@@ -480,7 +479,6 @@ static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */
480479
if (strcmp("__mcount_loc", txtname) == 0) {
481480
fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
482481
fname);
483-
cleanup();
484482
return already_has_rel_mcount;
485483
}
486484
if (w(txthdr->sh_type) != SHT_PROGBITS ||

0 commit comments

Comments
 (0)