-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safe RE_VA_ARG helpers #758
Conversation
0754e1a
to
76bacc4
Compare
dd7e64c
to
3c75dd7
Compare
Very impressive, thank you! I am still spending some time in that area too and have written an LLVM Pass to help me find possible errors in variadic functions in general (there are many variadic functions), not only format string related ones. I am basically done going through the whole code base, there is one more call of a variadic function which I am fixing that is non-trivial. I will open a PR for that soon and will report my findings. |
cc3ed0f
to
034c907
Compare
test/test.c
Outdated
@@ -515,7 +515,7 @@ static int test_unit(const char *name, bool verbose) | |||
for (i=0; i<RE_ARRAY_SIZE(tests); i++) { | |||
|
|||
if (verbose) { | |||
re_printf("test %u -- %s\n", | |||
re_printf("test %zu -- %s\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some of the hunks in this patch are fixing the formatting strings.
it could be an idea to make a separate PR for these fixes ...
this is great work! Variable arguments is certainly a source for errors and security issues :) |
I think this can be merged now.. You can also rebase it with main if you want ... |
Currently I have two open issues/todos:
|
this patch is quite large, around 900 lines. since the patch is quite large and intrusive, would it be possible to split it up into smaller parts ? |
Nice thanks! I will rebase this PR soon. |
include/re_types.h
Outdated
if ((safe)) { \ | ||
size_t sz = va_arg((ap), size_t); \ | ||
assert(sz && "RE_VA_ARG: no more arguments"); \ | ||
assert(sz <= sizeof(type) && \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider a warning instead of assert ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice, if a test fails if size is mismatched or no more args available.
btw: cmake sets -DNDEBUG
, on CMAKE_BUILD_TYPE=Release
builds and assert()
is ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another approach is to return an error code if the format is wrong.
This means that e.g. mbuf_printf would return the error code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another approach is to return an error code if the format is wrong.
Most printf code ignores the return code I think, but its already set and returned here:
if (!sz || sz > sizeof(type)) { \
err = EINVAL; \
goto out; \
}
With Release build type assert is not called (I can make this more visible with #ifndef RELEASE
)
The test "dtls_turn" is a combo test, I am not sure if its needed anymore.
Can you also log the printf string that is being printed ? |
I think you should try this patch on both 32-bits and 64-bits platforms. Before the %zu fix in http, the test was failing on Mingw:
perhaps you can rebase this PR to main HEAD ? |
1fc6780
to
f7029f6
Compare
Looks like after this commit, baresip-studio project armeabi-v7a build stopped working:
Any suggestions how to fix the build or should I just stop supporting 32 bit armeabi-v7a architecture? |
The build worked when I commented out this:
But I don't have any armeabi-v7a devices to test if the app actually works. |
Where is this defined? |
Sebastian Reimers writes:
> #define long long long
Where is this defined?
It was defined in my app and was needed before this commit. Otherwise
32 bit build didn't work.
|
RE_VA_ARG
helpers ensure type size safety by using the_Generic
keyword (C11 only) to determine the size of the argument based on its type. This ensures that only arguments of the expected types are passed to the function, and prevents type mismatches that could lead to undefined behavior or security vulnerabilities.