From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 7E02C3858D38 for ; Tue, 19 Dec 2023 07:11:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7E02C3858D38 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7E02C3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702969880; cv=none; b=u1feoMETltCEgpiFo69BI+6LPOvaGQRMP4WFw9Ew9oD03XjWqu9hHj8xPsLjM8GWTc+ekV3QCDg39/T4TxhxVLUvp1dV6uphkI70ahm417UX246OobCDZrbb6fiuOkOAzGxwg3/B2k8cexrBoIiYk+KIUeWmjRwKve2w7avL9Hs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702969880; c=relaxed/simple; bh=/ir7NRGxVJXgQnKRRlL1W+ayZswuOeByPNI9CLAgdLg=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=L/kXZQy91Eh0I5a+pII20wZS+ogxAOrRyDJ6LETH1GsZRORRvCkA5yV2kko8AGBlalb5M1uhgXsYQNFEUUjMzH+U5MM6c1iB9ZxVpLG3bxztIsxM4+kgM4JjVB+1YpocCrQgb2zo4Zb/nRQZzLA5S7mzPuWAk6E9RwhMCjW/d84= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [192.168.0.221] (84-115-220-210.cable.dynamic.surfer.at [84.115.220.210]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4SvSXb2nv4z3wVP; Tue, 19 Dec 2023 08:11:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1702969872; bh=DIUwrT9XR0ZeYVUK0E6B0mKJ0RbcwKfymDLyuGe7zmM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=eVmILO22ZUG6g+fq1f74qxm7IN8oRMfim0pxWYUOOqh2w1U1xVaoUTaHiNOhXQL2/ 5IwZjU8XxMgwMKGaFI7w9Cb85rnp225xD/e4IhfuzMSNkqJRpdPpdng/D8pIXFtipx mzRhhYlJ5gAo03qu5giDo4v4dih4BX/F7JdPTZ4k= Message-ID: Subject: Re: [PATCH] c: Split -Wcalloc-transposed-args warning from -Walloc-size, -Walloc-size fixes From: Martin Uecker To: Jakub Jelinek , "Joseph S. Myers" , Marek Polacek , Jason Merrill , Richard Biener Cc: gcc-patches@gcc.gnu.org Date: Tue, 19 Dec 2023 08:11:11 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Am Montag, dem 18.12.2023 um 20:14 +0100 schrieb Jakub Jelinek: > Hi! >=20 > The following patch changes -Walloc-size warning to no longer warn > about int *p =3D calloc (1, sizeof (int));, because as discussed earlier, > the size is IMNSHO sufficient in that case, for alloc_size with 2 > arguments warns if the product of the 2 arguments is insufficiently small= . >=20 > Also, it warns also for explicit casts of malloc/calloc etc. calls > rather than just implicit, so not just > int *p =3D malloc (1); > but also > int *p =3D (int *) malloc (1); >=20 > It also fixes some ICEs where the code didn't verify the alloc_size > arguments properly (Walloc-size-5.c testcase ICEs with vanilla trunk). >=20 > And lastly, it introduces a coding style warning, -Wcalloc-transposed-arg= s > to warn for calloc (sizeof (struct S), 1) and similar calls (regardless > of what they are cast to, warning whenever first argument is sizeof and > the second is not). I would generally see function arguments that are swapped relative to the documented ABI as more than a coding style issue even in=C2=A0 cases where it can be expected to make no difference. >=20 > Ok for trunk if this passes bootstrap/regtest? I wonder whether we could turn on -Walloc-size for -Wall with this change? Martin >=20 > If yes, I'd implement it for C++ next. > If not, we should at least fix the ICEs. >=20 > 2023-12-18 Jakub Jelinek >=20 > gcc/ > * doc/invoke.texi (-Walloc-size): Add to the list of > warning options, remove unnecessary line-break. > (-Wcalloc-transposed-args): Document new warning. > gcc/c-family/ > * c.opt (Wcalloc-transposed-args): New warning. > * c-common.h (warn_for_calloc, warn_for_alloc_size): Declare. > * c-warn.cc (warn_for_calloc, warn_for_alloc_size): New functions. > gcc/c/ > * c-parser.cc (c_parser_postfix_expression_after_primary): Grow > sizeof_arg and sizeof_arg_loc arrays to 6 elements. Call > warn_for_calloc if warn_calloc_transposed_args for functions with > alloc_size type attribute with 2 arguments. > (c_parser_expr_list): Use 6 instead of 3. > * c-typeck.cc (build_c_cast): Call warn_for_alloc_size for casts > of calls to functions with alloc_size type attribute. > (convert_for_assignment): Likewise. > gcc/testsuite/ > * gcc.dg/Walloc-size-4.c: New test. > * gcc.dg/Walloc-size-5.c: New test. > * gcc.dg/Wcalloc-transposed-args-1.c: New test. >=20 > --- gcc/doc/invoke.texi.jj 2023-12-18 09:39:49.411355496 +0100 > +++ gcc/doc/invoke.texi 2023-12-18 19:59:37.139525128 +0100 > @@ -328,7 +328,7 @@ Objective-C and Objective-C++ Dialects}. > -pedantic-errors -fpermissive > -w -Wextra -Wall -Wabi=3D@var{n} > -Waddress -Wno-address-of-packed-member -Waggregate-return > --Walloc-size-larger-than=3D@var{byte-size} -Walloc-zero > +-Walloc-size -Walloc-size-larger-than=3D@var{byte-size} -Walloc-zero > -Walloca -Walloca-larger-than=3D@var{byte-size} > -Wno-aggressive-loop-optimizations > -Warith-conversion > @@ -344,6 +344,7 @@ Objective-C and Objective-C++ Dialects}. > -Wc++20-compat > -Wno-c++11-extensions -Wno-c++14-extensions -Wno-c++17-extensions > -Wno-c++20-extensions -Wno-c++23-extensions > +-Wcalloc-transposed-args > -Wcast-align -Wcast-align=3Dstrict -Wcast-function-type -Wcast-qual > -Wchar-subscripts > -Wclobbered -Wcomment > @@ -8260,8 +8261,7 @@ Warn about calls to allocation functions > @code{alloc_size} that specify insufficient size for the target type of > the pointer the result is assigned to, including those to the built-in > forms of the functions @code{aligned_alloc}, @code{alloca}, > -@code{calloc}, > -@code{malloc}, and @code{realloc}. > +@code{calloc}, @code{malloc}, and @code{realloc}. > =20 > @opindex Wno-alloc-zero > @opindex Walloc-zero > @@ -8274,6 +8274,21 @@ when called with a zero size differs amo > of @code{realloc} has been deprecated) relying on it may result in subtl= e > portability bugs and should be avoided. > =20 > +@opindex Wcalloc-transposed-args > +@opindex Wno-calloc-transposed-args > +@item -Wcalloc-transposed-args > +Warn about calls to allocation functions decorated with attribute > +@code{alloc_size} with two arguments, which use @code{sizeof} operator > +as the earlier size argument and don't use it as the later size argument= . > +This is a coding style warning. The first argument to @code{calloc} is > +documented to be number of elements in array, while the second argument > +is size of each element, so @code{calloc (@var{n}, sizeof (int))} is pre= ferred > +over @code{calloc (sizeof (int), @var{n})}. If @code{sizeof} in the ear= lier > +argument and not the latter is intentional, the warning can be suppresse= d > +by using @code{calloc (sizeof (struct @var{S}) + 0, n)} or > +@code{calloc (1 * sizeof (struct @var{S}), 4)} or using @code{sizeof} in= the > +later argument as well. > + > @opindex Walloc-size-larger-than=3D > @opindex Wno-alloc-size-larger-than > @item -Walloc-size-larger-than=3D@var{byte-size} > --- gcc/c-family/c.opt.jj 2023-12-14 07:49:52.951583511 +0100 > +++ gcc/c-family/c.opt 2023-12-18 13:57:11.834184689 +0100 > @@ -502,6 +502,10 @@ Wc++26-extensions > C++ ObjC++ Var(warn_cxx26_extensions) Warning Init(1) > Warn about C++26 constructs in code compiled with an older standard. > =20 > +Wcalloc-transposed-args > +C ObjC C++ ObjC++ Var(warn_calloc_transposed_args) Warning LangEnabledBy= (C ObjC C++ ObjC++,Wextra) > +Warn about suspicious calls to calloc-like functions where sizeof expres= sion is the earlier size argument and not the latter. > + > Wcast-function-type > C ObjC C++ ObjC++ Var(warn_cast_function_type) Warning EnabledBy(Wextra) > Warn about casts between incompatible function types. > --- gcc/c-family/c-common.h.jj 2023-12-14 07:49:52.915584002 +0100 > +++ gcc/c-family/c-common.h 2023-12-18 16:26:40.420196735 +0100 > @@ -1599,6 +1599,9 @@ extern void warn_about_parentheses (loca > extern void warn_for_unused_label (tree label); > extern void warn_for_div_by_zero (location_t, tree divisor); > extern void warn_for_memset (location_t, tree, tree, int); > +extern void warn_for_calloc (location_t *, tree, vec *, > + tree *, tree); > +extern void warn_for_alloc_size (location_t, tree, tree, tree); > extern void warn_for_sign_compare (location_t, > tree orig_op0, tree orig_op1, > tree op0, tree op1, > --- gcc/c-family/c-warn.cc.jj 2023-12-14 07:49:52.951583511 +0100 > +++ gcc/c-family/c-warn.cc 2023-12-18 19:30:47.285607029 +0100 > @@ -2263,6 +2263,76 @@ warn_for_memset (location_t loc, tree ar > } > } > =20 > +/* Warn for calloc (sizeof (X), n). */ > + > +void > +warn_for_calloc (location_t *sizeof_arg_loc, tree callee, > + vec *params, tree *sizeof_arg, tree attr) > +{ > + if (!TREE_VALUE (attr) || !TREE_CHAIN (TREE_VALUE (attr))) > + return; > + > + int arg1 =3D TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr))) - 1; > + int arg2 > + =3D TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (TREE_VALUE (attr)))) -= 1; > + if (arg1 < 0 > + || (unsigned) arg1 >=3D vec_safe_length (params) > + || arg1 >=3D 6 > + || arg2 < 0 > + || (unsigned) arg2 >=3D vec_safe_length (params) > + || arg2 >=3D 6 > + || arg1 >=3D arg2) > + return; > + > + if (sizeof_arg[arg1] =3D=3D NULL_TREE || sizeof_arg[arg2] !=3D NULL_TR= EE) > + return; > + > + if (warning_at (sizeof_arg_loc[arg1], OPT_Wcalloc_transposed_args, > + "%qD sizes specified with % in the earlier " > + "argument and not in the later argument", callee)) > + inform (sizeof_arg_loc[arg1], "earlier argument should specify numbe= r " > + "of elements, later size of each element"); > +} > + > +/* Warn for allocator calls where the constant allocated size is smaller > + than sizeof (TYPE). */ > + > +void > +warn_for_alloc_size (location_t loc, tree type, tree call, tree alloc_si= ze) > +{ > + if (!TREE_VALUE (alloc_size)) > + return; > + > + tree arg1 =3D TREE_VALUE (TREE_VALUE (alloc_size)); > + int idx1 =3D TREE_INT_CST_LOW (arg1) - 1; > + if (idx1 < 0 || idx1 >=3D call_expr_nargs (call)) > + return; > + arg1 =3D CALL_EXPR_ARG (call, idx1); > + if (TREE_CODE (arg1) !=3D INTEGER_CST) > + return; > + if (TREE_CHAIN (TREE_VALUE (alloc_size))) > + { > + tree arg2 =3D TREE_VALUE (TREE_CHAIN (TREE_VALUE (alloc_size))); > + int idx2 =3D TREE_INT_CST_LOW (arg2) - 1; > + if (idx2 < 0 || idx2 >=3D call_expr_nargs (call)) > + return; > + arg2 =3D CALL_EXPR_ARG (call, idx2); > + if (TREE_CODE (arg2) !=3D INTEGER_CST) > + return; > + arg1 =3D int_const_binop (MULT_EXPR, fold_convert (sizetype, arg1)= , > + fold_convert (sizetype, arg2)); > + if (TREE_CODE (arg1) !=3D INTEGER_CST) > + return; > + } > + if (!VOID_TYPE_P (type) > + && TYPE_SIZE_UNIT (type) > + && TREE_CODE (TYPE_SIZE_UNIT (type)) =3D=3D INTEGER_CST > + && tree_int_cst_lt (arg1, TYPE_SIZE_UNIT (type))) > + warning_at (loc, OPT_Walloc_size, > + "allocation of insufficient size %qE for type %qT with " > + "size %qE", arg1, type, TYPE_SIZE_UNIT (type)); > +} > + > /* Subroutine of build_binary_op. Give warnings for comparisons > between signed and unsigned quantities that may fail. Do the > checking based on the original operand trees ORIG_OP0 and ORIG_OP1, > --- gcc/c/c-parser.cc.jj 2023-12-14 07:49:52.969583266 +0100 > +++ gcc/c/c-parser.cc 2023-12-18 19:24:01.508298779 +0100 > @@ -12478,8 +12478,8 @@ c_parser_postfix_expression_after_primar > { > struct c_expr orig_expr; > tree ident, idx; > - location_t sizeof_arg_loc[3], comp_loc; > - tree sizeof_arg[3]; > + location_t sizeof_arg_loc[6], comp_loc; > + tree sizeof_arg[6]; > unsigned int literal_zero_mask; > unsigned int i; > vec *exprlist; > @@ -12512,7 +12512,7 @@ c_parser_postfix_expression_after_primar > { > matching_parens parens; > parens.consume_open (parser); > - for (i =3D 0; i < 3; i++) > + for (i =3D 0; i < 6; i++) > { > sizeof_arg[i] =3D NULL_TREE; > sizeof_arg_loc[i] =3D UNKNOWN_LOCATION; > @@ -12577,6 +12577,13 @@ c_parser_postfix_expression_after_primar > "not permitted in intervening code"); > parser->omp_for_parse_state->fail =3D true; > } > + if (warn_calloc_transposed_args) > + if (tree attr =3D lookup_attribute ("alloc_size", > + TYPE_ATTRIBUTES > + (TREE_TYPE (expr.value)))) > + if (TREE_VALUE (attr) && TREE_CHAIN (TREE_VALUE (attr))) > + warn_for_calloc (sizeof_arg_loc, expr.value, exprlist, > + sizeof_arg, attr); > } > =20 > start =3D expr.get_start (); > @@ -12861,7 +12868,7 @@ c_parser_expr_list (c_parser *parser, bo > vec_safe_push (orig_types, expr.original_type); > if (locations) > locations->safe_push (expr.get_location ()); > - if (++idx < 3 > + if (++idx < 6 > && sizeof_arg !=3D NULL > && (expr.original_code =3D=3D SIZEOF_EXPR > || expr.original_code =3D=3D PAREN_SIZEOF_EXPR)) > --- gcc/c/c-typeck.cc.jj 2023-12-14 07:49:52.990582980 +0100 > +++ gcc/c/c-typeck.cc 2023-12-18 19:01:40.013746860 +0100 > @@ -6054,6 +6054,19 @@ build_c_cast (location_t loc, tree type, > c_addr_space_name (as_to), > c_addr_space_name (as_from)); > } > + > + /* Warn of new allocations that are not big enough for the target > + type. */ > + if (warn_alloc_size && TREE_CODE (value) =3D=3D CALL_EXPR) > + if (tree fndecl =3D get_callee_fndecl (value)) > + if (DECL_IS_MALLOC (fndecl)) > + { > + tree attrs =3D TYPE_ATTRIBUTES (TREE_TYPE (fndecl)); > + tree alloc_size =3D lookup_attribute ("alloc_size", attrs); > + if (alloc_size) > + warn_for_alloc_size (loc, TREE_TYPE (type), value, > + alloc_size); > + } > } > =20 > /* Warn about possible alignment problems. */ > @@ -7277,32 +7290,15 @@ convert_for_assignment (location_t locat > =20 > /* Warn of new allocations that are not big enough for the target > type. */ > - tree fndecl; > - if (warn_alloc_size > - && TREE_CODE (rhs) =3D=3D CALL_EXPR > - && (fndecl =3D get_callee_fndecl (rhs)) !=3D NULL_TREE > - && DECL_IS_MALLOC (fndecl)) > - { > - tree fntype =3D TREE_TYPE (fndecl); > - tree fntypeattrs =3D TYPE_ATTRIBUTES (fntype); > - tree alloc_size =3D lookup_attribute ("alloc_size", fntypeattrs); > - if (alloc_size) > + if (warn_alloc_size && TREE_CODE (rhs) =3D=3D CALL_EXPR) > + if (tree fndecl =3D get_callee_fndecl (rhs)) > + if (DECL_IS_MALLOC (fndecl)) > { > - tree args =3D TREE_VALUE (alloc_size); > - int idx =3D TREE_INT_CST_LOW (TREE_VALUE (args)) - 1; > - /* For calloc only use the second argument. */ > - if (TREE_CHAIN (args)) > - idx =3D TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1; > - tree arg =3D CALL_EXPR_ARG (rhs, idx); > - if (TREE_CODE (arg) =3D=3D INTEGER_CST > - && !VOID_TYPE_P (ttl) && TYPE_SIZE_UNIT (ttl) > - && INTEGER_CST =3D=3D TREE_CODE (TYPE_SIZE_UNIT (ttl)) > - && tree_int_cst_lt (arg, TYPE_SIZE_UNIT (ttl))) > - warning_at (location, OPT_Walloc_size, "allocation of " > - "insufficient size %qE for type %qT with " > - "size %qE", arg, ttl, TYPE_SIZE_UNIT (ttl)); > + tree attrs =3D TYPE_ATTRIBUTES (TREE_TYPE (fndecl)); > + tree alloc_size =3D lookup_attribute ("alloc_size", attrs); > + if (alloc_size) > + warn_for_alloc_size (location, ttl, rhs, alloc_size); > } > - } > =20 > /* See if the pointers point to incompatible address spaces. */ > asl =3D TYPE_ADDR_SPACE (ttl); > --- gcc/testsuite/gcc.dg/Walloc-size-4.c.jj 2023-12-18 19:06:41.069528176= +0100 > +++ gcc/testsuite/gcc.dg/Walloc-size-4.c 2023-12-18 19:34:06.964824180 +0= 100 > @@ -0,0 +1,54 @@ > +/* Tests the warnings for insufficient allocation size. */ > +/* { dg-do compile } */ > +/* { dg-options "-Walloc-size -Wno-calloc-transposed-args" } */ > + > +struct S { int x[10]; }; > +void bar (struct S *); > +typedef __SIZE_TYPE__ size_t; > +void *myfree (void *, int, int); > +void *mymalloc (int, int, size_t) __attribute__((malloc, malloc (myfree)= , alloc_size (3))); > +void *mycalloc (int, int, size_t, size_t) __attribute__((malloc, malloc = (myfree), alloc_size (3, 4))); > + > +void > +foo (void) > +{ > + struct S *p =3D (struct S *) __builtin_malloc (sizeof *p); > + __builtin_free (p); > + p =3D (struct S *) __builtin_malloc (sizeof p); /* { dg-warning "allo= cation of insufficient size" } */ > + __builtin_free (p); > + p =3D (struct S *) __builtin_alloca (sizeof p); /* { dg-warning "allo= cation of insufficient size" } */ > + bar (p); > + p =3D (struct S *) __builtin_calloc (1, sizeof p); /* { dg-warning "al= location of insufficient size" } */ > + __builtin_free (p); > + bar ((struct S *) __builtin_malloc (4)); /* { dg-warning "allocation = of insufficient size" } */ > + __builtin_free (p); > + p =3D (struct S *) __builtin_calloc (sizeof *p, 1); > + __builtin_free (p); > + p =3D __builtin_calloc (sizeof *p, 1); > + __builtin_free (p); > +} > + > +void > +baz (void) > +{ > + struct S *p =3D (struct S *) mymalloc (42, 42, sizeof *p); > + myfree (p, 42, 42); > + p =3D (struct S *) mymalloc (42, 42, sizeof p); /* { dg-warning "allo= cation of insufficient size" } */ > + myfree (p, 42, 42); > + p =3D (struct S *) mycalloc (42, 42, 1, sizeof p); /* { dg-warning "al= location of insufficient size" } */ > + myfree (p, 42, 42); > + bar ((struct S *) mymalloc (42, 42, 4)); /* { dg-warning "allocation = of insufficient size" } */ > + myfree (p, 42, 42); > + p =3D (struct S *) mycalloc (42, 42, sizeof *p, 1); > + myfree (p, 42, 42); > + p =3D mycalloc (42, 42, sizeof *p, 1); > + myfree (p, 42, 42); > + p =3D mymalloc (42, 42, sizeof *p); > + myfree (p, 42, 42); > + p =3D mymalloc (42, 42, sizeof p); /* { dg-warning "allocation of in= sufficient size" } */ > + myfree (p, 42, 42); > + p =3D mycalloc (42, 42, 1, sizeof p); /* { dg-warning "allocation of= insufficient size" } */ > + myfree (p, 42, 42); > + bar (mymalloc (42, 42, 4)); /* { dg-warning "allocation of insuffic= ient size" } */ > + myfree (p, 42, 42); > +} > --- gcc/testsuite/gcc.dg/Walloc-size-5.c.jj 2023-12-18 19:18:13.451180896= +0100 > +++ gcc/testsuite/gcc.dg/Walloc-size-5.c 2023-12-18 19:19:25.244173868 +0= 100 > @@ -0,0 +1,20 @@ > +/* { dg-do compile } */ > +/* { dg-options "-Walloc-size -std=3Dgnu11" } */ > + > +struct S { int x[10]; }; > +void myfree (); > +void *mymalloc () __attribute__((malloc, alloc_size (16))); > +void *mycalloc () __attribute__((malloc, alloc_size (16, 17))); > + > +void > +foo (void) > +{ > + struct S *p =3D mymalloc (1); > + myfree (p); > + p =3D mycalloc (1, 1); > + myfree (p); > + p =3D (struct S *) mymalloc (1); > + myfree (p); > + p =3D (struct S *) mycalloc (1, 1); > + myfree (p); > +} > --- gcc/testsuite/gcc.dg/Wcalloc-transposed-args-1.c.jj 2023-12-18 19:39:= 42.930196797 +0100 > +++ gcc/testsuite/gcc.dg/Wcalloc-transposed-args-1.c 2023-12-18 19:52:03.= 273855456 +0100 > @@ -0,0 +1,54 @@ > +/* { dg-do compile } */ > +/* { dg-options "-Wcalloc-transposed-args" } */ > + > +typedef __SIZE_TYPE__ size_t; > +void free (void *); > +void *calloc (size_t, size_t); > +void *myfree (void *, int, int); > +void *mycalloc (int, int, size_t, size_t) __attribute__((malloc, malloc = (myfree), alloc_size (3, 4))); > + > +void > +foo (int n) > +{ > + void *p; > + p =3D __builtin_calloc (1, sizeof (int)); > + __builtin_free (p); > + p =3D __builtin_calloc (n, sizeof (int)); > + __builtin_free (p); > + p =3D __builtin_calloc (sizeof (int), 1); /* { dg-warning "'__builtin= _calloc' sizes specified with 'sizeof' in the earlier argument and not in t= he later argument" } */ > + __builtin_free (p); /* { dg-message "earlier argument should speci= fy number of elements, later size of each element" "" { target *-*-* } .-1 = } */ > + p =3D __builtin_calloc (sizeof (int), n); /* { dg-warning "'__builtin= _calloc' sizes specified with 'sizeof' in the earlier argument and not in t= he later argument" } */ > + __builtin_free (p); /* { dg-message "earlier argument should speci= fy number of elements, later size of each element" "" { target *-*-* } .-1 = } */ > + p =3D __builtin_calloc ((sizeof (int)), 1); /* { dg-warning "'__built= in_calloc' sizes specified with 'sizeof' in the earlier argument and not in= the later argument" } */ > + __builtin_free (p); /* { dg-message "earlier argument should speci= fy number of elements, later size of each element" "" { target *-*-* } .-1 = } */ > + p =3D __builtin_calloc (sizeof (int) + 0, 1); > + __builtin_free (p); > + p =3D __builtin_calloc (sizeof (int), sizeof (char)); > + __builtin_free (p); > + p =3D __builtin_calloc (1 * sizeof (int), 1); > + __builtin_free (p); > + p =3D calloc (1, sizeof (int)); > + free (p); > + p =3D calloc (n, sizeof (int)); > + free (p); > + p =3D calloc (sizeof (int), 1); /* { dg-warning "'calloc' sizes spe= cified with 'sizeof' in the earlier argument and not in the later argument"= } */ > + free (p); /* { dg-message "earlier argument should specify number= of elements, later size of each element" "" { target *-*-* } .-1 } */ > + p =3D calloc (sizeof (int), n); /* { dg-warning "'calloc' sizes spe= cified with 'sizeof' in the earlier argument and not in the later argument"= } */ > + free (p); /* { dg-message "earlier argument should specify number= of elements, later size of each element" "" { target *-*-* } .-1 } */ > + p =3D calloc (sizeof (int), sizeof (char)); > + free (p); > + p =3D calloc (1 * sizeof (int), 1); > + free (p); > + p =3D mycalloc (42, 42, 1, sizeof (int)); > + myfree (p, 42, 42); > + p =3D mycalloc (42, 42, n, sizeof (int)); > + myfree (p, 42, 42); > + p =3D mycalloc (42, 42, sizeof (int), 1); /* { dg-warning "'mycalloc'= sizes specified with 'sizeof' in the earlier argument and not in the later= argument" } */ > + myfree (p, 42, 42); /* { dg-message "earlier argument should speci= fy number of elements, later size of each element" "" { target *-*-* } .-1 = } */ > + p =3D mycalloc (42, 42, sizeof (int), n); /* { dg-warning "'mycalloc'= sizes specified with 'sizeof' in the earlier argument and not in the later= argument" } */ > + myfree (p, 42, 42); /* { dg-message "earlier argument should speci= fy number of elements, later size of each element" "" { target *-*-* } .-1 = } */ > + p =3D mycalloc (42, 42, sizeof (int), sizeof (char)); > + myfree (p, 42, 42); > + p =3D mycalloc (42, 42, 1 * sizeof (int), 1); > + myfree (p, 42, 42); > +} >=20 > Jakub >=20