Skip to content

Remove two tests from test_logging_apis.cc#19100

Merged
snnn merged 1 commit intomainfrom
snnn-patch-11
Jan 12, 2024
Merged

Remove two tests from test_logging_apis.cc#19100
snnn merged 1 commit intomainfrom
snnn-patch-11

Conversation

@snnn
Copy link
Copy Markdown
Contributor

@snnn snnn commented Jan 12, 2024

Description

The code has undefined behavior. To prove it, save the following code as test.cpp

#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

@snnn snnn requested a review from adrianlizarraga January 12, 2024 05:10
@snnn snnn merged commit 2cb5781 into main Jan 12, 2024
@snnn snnn deleted the snnn-patch-11 branch January 12, 2024 17:26
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. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants