This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/39438] Can't compile a wrapper around strftime with -Werror=format-nonliteral


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=39438

Manuel LÃpez-IbÃÃez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-02-27
     Ever confirmed|0                           |1

--- Comment #10 from Manuel LÃpez-IbÃÃez <manu at gcc dot gnu.org> ---
(In reply to D. Hugh Redelmeier from comment #6)
> If an arg is marked as a const char * (i.e. unchanging) and has the strftime
> format attribute, it should be accepted as if it were a literal strftime
> argument.  After all, the necessary checking would have been done at this
> routine's points of call.

The code that warns is this: gcc/c-family/c-format.c

    if (res.number_non_literal > 0)
      {
        /* Functions taking a va_list normally pass a non-literal format
           string.  These functions typically are declared with
           first_arg_num == 0, so avoid warning in those cases.  */
        if (!(format_types[info->format_type].flags & (int)
FMT_FLAG_ARG_CONVERT))
          {
            /* For strftime-like formats, warn for not checking the format
               string; but there are no arguments to check.  */
            warning_at (loc, OPT_Wformat_nonliteral,
                        "format not a string literal, format string not
checked");
          }

Thus, there is nothing checking the attributes of the caller. In the same file
there is this code:

                tree c;
                for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
                     c;
                     c = TREE_CHAIN (c))
                  if (is_attribute_p ("format", TREE_PURPOSE (c))
                      && (decode_format_type (IDENTIFIER_POINTER
                                              (TREE_VALUE (TREE_VALUE (c))))
                          == info.format_type))
                    break;
                if (c == NULL_TREE)
                  {
                    /* Check if the current function has a parameter to which
                       the format attribute could be attached; if not, it
                       can't be a candidate for a format attribute, despite
                       the vprintf-like or vscanf-like call.  */
                    tree args;
                    for (args = DECL_ARGUMENTS (current_function_decl);
                         args != 0;
                         args = DECL_CHAIN (args))
                      {
                        if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE
                            && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE
(args)))
                                == char_type_node))
                          break;
                      }
                    if (args != 0)
                      warning (OPT_Wsuggest_attribute_format, "function might "
                               "be possible candidate for %qs format
attribute",
                               format_types[info.format_type].name);
                  }

I'm confident that the above code can be adjusted and moved to the point of the
warning in order to check that the caller of strftime has an attribute format
of type 'strftime' that refers to the same declaration as the nonliteral format
argument in the call to strftime, then avoid warning. I'm not planning to work
on this though.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]