Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #107858

Re: How to fix: [-Wformat-nonliteral]

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: How to fix: [-Wformat-nonliteral]
Date 2017-04-08 13:41 +0200
Organization A noiseless patient Spider
Message-ID <ocai4l$lc8$1@dont-email.me> (permalink)
References <oc7g6c$lpq$1@dont-email.me> <oc7nm7$d6i$1@dont-email.me> <87mvbsv0tq.fsf@bsb.me.uk> <oc85gk$s8g$1@dont-email.me> <87h920ukud.fsf@bsb.me.uk>

Show all headers | View raw


On 07/04/17 20:35, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 07/04/17 14:50, Ben Bacarisse wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>> <snip>
>>>> #define my_errPrintfBuffLen 256
>>>> #define my_err(f, x...) \
>>>> 	do { \
>>>> 		char buff[my_errPrintfBuffLen]; \
>>>> 		snprintf(buff, sizeof(buff) - 1, f, ## x); \
>>>> 		buff[my_errPrintfBuffLen - 1] = 0; \
>>>
>>> I was also going to mention this.  It seems a bit belt and braces.  Is
>>> there any problem with
>>>
>>>  		snprintf(buff, sizeof(buff), f, ## x); \
>>>
>>> and no extra "buff[my_errPrintfBuffLen - 1] = 0;"?
>>
>> Looks like you are right - snprintf guarantees to put a null character
>> at the end.  The history of this macro in my code stretches back to use
>> with older compilers from long ago, where I was less confident in the
>> accuracy of the C library implementation.  I can't remember if I
>> actually found a problem with a missing termination, or merely thought
>> it was a possibility.
>
> Yes, that was why I asked.  The sn* family predate the C99 standard but
> I don't recall any that would not generate a string (unless the size is
> zero of course).  But then my memory is not what it was, so I asked if
> you had had reason to be suspicious.

I can't remember the details of the tools I had at that time.  But in 
general, I have in the past used quite a number of compilers (and 
libraries) that intentionally or unintentionally (i.e., toolchain bugs) 
do not follow the rules of C.  Some of the fun ones include:

Floating point only working for local variables, not statically 
allocated variables.

Uninitialised data that is not zeroed before main().

"const" being treated as meaning "in flash", and using different 
assembly instructions for access than ram data - thus casting a "char *" 
pointer to a "const char *" pointer will break the program.

Integer promotion rules aimed at being "helpful" to the programmer, 
rather than following the standards.

And of course, lots of bugs in compilers and libraries.  Many of the 
tools I have used have been small ones for odd microcontrollers - they 
simply haven't had the levels of use and testing of "big" compilers.


But perhaps the reason I explicitly added a terminating 0 to that macro 
was that when I wrote it, I didn't know the details of snprintf well enough!


>
>>> Incidentally, mixing the use of my_errPrintfBuffLen and sizeof buff is
>>> less the ideal.  I'd either use the defined constant only in the
>>> declaration and sizeof buff - 1 in both other places, or, if I felt the
>>> need to avoid confusing people with sizeof, I'd use the defined constant
>>> everywhere.
>>
>> Either method would work - I view the two expressions as synonymous and
>> interchangeable.  It might be a little different if the uses were
>> separated over a longer distance in code rather than being adjacent lines.
>>
>> And I know that some people think it is nice to write "sizeof(buff)",
>> others prefer "sizeof buff".  I'd rather not argue too much about style
>> here.
>
> Same here.  I wrote sizeof buff because that's how I write it.  I did
> not intend to suggest there was any reason to change or re-hash that old
> chestnut.  The mixing of the constant and sizeof is slightly more than
> style, though, since there's a small cognitive cost to checking the
> constant.  Even if defined just above, you need to check the spelling in
> both usages just in case there's some other similar #define for
> something else like my_logPrintBuffLen.
>

Fair 'nuff.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-07 12:20 +0430
  Re: How to fix: [-Wformat-nonliteral] Barry Schwarz <schwarzb@dqel.com> - 2017-04-07 01:28 -0700
  Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 11:58 +0200
    Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-07 11:43 +0100
      Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 13:35 +0200
    Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-07 13:50 +0100
      Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 15:54 +0200
        Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-07 19:35 +0100
          Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-08 13:41 +0200
            Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-10 13:52 -0700
              Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-11 00:18 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-10 15:43 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-10 16:10 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-10 16:26 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-11 11:33 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-11 08:37 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-13 00:20 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-12 15:55 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-12 18:17 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-14 12:27 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-14 11:57 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-17 18:08 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-17 09:38 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-12 18:13 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 07:37 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-13 08:43 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 11:24 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-13 11:49 -0700
                Re: How to fix: [-Wformat-nonliteral] Robert Wessel <robertwessel2@yahoo.com> - 2017-04-13 14:43 -0500
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 13:26 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-13 13:32 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 14:27 -0700
                Re: How to fix: [-Wformat-nonliteral] Robert Wessel <robertwessel2@yahoo.com> - 2017-04-13 17:39 -0500
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 16:16 -0700
                Re: How to fix: [-Wformat-nonliteral] Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-16 17:53 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 13:19 -0700
                Re: How to fix: [-Wformat-nonliteral] Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-16 17:49 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-17 18:14 +0200
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-17 11:54 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-17 22:41 +0200
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-17 15:02 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-14 12:40 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-14 12:10 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-14 13:22 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-14 17:14 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-13 00:20 +0200
      Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-07 20:29 +0430
        Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 18:42 +0200
          Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-08 08:04 +0430
            Re: How to fix: [-Wformat-nonliteral] Richard Heathfield <rjh@cpax.org.uk> - 2017-04-08 07:51 +0100
              Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-08 12:11 +0430
                Re: How to fix: [-Wformat-nonliteral] Richard Heathfield <rjh@cpax.org.uk> - 2017-04-08 11:38 +0100
                Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-08 18:52 +0100
            Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-08 13:48 +0200
              Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-08 20:34 +0430

csiph-web