This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: -Wformat-security warnings generated in gcc build
- From: Prathamesh Kulkarni <bilbotheelffriend at gmail dot com>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: gcc <gcc at gcc dot gnu dot org>
- Date: Wed, 22 Jan 2014 20:59:47 +0530
- Subject: Re: -Wformat-security warnings generated in gcc build
- Authentication-results: sourceware.org; auth=none
- References: <CAJXstsAMh25uwq5tCL86izSEMyvVtbUj4YNrmbZpFoFNVD=1Mg at mail dot gmail dot com> <Pine dot LNX dot 4 dot 64 dot 1401211749001 dot 20755 at digraph dot polyomino dot org dot uk>
On Tue, Jan 21, 2014 at 11:20 PM, Joseph S. Myers
<joseph@codesourcery.com> wrote:
> On Tue, 21 Jan 2014, Prathamesh Kulkarni wrote:
>
>> Souce of these warnings are typically calls to error() and friends.
>> In C and C++ front ends there are many calls of error (errmsg).
>> errmsg is in many cases, assigned the return value of targetm hooks
>> (tagetm.invalid_return_type(), etc.) Is it correct to replace error
>> (errmsg) by
>> error ("%s", errmsg) in these cases ?
>
> No. Typically the message returned by the hook may contain no-arguments
> format specifiers such as %< and %>. Instead, to avoid such warnings you
> need to add a new function error_at_no_args (location, message) that
> accepts and processes only formats taking no arguments (and probably
> aborts if given a format that needs arguments).
>
Here's an initial try:
void
error_at_no_args (location_t loc, const char *gmsgid)
{
diagnostic_info diagnostic;
diagnostic_set_info (&diagnostic, gmsgid, NULL, loc, DK_ERROR);
report_diagnostic (&diagnostic);
}
I guess this shall work fine for no-argument specifiers ? (I tested it
with %%, %m, %', %<, %>)
However this causes segmentation fault, when format string contains
argument specifier (which is anyways incorrect), I believe that's
because I am passing NULL in place of argument pointer.
Unfortunately, I am not clear on how to check for format specifiers in string.
Should I do it manually by checking the format string for specifiers
and call abort if found a no-argument specifier,
or is there a better way to do it ?
Thanks!
> --
> Joseph S. Myers
> joseph@codesourcery.com