Skip to content
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

IKOS should tolerate snprintf(NULL, 0, ...) #221

Open
szhorvat opened this issue Jul 27, 2023 · 2 comments
Open

IKOS should tolerate snprintf(NULL, 0, ...) #221

szhorvat opened this issue Jul 27, 2023 · 2 comments
Labels
C-false-positive Category: False Positive L-c Language: C

Comments

@szhorvat
Copy link

szhorvat commented Jul 27, 2023

ikos claims that the following C program is unsafe because of the null pointer passed to snprintf():

#include <stdio.h>

int main(void) {
    snprintf(NULL, 0, "%d", 123);
    return 0;
}

However, according to cppreference, when the second argument is 0, the first argument is allowed to be NULL.

https://en.cppreference.com/w/c/io/fprintf

If bufsz is zero, nothing is written and buffer may be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned.

We use this feature to determine the number of characters that would be written before actually writing them.

@ivanperez-keera
Copy link
Collaborator

I could be wrong, but at first glance I think we'd need to change:

case ar::Intrinsic::LibcSnprintf: {
return {this->check_null(call, call->argument(0), inv),
this->check_null(call, call->argument(2), inv)};
}

so that this->check_null(call, call->argument(0), inv) is only included when this->_lit_factory.get_scalar(call->argument(1)); is not zero. There may be several cases that we'd have to consider, such as it not being zero but being a variable (which may contain the value zero) and so on.

@arthaud
Copy link
Member

arthaud commented Nov 29, 2023

Maybe handling the special case of argument 1 being the 0 constant would be fine for now since that seems like a common use case.

@ivanperez-keera ivanperez-keera changed the title ikos should tolerate snprintf(NULL, 0, ...) ikos should tolerate snprintf(NULL, 0, ...) Dec 18, 2023
@ivanperez-keera ivanperez-keera changed the title ikos should tolerate snprintf(NULL, 0, ...) IKOS should tolerate snprintf(NULL, 0, ...) Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-false-positive Category: False Positive L-c Language: C
Projects
None yet
Development

No branches or pull requests

3 participants