From: Jason Merrill <jason@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: Enable -Walloc-size and -Wcalloc-transposed-args warnings for C++
Date: Wed, 20 Dec 2023 17:45:05 -0500 [thread overview]
Message-ID: <b38f3e24-56fc-4a1a-96d7-e746a328fcb7@redhat.com> (raw)
In-Reply-To: <ZYM+cZIJOmWDZTSH@tucnak>
On 12/20/23 14:20, Jakub Jelinek wrote:
> Hi!
>
> The following patch enables the -Walloc-size and -Wcalloc-transposed-args
> warnings for C++ as well.
>
> Ok for trunk if it passes bootstrap/regtest?
>
> 2023-12-20 Jakub Jelinek <jakub@redhat.com>
>
> gcc/c-family/
> * c.opt (Walloc-size): Enable also for C++ and ObjC++.
> gcc/cp/
> * cp-gimplify.cc (cp_genericize_r): If warn_alloc_size, call
> warn_for_alloc_size for -Walloc-size diagnostics.
> * semantics.cc (finish_call_expr): If warn_calloc_transposed_args,
> call warn_for_calloc for -Wcalloc-transposed-args diagnostics.
> gcc/testsuite/
> * g++.dg/warn/Walloc-size-1.C: New test.
> * g++.dg/warn/Wcalloc-transposed-args-1.C: New test.
>
> --- gcc/c-family/c.opt.jj 2023-12-20 11:31:07.897806698 +0100
> +++ gcc/c-family/c.opt 2023-12-20 20:02:36.889910599 +0100
> @@ -332,7 +332,7 @@ C ObjC C++ ObjC++ Var(warn_alloca) Warni
> Warn on any use of alloca.
>
> Walloc-size
> -C ObjC Var(warn_alloc_size) Warning LangEnabledBy(C ObjC, Wextra)
> +C ObjC C++ ObjC++ Var(warn_alloc_size) Warning LangEnabledBy(C ObjC C++ ObjC++, Wextra)
> Warn when allocating insufficient storage for the target type of the assigned pointer.
>
> Walloc-size-larger-than=
> --- gcc/cp/cp-gimplify.cc.jj 2023-12-15 10:08:53.877236695 +0100
> +++ gcc/cp/cp-gimplify.cc 2023-12-20 20:10:59.689914648 +0100
> @@ -2048,6 +2048,25 @@ cp_genericize_r (tree *stmt_p, int *walk
>
> case NOP_EXPR:
> *stmt_p = predeclare_vla (*stmt_p);
> +
> + /* Warn of new allocations that are not big enough for the target
> + type. */
> + if (warn_alloc_size
> + && TREE_CODE (TREE_OPERAND (stmt, 0)) == CALL_EXPR
> + && POINTER_TYPE_P (TREE_TYPE (stmt)))
> + {
> + if (tree fndecl = get_callee_fndecl (TREE_OPERAND (stmt, 0)))
> + if (DECL_IS_MALLOC (fndecl))
> + {
> + tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
> + tree alloc_size = lookup_attribute ("alloc_size", attrs);
> + if (alloc_size)
> + warn_for_alloc_size (EXPR_LOCATION (stmt),
> + TREE_TYPE (TREE_TYPE (stmt)),
> + TREE_OPERAND (stmt, 0), alloc_size);
> + }
> + }
> +
> if (!wtd->no_sanitize_p
> && sanitize_flags_p (SANITIZE_NULL | SANITIZE_ALIGNMENT)
> && TYPE_REF_P (TREE_TYPE (stmt)))
> --- gcc/cp/semantics.cc.jj 2023-12-14 07:49:53.150580801 +0100
> +++ gcc/cp/semantics.cc 2023-12-20 20:13:33.773772759 +0100
> @@ -2939,15 +2939,24 @@ finish_call_expr (tree fn, vec<tree, va_
>
> if (!result)
> {
> - if (warn_sizeof_pointer_memaccess
> + tree alloc_size_attr = NULL_TREE;
> + if (warn_calloc_transposed_args
> + && TREE_CODE (fn) == FUNCTION_DECL
> + && (alloc_size_attr
> + = lookup_attribute ("alloc_size",
> + TYPE_ATTRIBUTES (TREE_TYPE (fn)))))
> + if (TREE_VALUE (alloc_size_attr) == NULL_TREE
> + || TREE_CHAIN (TREE_VALUE (alloc_size_attr)) == NULL_TREE)
> + alloc_size_attr = NULL_TREE;
> + if ((warn_sizeof_pointer_memaccess || alloc_size_attr)
> && (complain & tf_warning)
> && !vec_safe_is_empty (*args)
> && !processing_template_decl)
> {
> - location_t sizeof_arg_loc[3];
> - tree sizeof_arg[3];
> + location_t sizeof_arg_loc[6];
> + tree sizeof_arg[6];
Why do we need to check 6 args for calloc? The patch is OK, just wondering.
Jason
next prev parent reply other threads:[~2023-12-20 22:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-20 19:20 Jakub Jelinek
2023-12-20 22:45 ` Jason Merrill [this message]
2023-12-20 23:29 ` Jakub Jelinek
2023-12-20 23:44 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b38f3e24-56fc-4a1a-96d7-e746a328fcb7@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).