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

[BUG] Segmentation fault from trying to print std::string as %d #5787

Closed
Source61 opened this issue Oct 29, 2023 · 3 comments
Closed

[BUG] Segmentation fault from trying to print std::string as %d #5787

Source61 opened this issue Oct 29, 2023 · 3 comments

Comments

@Source61
Copy link

Describe the bug

Doesn't happen on its own, but with 2 preceding arguments it segfaults.
Haven't tested anything else, like how many arguments are required to cause segfault, etc.

Code to reproduce the behaviour:

from libcpp.string cimport string

cdef:
        string s = b"abc"

print("addCost %f -> this.cost: %f (this.ip: %d)" % (7, 77, s))

Expected behaviour

No response

OS

Linux (Ubuntu 22.04.3 LTS WSL)

Python version

3.10.12

Cython version

3.0.4

Additional context

No response

@matusvalo
Copy link
Contributor

matusvalo commented Nov 1, 2023

Issue is caused by string s formatted as integer using %d. Changing line to:

print("addCost %f -> this.cost: %f (this.ip: %s)" % (7, 77, s))

Fixes the issue. But still we should error out in better way than segfault...

@matusvalo
Copy link
Contributor

OK this is minimal reproducer:

s= 'asdf'
"%d" % (s,)

Compiling it and importing will cause segfault.

When brackets are not used:

s= 'asdf'
"%d" % s

the compiling and running an example will end up with an exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "nogil.pyx", line 2, in init nogil
    "%d" % s
TypeError: %d format: a number is required, not str

@scoder
Copy link
Contributor

scoder commented Nov 1, 2023

Thanks for investigating. There was a missing failure check which made it continue the formatting with a NULL pointer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants