Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

C++: wrong exception handling with std::stoi on non-debug builds #10892

Closed
sagehane opened this issue Feb 14, 2022 · 3 comments
Closed

C++: wrong exception handling with std::stoi on non-debug builds #10892

sagehane opened this issue Feb 14, 2022 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-linux
Milestone

Comments

@sagehane
Copy link
Contributor

sagehane commented Feb 14, 2022

Zig Version

0.10.0-dev.717+90f2a8d9c

Steps to Reproduce

Reproduced on NixOS Unstable and Arch Linux.


Run the following code with any build mode but debug mode.

Sample stoi.cpp:

#include <iostream>

int main() {
  try {
    // Can be stoi, stoul, stoull, etc.
    std::stoi("test", nullptr, 0);
  } catch (...) {
  }

  // Commenting out this line fixes the runtime error.
  // Only causes an error on Fast/Small if it's equal to the function above.
  std::stoi("0");

  return 0;
}

Sample stoi_safe.cpp:

#include <iostream>

int main() {
  try {
    // Can be stoi, stoul, stoull, etc.
    std::stoi("test", nullptr, 0);
  } catch (...) {
  }

  // Commenting out this line fixes the runtime error.
  // Only causes an error on Fast/Small if it's equal to the function above.
  std::stoll("0");

  return 0;
}

Expected Behavior

The code should run (and essentially do nothing) successfully.

$ zig run -lc++ -O Debug stoi_safe.cpp

$ zig run -lc++ -O Debug stoi.cpp

My attempt at replicating the flags passed to Clang by looking at the outputs of --verbose-cc of -O ReleaseSmall:

$ clang++ -fno-spell-checking -flto -mred-zone -fomit-frame-pointer -DNDEBUG -Os -fno-stack-protector -fPIC -fno-unwind-tables -MD -MV -fPIC -fstrict-aliasing -funwind-tables -std=c++11 stoi.cpp
$ ./a.out

Actual Behavior

Causes a runtime error.

Safe:

$ zig run -lc++ -O ReleaseSafe stoi_safe.cpp
libc++abi: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Aborted (core dumped)

$ zig run -lc++ -O ReleaseSafe stoi.cpp
libc++abi: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Aborted (core dumped)

Fast/Small:

$ zig run -lc++ -O ReleaseSmall stoi_safe.cpp

$ zig run -lc++ -O ReleaseSmall stoi.cpp
libc++abi: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Aborted (core dumped)
@sagehane sagehane added the bug Observed behavior contradicts documented or intended behavior label Feb 14, 2022
@sagehane sagehane changed the title C++: std::stoi causes issues only on non-debug builds C++: wrong exception handling with std::stoi on non-debug builds Feb 15, 2022
@sagehane
Copy link
Contributor Author

sagehane commented Jul 2, 2022

Update. I tried running the same code with 0.10.0-dev.2836+2360f8c49 and it seems to throw that uncaught exception error on safe mode even after commenting out the 2nd call to stoi. The behavior doesn't seem to have changed for other release modes.

I also noticed that #9591 might be relevant.

@andrewrk andrewrk added this to the 0.10.0 milestone Aug 19, 2022
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Sep 15, 2022
@sagehane
Copy link
Contributor Author

@andrewrk, I don't seem to be able to reproduce this issue anymore. Do you reckon this is safe to close?

@andrewrk
Copy link
Member

I think we should try to add test coverage for it before closing if we can.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-linux
Projects
None yet
Development

No branches or pull requests

2 participants