Skip to content

Commit

Permalink
Ensure that strlen() returns a value >= 0
Browse files Browse the repository at this point in the history
Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
Reviewed-by: Eugene Loh <eugene.loh@oracle.com>
  • Loading branch information
kvanhees committed Apr 8, 2022
1 parent 31bbe75 commit 9489c3b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bpf/strlen.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ noinline uint64_t dt_strlen(const dt_dctx_t *dctx, const char *str)
char *tmp = dctx->strtab + (uint64_t)&STBSZ;
int64_t len;

len = bpf_probe_read_str(tmp, (uint64_t)&STRSZ + 1, str);
/*
* The bpf_probe_read_str() helper returns either a negative value (for
* error conditions) or a positive value (string length + 1 to account
* for the terminating 0-byte). It will never return 0, so it is safe
* to speculatively subtract 1. Any negative value will be converted
* into a 0.
*/
len = bpf_probe_read_str(tmp, (uint64_t)&STRSZ + 1, str) - 1;
set_not_neg_bound(len);

return len - 1; /* bpf_probe_read_str() never returns 0 */
return len;
}

0 comments on commit 9489c3b

Please sign in to comment.