Re: Fixes for compiler warnings

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: alanwli(at)gmail(dot)com
Cc: Gregory Stark <stark(at)enterprisedb(dot)com>, Magnus Hagander <magnus(at)hagander(dot)net>, Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Fixes for compiler warnings
Date: 2009-01-18 18:17:57
Message-ID: 4485.1232302677@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

alanwli(at)gmail(dot)com writes:
>> One thing to watch out for is that the intention may have been to allow
>> the strings to be translated.

> I'm not sure if that's the case. How does one find out?

If the origin of the "variable" format is a constant or set of constants
decorated with gettext_noop(), then this type of edit will have defeated
the intended localization. In the cases at hand, I believe that all but
one of your proposed patches break the desired behavior.

What's worse, I see that Magnus got there before you, and has broken
localization here and in several other places:
http://archives.postgresql.org/pgsql-committers/2008-11/msg00264.php

Magnus, you wanna clean up the mess? And what patch does the "few more"
comment refer back to?

A workable solution that both silences the warning and preserves
localizability is to follow a coding pattern like this:

const char *mymsg = gettext_noop("Some text to be localized.");

...

errmsg("%s", _(mymsg)) // not just errmsg(mymsg)

I would recommend that we do this, because otherwise we are certainly
going to have more breakage from well-intentioned patchers, whatever
Peter's opinion of the merits of the compiler warning might be ;-)

The really nasty cases are like this:

const char *myfmt = gettext_noop("Some bleat about object \"%s\".");

...

errmsg(myfmt, objectname)

where there really is no simple way to convince the compiler that you
know what you're doing without breaking functionality. This is probably
why -Wformat-security doesn't warn about the latter type of usage. It
does kind of beg the question of why bother with that warning though ...

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gregory Stark 2009-01-18 18:30:12 Re: Fixes for compiler warnings
Previous Message Heikki Linnakangas 2009-01-18 15:00:56 Re: Fixes for compiler warnings