Conversation
adrianlizarraga
approved these changes
Jan 12, 2024
mszhanyi
pushed a commit
that referenced
this pull request
Jan 15, 2024
### Description
In some environments the test code has undefined behavior. To prove it, save the following code as
test.cpp
```c++
#include <iostream>
#include <stdio.h>
int main(){
char buf[1024];
int ret = snprintf(buf, sizeof(buf), "%ls","abc");
if(ret <0){
std::cout<< ret<< std::endl;
} else{
std::cout<< "OK: ret="<<ret<< std::endl;
}
return 0;
}
```
Then compile it as
```
g++ -DNDEBUG -std=gnu++17 test.cpp -o /tmp/t
```
Or
```
g++ -O2 -DNDEBUG -std=gnu++17 test.cpp -o /tmp/t
```
The first command is without optimization. The second one turns on
optimization. Then the outputs are different.
When optimization is enabled, the output might be:
```
OK: ret=-1
```
You cannot explain why it would go to this branch when ret is "-1". It
might be a bug of a specific version of GCC. However, at this moment we
cannot change the version. It was found in GCC version 8.5.0 20210514
(Red Hat 8.5.0-18) (GCC) that is provided by UBI8. RHEL9 doesn't have
the problem. snprintf is a builtin function of GCC. So the problem was
not related to glibc.
### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The code has undefined behavior. To prove it, save the following code as test.cpp
Then compile it as
Or
The first command is without optimization. The second one turns on optimization. Then the outputs are different.
When optimization is enabled, the output might be:
You cannot explain why it would go to this branch when ret is "-1". It might be a bug of a specific version of GCC. However, at this moment we cannot change the version. It was found in GCC version 8.5.0 20210514 (Red Hat 8.5.0-18) (GCC) that is provided by UBI8. RHEL9 doesn't have the problem. snprintf is a builtin function of GCC. So the problem was not related to glibc.
Motivation and Context