fix: check correct variable after strdup in _bf_parse_tcp_flags#393
Conversation
There was a problem hiding this comment.
LGTM, except for the commit title: fix: check correct variable after strdup in _bf_parse_tcp_flags -> lib: matcher: fix strdup return value check in _bf_parse_tcp_flags as per https://bpfilter.io/developers/style.html.
Did you use any tool to detect this issue? If so, which one? This is definitely something we should not have missed, if there's any tool that can validate this for us, that would be great.
b8a0bc7 to
8a7b502
Compare
|
Thanks for the review! I've updated the commit title to follow the style guide. Regarding the detection: I found this through manual code review while looking for common C bug patterns (unchecked return values, null pointer issues, etc.). For automated detection, tools like:
could potentially catch this pattern. The specific issue is that the strdup result is assigned to |
Thanks for the explanation. We use In the meantime, LGTM. Congrats on your first PR, and thanks for the fix! I'll merge this PR once the CI is green. |
qdeslandes
left a comment
There was a problem hiding this comment.
LGTM, except for the commit title: fix: check correct variable after strdup in _bf_parse_tcp_flags -> lib: matcher: fix strdup return value check in _bf_parse_tcp_flags as per https://bpfilter.io/developers/style.html.
|
Thank you for the kind words and for merging this! 🎉 I saw you created issue #394 for investigating the static analysis detection. I'd be happy to look into that and see if I can help find a configuration that catches these issues without too many false positives. I'll take a look at the clang-tidy options and report back on that issue. |
Thanks, I'll take a look today :) |
Summary
Fix a null check bug in
_bf_parse_tcp_flags()where the wrong variable is checked afterstrdup().The Bug
In
src/libbpfilter/matcher.cat line 617, after callingstrdup(raw_payload)and storing the result in_raw_payload, the code incorrectly checksraw_payload(the input parameter) instead of_raw_payload(the strdup result):The Fix
Check
_raw_payload(the result of strdup) instead:Impact
If
strdup()fails due to memory allocation failure, it returnsNULL. Without this fix:raw_payloadinput is non-null)_raw_payloadis used on line 621:tmp = _raw_payload;strtok_r()callThis fix ensures proper error handling when memory allocation fails.