Skip to content

Commit

Permalink
Move symol length check out of rebinding loop (facebook#27)
Browse files Browse the repository at this point in the history
The symbol length check is loop invariant code, and so can be done
once before looping over the rebindings.

Also, replaces `str_longer` with `strnlen`.

This optimization was the work of @ocrickard in
facebook#26
  • Loading branch information
kastiglione authored and grp committed Apr 26, 2016
1 parent bf38895 commit 3958ea3
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions fishhook.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,6 @@ static int prepend_rebindings(struct rebindings_entry **rebindings_head,
return 0;
}

static bool str_longer(const char *symbol, size_t len) {
for (size_t i = 0; i <= len; i++) {
if (symbol[i] == 0) {
return false;
}
}
return true;
}

static void perform_rebinding_with_section(struct rebindings_entry *rebindings,
section_t *section,
intptr_t slide,
Expand All @@ -101,11 +92,13 @@ static void perform_rebinding_with_section(struct rebindings_entry *rebindings,
}
uint32_t strtab_offset = symtab[symtab_index].n_un.n_strx;
char *symbol_name = strtab + strtab_offset;
if (strnlen(symbol_name, 2) < 2) {
continue;
}
struct rebindings_entry *cur = rebindings;
while (cur) {
for (uint j = 0; j < cur->rebindings_nel; j++) {
if (str_longer(symbol_name, 1) &&
strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
if (strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
if (cur->rebindings[j].replaced != NULL &&
indirect_symbol_bindings[i] != cur->rebindings[j].replacement) {
*(cur->rebindings[j].replaced) = indirect_symbol_bindings[i];
Expand Down

0 comments on commit 3958ea3

Please sign in to comment.