Add unit tests for xasprintf()#816
Conversation
Signed-off-by: Alejandro Colomar <alx@kernel.org>
As other x...() wrappers around functions that allocate, these wrappers are like [v]asprintf(3), but exit on failure. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
asprintf(3) is non-standard, but is provided by GNU, the BSDs, and musl. That makes it portable enough for us to use. This function is much simpler than the burdensome code for allocating the right size. Being simpler, it's thus safer. I took the opportunity to fix the style to my preferred one in the definitions of variables used in these calls, and also in the calls to free(3) with these pointers. That isn't gratuituous, but has a reason: it makes those appear in the diff for this patch, which helps review it. Oh, well, I had an excuse :) Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This is not just a style issue. This should be a hard error, and never compile. ISO C89 already had this feature as deprecated. ISO C99 removed this deprecated feature, for good reasons. If we compile ignoring this warning, shadow is not going to behave well. Cc: Sam James <sam@gentoo.org> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
|
@ikerexxe Do you know why I get linker errors about main()? I really don't understand what I'm doing in the autotools code for it. Edit: ahh, it was just a typo! |
6caaec1 to
7f75d70
Compare
0767c28 to
7f98c6b
Compare
7f98c6b to
0cef708
Compare
|
v2 changes: |
0cef708 to
dfaa64f
Compare
|
v3 changes:
|
dfaa64f to
c23eb2b
Compare
|
v4 changes:
|
c23eb2b to
52116fe
Compare
|
v5 changes:
|
|
v6 changes:
|
dfbd529 to
b03aa7e
Compare
|
v7 changes:
|
|
Here's the log of the tests: The failure seems to be due to reaching line 65, which is Or maybe we can't interpose what glibc calls internally within asprintf(3), so maybe the malloc(3) wrapper is not working as intended. |
Let's wait for Andreas to answer in the other PR |
Sure; let's add a mention here just in case: @cryptomilk |
b03aa7e to
d3ad213
Compare
|
v8 changes:
|
|
Even interposing asprintf(3) directly, the longjmp(3) isn't working. Here's the log: EDIT: My bad, I need to interpose vasprintf(3). |
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
d3ad213 to
e866897
Compare
|
v9 changes:
|
|
Weee! |
|
I would not suggest to wrap exit or other real low level functions, it is not really worth testing. Wrap asprintf() or whatever function your implementation calls and mock it, then you can either return NULL or a valid string. |
|
These are the functions I'm trying to test: ./lib/sprintf.h:40:
inline int
xvasprintf(char **restrict s, const char *restrict fmt, va_list ap)
{
int len;
len = vasprintf(s, fmt, ap);
if (len == -1) {
perror("asprintf");
exit(EXIT_FAILURE);
}
return len;
}./lib/sprintf.h:26:
inline int
xasprintf(char **restrict s, const char *restrict fmt, ...)
{
int len;
va_list ap;
va_start(ap, fmt);
len = xvasprintf(s, fmt, ap);
va_end(ap);
return len;
}One test is just checking that it normally behaves as creating a formatted string; that's easy, and we don't need to wrap anything. But the other test is that the function actually exit(3)s on failure. How would you test that path? We need to actually call exit(3) (or rather, its wrapper). Otherwise, we cannot test that path. I.e., we want to specifically test this path (which is the main purpose of this function): if (len == -1) {
perror("asprintf");
exit(EXIT_FAILURE);
}It seems interposing vasprintf(3) to make it return -1, and then exit(3) to perform a longjmp(3) is working. |
|
Looks fine for me :-) |
|
Now that these changes work, I've merged them to the main PR: #793 |
Link: <shadow-maint#816> Acked-by: Andreas Schneider <https://github.com/cryptomilk> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Link: <shadow-maint#816> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Link: <shadow-maint#816> Suggested-by: Iker Pedrosa <ipedrosa@redhat.com> Acked-by: Andreas Schneider <https://github.com/cryptomilk> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Link: <shadow-maint#816> Suggested-by: Iker Pedrosa <ipedrosa@redhat.com> Acked-by: Andreas Schneider <https://github.com/cryptomilk> Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Link: <shadow-maint#816> Suggested-by: Iker Pedrosa <ipedrosa@redhat.com> Acked-by: Andreas Schneider <https://github.com/cryptomilk> Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Link: <shadow-maint#816> Suggested-by: Iker Pedrosa <ipedrosa@redhat.com> Acked-by: Andreas Schneider <https://github.com/cryptomilk> Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Link: <#816> Suggested-by: Iker Pedrosa <ipedrosa@redhat.com> Acked-by: Andreas Schneider <https://github.com/cryptomilk> Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
@ikerexxe I'll add the tests in this separate PR, to avoid cluttering the discussion for the other changes.