GCC error on printf without format string

860 views
Skip to first unread message

Andreas Mueller

unread,
Aug 3, 2015, 5:18:54 PM8/3/15
to cython-users
The code generated for the following currently (0.22.1) fails to compile on GCC:

from libc.stdio cimport printf
printf("asdf\n")

I'm not sure if it has been reported yet.
See @ogrisel's explanation here:
https://github.com/scikit-learn/scikit-learn/pull/4025#issuecomment-114806974

Cheers,
Andy

Andreas Mueller

unread,
Aug 4, 2015, 3:15:35 AM8/4/15
to cython-users
forgot the gcc version:
╰─$ gcc --version                                                                                                                                                                                          1 ↵
gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2

The error message looks something like this:

building 'cython_printf' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c cython_printf.c -o build/temp.linux-x86_64-2.7/cython_printf.o
cython_printf.c: In function ‘__pyx_pf_13cython_printf_blub’:
cython_printf.c:603:3: error: format not a string literal and no format arguments [-Werror=format-security]
   printf(__pyx_k_asdf);
   ^
cc1: some warnings being treated as errors
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1


Lisandro Dalcin

unread,
Aug 5, 2015, 11:38:47 AM8/5/15
to cython-users
Not sure if this one is easy to fix in Cython side, but in the mean
time you can try to workaround it:

cdef extern from *:
const char *message '"asdf\n"' # single-quoted double-quoted string

printf(message)


--
Lisandro Dalcin
============
Research Scientist
Computer, Electrical and Mathematical Sciences & Engineering (CEMSE)
Numerical Porous Media Center (NumPor)
King Abdullah University of Science and Technology (KAUST)
http://numpor.kaust.edu.sa/

4700 King Abdullah University of Science and Technology
al-Khawarizmi Bldg (Bldg 1), Office # 4332
Thuwal 23955-6900, Kingdom of Saudi Arabia
http://www.kaust.edu.sa

Office Phone: +966 12 808-0459

slonik

unread,
Aug 5, 2015, 1:31:16 PM8/5/15
to cython-users

Andreas,
your code works fine on MacOSX-10.9 with clang-3.5. I am only getting format warning, not an error. Apparently your gcc compile line has -Werror=format-security which makes gcc convert harmless warning into an error. Try removing this flag and recompile.
Hope it helps.
--Leo

Stefan Behnel

unread,
Aug 5, 2015, 4:52:47 PM8/5/15
to cython...@googlegroups.com
Lisandro Dalcin schrieb am 05.08.2015 um 17:38:
> On 4 August 2015 at 00:50, Andreas Mueller wrote:
>> forgot the gcc version:
>> ╰─$ gcc --version
>> 1 ↵
>> gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2
>>
>> The error message looks something like this:
>>
>> building 'cython_printf' extension
>> x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall
>> -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g
>> -fstack-protector-strong -Wformat -Werror=format-security -fPIC
>> -I/usr/include/python2.7 -c cython_printf.c -o
>> build/temp.linux-x86_64-2.7/cython_printf.o
>> cython_printf.c: In function ‘__pyx_pf_13cython_printf_blub’:
>> cython_printf.c:603:3: error: format not a string literal and no format
>> arguments [-Werror=format-security]
>> printf(__pyx_k_asdf);
>> ^
>> cc1: some warnings being treated as errors
>> error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
>>
>
> Not sure if this one is easy to fix in Cython side, but in the mean
> time you can try to workaround it:
>
> cdef extern from *:
> const char *message '"asdf\n"' # single-quoted double-quoted string
>
> printf(message)

Well, that's a rather ugly hack. The obvious work-around is to use this
instead:

printf("%s", message")

or to drop the "-Werror=format-security" option, as was already suggested.

Stefan

Andreas van Cranenburgh

unread,
Aug 5, 2015, 7:31:08 PM8/5/15
to cython-users, stef...@behnel.de
On Wednesday, August 5, 2015 at 10:52:47 PM UTC+2, Stefan Behnel wrote:
Well, that's a rather ugly hack. The obvious work-around is to use this
instead:

    printf("%s", message")

This one gives "Python object cannot be passed as a varargs parameter":


printf("%s", "message")

You probably meant:

cdef char *message = "foo"
printf("%s", message)

The most succinct version seems to be:

printf("%s", <char*>"asdf\n")

Stefan Behnel

unread,
Aug 6, 2015, 2:49:27 AM8/6/15
to cython...@googlegroups.com
Andreas van Cranenburgh schrieb am 06.08.2015 um 01:31:
> On Wednesday, August 5, 2015 at 10:52:47 PM UTC+2, Stefan Behnel wrote:
>>
>> Well, that's a rather ugly hack. The obvious work-around is to use this
>> instead:
>>
>> printf("%s", message")
>>
>
> This one gives "Python object cannot be passed as a varargs parameter":
>
> printf("%s", "message")

That was an incomplete feature:

https://github.com/cython/cython/commit/7f89a1d6ac6e022a22ec7341fb852c91fc1b1c6f

> You probably meant:
>
> cdef char *message = "foo"
> printf("%s", message)
>
> The most succinct version seems to be:
>
> printf("%s", <char*>"asdf\n")

Or rather

printf(b"%s", b"asdf\n")

Stefan

Andreas Mueller

unread,
Aug 6, 2015, 8:58:55 AM8/6/15
to cython...@googlegroups.com
We did a workaround by passing an empty string

printf("some stuff\n%s", EMPTY_STRING)

so we didn't have to define a new variable for each message, but yes,
yours works too.
I just wanted to report it.

Andreas Mueller

unread,
Aug 6, 2015, 1:31:32 PM8/6/15
to cython...@googlegroups.com
How hard would it be to fix the original problem?
Or do you not consider it fix-worthy?

Stefan Behnel

unread,
Aug 6, 2015, 5:34:31 PM8/6/15
to cython...@googlegroups.com
This is because Cython doesn't store C charptr string literals as "const"
in the C code. Changing that is a bit more work than you might think
because not all constants are actually used as "const char*" in user code,
but I started working on a patch and it looks good so far. Might have a
pull request ready for review this weekend.

Stefan

Andreas Mueller

unread,
Aug 6, 2015, 5:53:17 PM8/6/15
to cython...@googlegroups.com

Awesome, thanks :)

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/DaysOUhv9do/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan Behnel

unread,
Aug 8, 2015, 4:32:30 AM8/8/15
to cython...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages