Message ID | ae74c8a1-2268-76bd-2e9d-a11c2e1e46a0@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB77F3857C4D for <patchwork@sourceware.org>; Fri, 7 Jan 2022 18:34:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB77F3857C4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641580462; bh=YKyT+zcl3o3G0FEb97RKTJ+WvleaHznmSk1bqKNH5mM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Kc0LY9UlONs6Klt0GuhY9ImHDwzkTjVlEyYyncSfnCDQ8dibD7CX7hnkzL1YaKakp 89YC+VVs8JNsJv7bokLKHabCIwYv1Sa4o+8pstnJI5ATQZZaGxMz+y5zKrxnXaxJP5 8ZlFy/GqPa057WF2zBh/mLGAAQcsbtn02KN7Uv+A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 790093858D28; Fri, 7 Jan 2022 18:33:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 790093858D28 Received: by mail-wr1-x42e.google.com with SMTP id r10so5027077wrc.3; Fri, 07 Jan 2022 10:33:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=YKyT+zcl3o3G0FEb97RKTJ+WvleaHznmSk1bqKNH5mM=; b=JgvFNipsn0P/0X7nSFGAKfSWTyXHqRN8N/ge7m3q2v07j04JlxH8VYVYbytrJ5F1cj lLoJY83rxCxBCJSA3NkP23Z+6x0Run9q6HlQnErF99zPMlFJHjdwUQQ3am/+fEXFL/9K I/tdQFObnQJS+KkdJq9jll59p6K/yM3Aox+MHPmxQKgLMvxwlGGh91gcPiEtSFp7i1w1 SXB+yT1zYGtN5emiRnwKGxr2ouIa9DGbhHpV+efJqcQORztXcTmOfIAR8qKrSNbTtHYt B0ex1VjxWJrJtpfgggxhlZD00Kq9klPyPdQEzz060ivSYB1epI3JjI2dj4DdPAK9BiqF RVvQ== X-Gm-Message-State: AOAM531sBnHsvydS5g/5f3z5l0p0r6ZUMcVp9sUjLfQpsQOk+2GvoO2h 6gYXIqwxbWf7yuFDgE+7n40= X-Google-Smtp-Source: ABdhPJwbN6i4QKLpeHrlpU4vum/n1NFCQor87dkMa2rj1Bx0uVYUJ7a+cZUX8ksnQ1nmyVLhDBFdPw== X-Received: by 2002:a05:6000:107:: with SMTP id o7mr11929876wrx.546.1641580432439; Fri, 07 Jan 2022 10:33:52 -0800 (PST) Received: from [137.208.148.103] ([137.208.148.103]) by smtp.gmail.com with ESMTPSA id d11sm5774750wri.101.2022.01.07.10.33.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 07 Jan 2022 10:33:51 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] [12/11/10] Fix invalid format warnings on Windows Message-ID: <ae74c8a1-2268-76bd-2e9d-a11c2e1e46a0@gmail.com> Date: Fri, 7 Jan 2022 19:33:40 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Spam-Status: No, score=-8.0 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Tomas Kalibera via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Tomas Kalibera <tomas.kalibera@gmail.com> Cc: redi@gcc.gnu.org, joseph@codesourcery.com Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
None
|
|
Commit Message
Tomas Kalibera
Jan. 7, 2022, 6:33 p.m. UTC
Mingw32 targets use ms_printf format for printf, but mingw-w64 when configured for UCRT uses gnu_format (via stdio.h). GCC then checks both formats, which means that one cannot print a 64-bit integer without a warning. All these lines issue a warning: printf("Hello %"PRIu64"\n", x); // 1 printf("Hello %I64u\n", x); // 2 printf("Hello %llu\n", x); // 3 because each of them violates one of the formats. This causes trouble particularly for systems that turn warnings into errors or otherwise require no warnings (leading to the use of -Wno-format or of various printf replacements). Also, one gets a warning twice if the format string violates both formats. These issues have been reported as PR 95130 and PR 92292: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95130 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92292 This patch fixes these issues following the suggestion of Joseph Myers, it disables the built in format in case there are additional ones. It applies to GCC 12, 11, 10 and fixes the example above as tested on cross compilers built on Linux. I've also verified that R built using a 10.3 native compiler with the patch applied builds and passes its tests. I've updated the patch based on advice and comments from Martin Liska and Martin Storsjo. Could this or a variant of please be accepted to 12/11/10? Thanks Tomas gcc/c-family/ChangeLog: * c-common.c (check_function_arguments): Pass also function declaration to check_function_format. * c-common.h (check_function_format): Extra argument - function declaration. * c-format.c (check_function_format): For builtin functions with a built in format and at least one more, do not check the first one. /* See if this function has any format attributes. */ for (a = attrs; a; a = TREE_CHAIN (a)) @@ -1176,6 +1177,58 @@ check_function_format (const_tree fntype, tree attrs, int nargs, function_format_info info; decode_format_attr (fntype, atname, TREE_VALUE (a), &info, /*validated=*/true); + + /* Mingw32 targets have traditionally used ms_printf format for the + printf function, and this format is built in GCC. But nowadays, + if mingw-w64 is configured to target UCRT, the printf function + uses the gnu_printf format (specified in the stdio.h header). This + causes GCC to check both formats, which means that there is no way + to e.g. print a long long unsigned without a warning (ms_printf + warns for %llu and gnu_printf warns for %I64u). Also, GCC would warn + twice about the same issue when both formats are violated, e.g. + for %lu used to print long long unsigned. + + Hence, if there are multiple format specifiers, we skip the first + one. See PR 95130, PR 92292. */ + + if (!skipped_default_format && fndecl) + { + for(aa = TREE_CHAIN (a); aa; aa = TREE_CHAIN(aa)) + { + if (is_attribute_p ("format", get_attribute_name (aa)) && + fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) + { + switch (DECL_FUNCTION_CODE (fndecl)) + { + case BUILT_IN_FSCANF: + case BUILT_IN_PRINTF: + case BUILT_IN_SCANF: + case BUILT_IN_SNPRINTF: + case BUILT_IN_SSCANF: + case BUILT_IN_VFSCANF: + case BUILT_IN_VPRINTF: + case BUILT_IN_VSCANF: + case BUILT_IN_VSNPRINTF: + case BUILT_IN_VSSCANF: + case BUILT_IN_DCGETTEXT: + case BUILT_IN_DGETTEXT: + case BUILT_IN_GETTEXT: + case BUILT_IN_STRFMON: + case BUILT_IN_STRFTIME: + case BUILT_IN_SNPRINTF_CHK: + case BUILT_IN_VSNPRINTF_CHK: + case BUILT_IN_PRINTF_CHK: + case BUILT_IN_VPRINTF_CHK: + skipped_default_format = 1; + break; + default: + break; + } + } + } + if (skipped_default_format) continue; + } + if (warn_format) { /* FIXME: Rewrite all the internal functions in this file
Comments
Hello. I do support the patch, but I would ... On 1/7/22 19:33, Tomas Kalibera wrote: > + if (is_attribute_p ("format", get_attribute_name (aa)) && > + fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) > + { > + switch (DECL_FUNCTION_CODE (fndecl)) > + { > + case BUILT_IN_FSCANF: > + case BUILT_IN_PRINTF: > + case BUILT_IN_SCANF: > + case BUILT_IN_SNPRINTF: > + case BUILT_IN_SSCANF: > + case BUILT_IN_VFSCANF: > + case BUILT_IN_VPRINTF: > + case BUILT_IN_VSCANF: > + case BUILT_IN_VSNPRINTF: > + case BUILT_IN_VSSCANF: > + case BUILT_IN_DCGETTEXT: > + case BUILT_IN_DGETTEXT: > + case BUILT_IN_GETTEXT: > + case BUILT_IN_STRFMON: > + case BUILT_IN_STRFTIME: > + case BUILT_IN_SNPRINTF_CHK: > + case BUILT_IN_VSNPRINTF_CHK: > + case BUILT_IN_PRINTF_CHK: > + case BUILT_IN_VPRINTF_CHK: > + skipped_default_format = 1; > + break; > + default: > + break; > + } > + } ... skip this as the listed functions are only these that have defined ATTR_FORMAT_*: $ grep ATTR_FORMAT gcc/builtins.def DEF_LIB_BUILTIN (BUILT_IN_FSCANF, "fscanf", BT_FN_INT_FILEPTR_CONST_STRING_VAR, ATTR_FORMAT_SCANF_2_3) DEF_LIB_BUILTIN (BUILT_IN_PRINTF, "printf", BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_1_2) DEF_LIB_BUILTIN (BUILT_IN_SCANF, "scanf", BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_SCANF_1_2) DEF_C99_BUILTIN (BUILT_IN_SNPRINTF, "snprintf", BT_FN_INT_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_NOTHROW_3_4) DEF_LIB_BUILTIN (BUILT_IN_SSCANF, "sscanf", BT_FN_INT_CONST_STRING_CONST_STRING_VAR, ATTR_FORMAT_SCANF_NOTHROW_2_3) DEF_C99_BUILTIN (BUILT_IN_VFSCANF, "vfscanf", BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_2_0) DEF_LIB_BUILTIN (BUILT_IN_VPRINTF, "vprintf", BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_1_0) DEF_C99_BUILTIN (BUILT_IN_VSCANF, "vscanf", BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_1_0) DEF_C99_BUILTIN (BUILT_IN_VSNPRINTF, "vsnprintf", BT_FN_INT_STRING_SIZE_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_NOTHROW_3_0) DEF_C99_BUILTIN (BUILT_IN_VSSCANF, "vsscanf", BT_FN_INT_CONST_STRING_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_NOTHROW_2_0) DEF_EXT_LIB_BUILTIN (BUILT_IN_DCGETTEXT, "dcgettext", BT_FN_STRING_CONST_STRING_CONST_STRING_INT, ATTR_FORMAT_ARG_2) DEF_EXT_LIB_BUILTIN (BUILT_IN_DGETTEXT, "dgettext", BT_FN_STRING_CONST_STRING_CONST_STRING, ATTR_FORMAT_ARG_2) DEF_EXT_LIB_BUILTIN (BUILT_IN_GETTEXT, "gettext", BT_FN_STRING_CONST_STRING, ATTR_FORMAT_ARG_1) DEF_EXT_LIB_BUILTIN (BUILT_IN_STRFMON, "strfmon", BT_FN_SSIZE_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_STRFMON_NOTHROW_3_4) DEF_LIB_BUILTIN (BUILT_IN_STRFTIME, "strftime", BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_TM_PTR, ATTR_FORMAT_STRFTIME_NOTHROW_3_0) DEF_EXT_LIB_BUILTIN (BUILT_IN_SNPRINTF_CHK, "__snprintf_chk", BT_FN_INT_STRING_SIZE_INT_SIZE_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_NOTHROW_5_6) DEF_EXT_LIB_BUILTIN (BUILT_IN_VSNPRINTF_CHK, "__vsnprintf_chk", BT_FN_INT_STRING_SIZE_INT_SIZE_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_NOTHROW_5_0) DEF_EXT_LIB_BUILTIN (BUILT_IN_PRINTF_CHK, "__printf_chk", BT_FN_INT_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3) DEF_EXT_LIB_BUILTIN (BUILT_IN_VPRINTF_CHK, "__vprintf_chk", BT_FN_INT_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_2_0) Martin
On 1/11/22 2:37 PM, Martin Liška wrote: > Hello. > > I do support the patch, but I would ... Thanks, Martin, that makes the patch simpler and easier to maintain. Would the attached version do? Thanks Tomas > > On 1/7/22 19:33, Tomas Kalibera wrote: >> + if (is_attribute_p ("format", get_attribute_name (aa)) && >> + fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) >> + { >> + switch (DECL_FUNCTION_CODE (fndecl)) >> + { >> + case BUILT_IN_FSCANF: >> + case BUILT_IN_PRINTF: >> + case BUILT_IN_SCANF: >> + case BUILT_IN_SNPRINTF: >> + case BUILT_IN_SSCANF: >> + case BUILT_IN_VFSCANF: >> + case BUILT_IN_VPRINTF: >> + case BUILT_IN_VSCANF: >> + case BUILT_IN_VSNPRINTF: >> + case BUILT_IN_VSSCANF: >> + case BUILT_IN_DCGETTEXT: >> + case BUILT_IN_DGETTEXT: >> + case BUILT_IN_GETTEXT: >> + case BUILT_IN_STRFMON: >> + case BUILT_IN_STRFTIME: >> + case BUILT_IN_SNPRINTF_CHK: >> + case BUILT_IN_VSNPRINTF_CHK: >> + case BUILT_IN_PRINTF_CHK: >> + case BUILT_IN_VPRINTF_CHK: >> + skipped_default_format = 1; >> + break; >> + default: >> + break; >> + } >> + } > > ... skip this as the listed functions are only these that have defined > ATTR_FORMAT_*: > > $ grep ATTR_FORMAT gcc/builtins.def > DEF_LIB_BUILTIN (BUILT_IN_FSCANF, "fscanf", > BT_FN_INT_FILEPTR_CONST_STRING_VAR, ATTR_FORMAT_SCANF_2_3) > DEF_LIB_BUILTIN (BUILT_IN_PRINTF, "printf", > BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_1_2) > DEF_LIB_BUILTIN (BUILT_IN_SCANF, "scanf", > BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_SCANF_1_2) > DEF_C99_BUILTIN (BUILT_IN_SNPRINTF, "snprintf", > BT_FN_INT_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_NOTHROW_3_4) > DEF_LIB_BUILTIN (BUILT_IN_SSCANF, "sscanf", > BT_FN_INT_CONST_STRING_CONST_STRING_VAR, ATTR_FORMAT_SCANF_NOTHROW_2_3) > DEF_C99_BUILTIN (BUILT_IN_VFSCANF, "vfscanf", > BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_2_0) > DEF_LIB_BUILTIN (BUILT_IN_VPRINTF, "vprintf", > BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_1_0) > DEF_C99_BUILTIN (BUILT_IN_VSCANF, "vscanf", > BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_1_0) > DEF_C99_BUILTIN (BUILT_IN_VSNPRINTF, "vsnprintf", > BT_FN_INT_STRING_SIZE_CONST_STRING_VALIST_ARG, > ATTR_FORMAT_PRINTF_NOTHROW_3_0) > DEF_C99_BUILTIN (BUILT_IN_VSSCANF, "vsscanf", > BT_FN_INT_CONST_STRING_CONST_STRING_VALIST_ARG, > ATTR_FORMAT_SCANF_NOTHROW_2_0) > DEF_EXT_LIB_BUILTIN (BUILT_IN_DCGETTEXT, "dcgettext", > BT_FN_STRING_CONST_STRING_CONST_STRING_INT, ATTR_FORMAT_ARG_2) > DEF_EXT_LIB_BUILTIN (BUILT_IN_DGETTEXT, "dgettext", > BT_FN_STRING_CONST_STRING_CONST_STRING, ATTR_FORMAT_ARG_2) > DEF_EXT_LIB_BUILTIN (BUILT_IN_GETTEXT, "gettext", > BT_FN_STRING_CONST_STRING, ATTR_FORMAT_ARG_1) > DEF_EXT_LIB_BUILTIN (BUILT_IN_STRFMON, "strfmon", > BT_FN_SSIZE_STRING_SIZE_CONST_STRING_VAR, > ATTR_FORMAT_STRFMON_NOTHROW_3_4) > DEF_LIB_BUILTIN (BUILT_IN_STRFTIME, "strftime", > BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_TM_PTR, > ATTR_FORMAT_STRFTIME_NOTHROW_3_0) > DEF_EXT_LIB_BUILTIN (BUILT_IN_SNPRINTF_CHK, "__snprintf_chk", > BT_FN_INT_STRING_SIZE_INT_SIZE_CONST_STRING_VAR, > ATTR_FORMAT_PRINTF_NOTHROW_5_6) > DEF_EXT_LIB_BUILTIN (BUILT_IN_VSNPRINTF_CHK, "__vsnprintf_chk", > BT_FN_INT_STRING_SIZE_INT_SIZE_CONST_STRING_VALIST_ARG, > ATTR_FORMAT_PRINTF_NOTHROW_5_0) > DEF_EXT_LIB_BUILTIN (BUILT_IN_PRINTF_CHK, "__printf_chk", > BT_FN_INT_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3) > DEF_EXT_LIB_BUILTIN (BUILT_IN_VPRINTF_CHK, "__vprintf_chk", > BT_FN_INT_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_2_0) > > Martin
On 1/12/22 14:34, Tomas Kalibera wrote: > > On 1/11/22 2:37 PM, Martin Liška wrote: >> Hello. >> >> I do support the patch, but I would ... > > Thanks, Martin, that makes the patch simpler and easier to maintain. Would the attached version do? > > Thanks > Tomas > >> >> On 1/7/22 19:33, Tomas Kalibera wrote: >>> + if (is_attribute_p ("format", get_attribute_name (aa)) && >>> + fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) >>> + { >>> + switch (DECL_FUNCTION_CODE (fndecl)) >>> + { >>> + case BUILT_IN_FSCANF: >>> + case BUILT_IN_PRINTF: >>> + case BUILT_IN_SCANF: >>> + case BUILT_IN_SNPRINTF: >>> + case BUILT_IN_SSCANF: >>> + case BUILT_IN_VFSCANF: >>> + case BUILT_IN_VPRINTF: >>> + case BUILT_IN_VSCANF: >>> + case BUILT_IN_VSNPRINTF: >>> + case BUILT_IN_VSSCANF: >>> + case BUILT_IN_DCGETTEXT: >>> + case BUILT_IN_DGETTEXT: >>> + case BUILT_IN_GETTEXT: >>> + case BUILT_IN_STRFMON: >>> + case BUILT_IN_STRFTIME: >>> + case BUILT_IN_SNPRINTF_CHK: >>> + case BUILT_IN_VSNPRINTF_CHK: >>> + case BUILT_IN_PRINTF_CHK: >>> + case BUILT_IN_VPRINTF_CHK: >>> + skipped_default_format = 1; >>> + break; >>> + default: >>> + break; >>> + } >>> + } >> >> ... skip this as the listed functions are only these that have defined ATTR_FORMAT_*: >> >> $ grep ATTR_FORMAT gcc/builtins.def >> DEF_LIB_BUILTIN (BUILT_IN_FSCANF, "fscanf", BT_FN_INT_FILEPTR_CONST_STRING_VAR, ATTR_FORMAT_SCANF_2_3) >> DEF_LIB_BUILTIN (BUILT_IN_PRINTF, "printf", BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_1_2) >> DEF_LIB_BUILTIN (BUILT_IN_SCANF, "scanf", BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_SCANF_1_2) >> DEF_C99_BUILTIN (BUILT_IN_SNPRINTF, "snprintf", BT_FN_INT_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_NOTHROW_3_4) >> DEF_LIB_BUILTIN (BUILT_IN_SSCANF, "sscanf", BT_FN_INT_CONST_STRING_CONST_STRING_VAR, ATTR_FORMAT_SCANF_NOTHROW_2_3) >> DEF_C99_BUILTIN (BUILT_IN_VFSCANF, "vfscanf", BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_2_0) >> DEF_LIB_BUILTIN (BUILT_IN_VPRINTF, "vprintf", BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_1_0) >> DEF_C99_BUILTIN (BUILT_IN_VSCANF, "vscanf", BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_1_0) >> DEF_C99_BUILTIN (BUILT_IN_VSNPRINTF, "vsnprintf", BT_FN_INT_STRING_SIZE_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_NOTHROW_3_0) >> DEF_C99_BUILTIN (BUILT_IN_VSSCANF, "vsscanf", BT_FN_INT_CONST_STRING_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_NOTHROW_2_0) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_DCGETTEXT, "dcgettext", BT_FN_STRING_CONST_STRING_CONST_STRING_INT, ATTR_FORMAT_ARG_2) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_DGETTEXT, "dgettext", BT_FN_STRING_CONST_STRING_CONST_STRING, ATTR_FORMAT_ARG_2) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_GETTEXT, "gettext", BT_FN_STRING_CONST_STRING, ATTR_FORMAT_ARG_1) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_STRFMON, "strfmon", BT_FN_SSIZE_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_STRFMON_NOTHROW_3_4) >> DEF_LIB_BUILTIN (BUILT_IN_STRFTIME, "strftime", BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_TM_PTR, ATTR_FORMAT_STRFTIME_NOTHROW_3_0) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_SNPRINTF_CHK, "__snprintf_chk", BT_FN_INT_STRING_SIZE_INT_SIZE_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_NOTHROW_5_6) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_VSNPRINTF_CHK, "__vsnprintf_chk", BT_FN_INT_STRING_SIZE_INT_SIZE_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_NOTHROW_5_0) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_PRINTF_CHK, "__printf_chk", BT_FN_INT_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3) >> DEF_EXT_LIB_BUILTIN (BUILT_IN_VPRINTF_CHK, "__vprintf_chk", BT_FN_INT_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_2_0) >> >> Martin Few inline comments: > From 82a659c7e5b24bbd39ac567dff3f79cc4c1e083f Mon Sep 17 00:00:00 2001 > From: Tomas Kalibera <tomas.kalibera@gmail.com> > Date: Wed, 12 Jan 2022 08:17:21 -0500 > Subject: [PATCH] Mingw32 targets use ms_printf format for printf, but > mingw-w64 when configured for UCRT uses gnu_format (via stdio.h). GCC then > checks both formats, which means that one cannot print a 64-bit integer > without a warning. All these lines issue a warning: Please shorted the commit message's first line and put the rest to next lines. > > printf("Hello %"PRIu64"\n", x); > printf("Hello %I64u\n", x); > printf("Hello %llu\n", x); > > because each of them violates one of the formats. Also, one gets a warning > twice if the format string violates both formats. > > Fixed by disabling the built in format in case there are additional ones. > > gcc/c-family/ChangeLog: > > PR c/95130 > PR c/92292 > > * c-common.c (check_function_arguments): Pass also function > declaration to check_function_format. > > * c-common.h (check_function_format): Extra argument - function > declaration. > > * c-format.c (check_function_format): For builtin functions with a > built in format and at least one more, do not check the first one. > --- > gcc/c-family/c-common.c | 2 +- > gcc/c-family/c-common.h | 2 +- > gcc/c-family/c-format.c | 31 +++++++++++++++++++++++++++++-- > 3 files changed, 31 insertions(+), 4 deletions(-) > > diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c > index 4a6a4edb763..00fc734d28e 100644 > --- a/gcc/c-family/c-common.c > +++ b/gcc/c-family/c-common.c > @@ -6064,7 +6064,7 @@ check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, > /* Check for errors in format strings. */ > > if (warn_format || warn_suggest_attribute_format) > - check_function_format (fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray, > + check_function_format (fndecl, fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray, > arglocs); > > if (warn_format) > diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h > index 8b7bf35e888..ee370eafbbc 100644 > --- a/gcc/c-family/c-common.h > +++ b/gcc/c-family/c-common.h > @@ -856,7 +856,7 @@ extern void check_function_arguments_recurse (void (*) > unsigned HOST_WIDE_INT); > extern bool check_builtin_function_arguments (location_t, vec<location_t>, > tree, tree, int, tree *); > -extern void check_function_format (const_tree, tree, int, tree *, > +extern void check_function_format (const_tree, const_tree, tree, int, tree *, > vec<location_t> *); > extern bool attribute_fallthrough_p (tree); > extern tree handle_format_attribute (tree *, tree, tree, int, bool *); > diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c > index afa77810a5c..da72d85f66e 100644 > --- a/gcc/c-family/c-format.c > +++ b/gcc/c-family/c-format.c > @@ -1160,12 +1160,13 @@ decode_format_type (const char *s, bool *is_raw /* = NULL */) > attribute themselves. */ > > void > -check_function_format (const_tree fntype, tree attrs, int nargs, > +check_function_format (const_tree fndecl, const_tree fntype, tree attrs, int nargs, > tree *argarray, vec<location_t> *arglocs) > { > - tree a; > + tree a, aa; > > tree atname = get_identifier ("format"); > + int skipped_default_format = 0; Use bool type: bool skipped_default_format = false; > > /* See if this function has any format attributes. */ > for (a = attrs; a; a = TREE_CHAIN (a)) > @@ -1176,6 +1177,32 @@ check_function_format (const_tree fntype, tree attrs, int nargs, > function_format_info info; > decode_format_attr (fntype, atname, TREE_VALUE (a), &info, > /*validated=*/true); > + > + /* Mingw32 targets have traditionally used ms_printf format for the > + printf function, and this format is built in GCC. But nowadays, > + if mingw-w64 is configured to target UCRT, the printf function > + uses the gnu_printf format (specified in the stdio.h header). This > + causes GCC to check both formats, which means that there is no way > + to e.g. print a long long unsigned without a warning (ms_printf > + warns for %llu and gnu_printf warns for %I64u). Also, GCC would warn > + twice about the same issue when both formats are violated, e.g. > + for %lu used to print long long unsigned. > + > + Hence, if there are multiple format specifiers, we skip the first > + one. See PR 95130, PR 92292. */ > + > + if (!skipped_default_format && fndecl) > + { > + for(aa = TREE_CHAIN (a); aa; aa = TREE_CHAIN(aa)) > + if (is_attribute_p ("format", get_attribute_name (aa)) && > + fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) > + { > + skipped_default_format = 1; > + break; > + } > + if (skipped_default_format) continue; continue on next line please. Apart from that, I support the patch (I cannot approve it). Note we're now approaching stage4 and this is definitelly a stage1 material (opens after GCC 12.1.0 gets released). Cheers, Martin > + } > + > if (warn_format) > { > /* FIXME: Rewrite all the internal functions in this file > -- > 2.25.1 >
On 1/13/22 10:40 AM, Martin Liška wrote: [...] > Apart from that, I support the patch (I cannot approve it). Note we're > now approaching > stage4 and this is definitelly a stage1 material (opens after GCC > 12.1.0 gets released). Thanks, Martin, I've updated the patch following your suggestions. Cheers Tomas > > Cheers, > Martin >
On 1/13/22 12:00, Tomas Kalibera wrote: > On 1/13/22 10:40 AM, Martin Liška wrote: > > [...] >> Apart from that, I support the patch (I cannot approve it). Note we're now approaching >> stage4 and this is definitelly a stage1 material (opens after GCC 12.1.0 gets released). > > Thanks, Martin, I've updated the patch following your suggestions. > > Cheers > Tomas > > >> >> Cheers, >> Martin >> May I please ping review for this? Cheers, Martin
On Wed, 11 May 2022, Martin Liška wrote:
> May I please ping review for this?
There are various coding style issues in the patch; at least missing space
before '(' and '&&' at end of line (should be at start of line). It will
also need to be updated for .c files having been renamed to .cc in the GCC
source tree.
I'd also like to check that "if mingw-w64 is configured to target UCRT" is
not something that is necessarily known when GCC is built or from the
command-line options passed to GCC. Because ideally one might expect the
TARGET_OVERRIDES_FORMAT_ATTRIBUTES / TARGET_OVERRIDES_FORMAT_INIT
definitions to handle things appropriately conditionally, so that printf
attributes are handled as gnu_printf for the "if mingw-w64 is configured
to target UCRT" case. Disregarding a built-in format attribute when one
is also specified explicitly in the header, even though the two are not
exactly equivalent attributes, as in this patch, seems more like the right
approach in the case where the attributes in installed header (at the time
GCC is run, not the time it is built) *are* the way in which GCC gets the
"configured to target UCRT" information - as opposed to it being something
available before the header is parsed.
On Wed, 11 May 2022, Joseph Myers wrote: > I'd also like to check that "if mingw-w64 is configured to target UCRT" is > not something that is necessarily known when GCC is built or from the > command-line options passed to GCC. Because ideally one might expect the > TARGET_OVERRIDES_FORMAT_ATTRIBUTES / TARGET_OVERRIDES_FORMAT_INIT > definitions to handle things appropriately conditionally, so that printf > attributes are handled as gnu_printf for the "if mingw-w64 is configured > to target UCRT" case. Disregarding a built-in format attribute when one > is also specified explicitly in the header, even though the two are not > exactly equivalent attributes, as in this patch, seems more like the right > approach in the case where the attributes in installed header (at the time > GCC is run, not the time it is built) *are* the way in which GCC gets the > "configured to target UCRT" information - as opposed to it being something > available before the header is parsed. Indeed, the "configured to target X" information isn't available at the point when GCC is built - that gets set afterwards. And even while it is usually mostly fixed soon afterwards, you can even (with some amount of gotchas) change what CRT to build for by passing the appropriate defines that picks a different default in the mingw-w64 headers. Anyway - from the GCC point of view, it's not fixed, and whatever the parsed headers say, is the only thing that can be relied upon. So I think the approach of the patch is the right one, code style/issues notwithstanding. // Martin
On 5/11/22 18:43, Joseph Myers wrote: > There are various coding style issues in the patch; at least missing space > before '(' and '&&' at end of line (should be at start of line). It will > also need to be updated for .c files having been renamed to .cc in the GCC > source tree. Thanks, I've fixed the formatting issue and updated the patch for master, 12, 11 and 10. In addition to the renaming of .c to .cc files, there was also a change in the first argument of check_function_format. I've also removed a duplicated check for whether fndecl was null and fixed indentation. I've updated the patches for each version to also note that in c51f1e7427e6a5ae2a6d82b5a790df77a3adc99a gcc: Add `ll` and `L` length modifiers for `ms_printf` the ms_printf format has been taught to support (not warn about) printing the 64-bit integers using the "%ll" modifier (currently GCC 11 and newer). However, I assume there may be other differences between the ms_printf and gnu_printf formats people might run into, so it still makes sense to fix this not only in GCC 10, but also in newer versions. Furthermore, the attached patch is still needed (GCC 11, GCC 12, master) to get rid of duplicate warnings for an incorrect format (e.g. "%lu" used to print "unsigned long long"), when both ms_printf and gnu_printf formats are violated (PR 92292). Best Tomas
On 5/16/2022 5:27 AM, Tomas Kalibera via Gcc-patches wrote: > > On 5/11/22 18:43, Joseph Myers wrote: >> There are various coding style issues in the patch; at least missing >> space >> before '(' and '&&' at end of line (should be at start of line). It >> will >> also need to be updated for .c files having been renamed to .cc in >> the GCC >> source tree. > > Thanks, I've fixed the formatting issue and updated the patch for > master, 12, 11 and 10. In addition to the renaming of .c to .cc files, > there was also a change in the first argument of > check_function_format. I've also removed a duplicated check for > whether fndecl was null and fixed indentation. > > I've updated the patches for each version to also note that in > > c51f1e7427e6a5ae2a6d82b5a790df77a3adc99a > gcc: Add `ll` and `L` length modifiers for `ms_printf` > > the ms_printf format has been taught to support (not warn about) > printing the 64-bit integers using the "%ll" modifier (currently GCC > 11 and newer). However, I assume there may be other differences > between the ms_printf and gnu_printf formats people might run into, so > it still makes sense to fix this not only in GCC 10, but also in newer > versions. > > Furthermore, the attached patch is still needed (GCC 11, GCC 12, > master) to get rid of duplicate warnings for an incorrect format (e.g. > "%lu" used to print "unsigned long long"), when both ms_printf and > gnu_printf formats are violated (PR 92292). I guess we're going to depend on the builtin-format always appearing first in the chain? While it's probably true in practice, I doubt we really want to depend on that. Is there any sensible way to distinguish between the builtin format and one that comes from the source? There's a trivial formatting nit: > + for(aa = TREE_CHAIN (a); aa; aa = TREE_CHAIN (aa)) Space between the "for" and its open paren. But I think the big question here is whether or not we want to assume the builtin format is always first on the chain. jeff
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 13341fa315e..be4d8400447 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -6057,7 +6057,7 @@ check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, /* Check for errors in format strings. */ if (warn_format || warn_suggest_attribute_format) - check_function_format (fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray, + check_function_format (fndecl, fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray, arglocs); if (warn_format) diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 8b7bf35e888..ee370eafbbc 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -856,7 +856,7 @@ extern void check_function_arguments_recurse (void (*) unsigned HOST_WIDE_INT); extern bool check_builtin_function_arguments (location_t, vec<location_t>, tree, tree, int, tree *); -extern void check_function_format (const_tree, tree, int, tree *, +extern void check_function_format (const_tree, const_tree, tree, int, tree *, vec<location_t> *); extern bool attribute_fallthrough_p (tree); extern tree handle_format_attribute (tree *, tree, tree, int, bool *); diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index afa77810a5c..8155ee8c6f2 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -1160,12 +1160,13 @@ decode_format_type (const char *s, bool *is_raw /* = NULL */) attribute themselves. */ void -check_function_format (const_tree fntype, tree attrs, int nargs, +check_function_format (const_tree fndecl, const_tree fntype, tree attrs, int nargs, tree *argarray, vec<location_t> *arglocs) { - tree a; + tree a, aa; tree atname = get_identifier ("format"); + int skipped_default_format = 0;