From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id EE7E7385AC1F; Fri, 1 Jul 2022 17:00:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE7E7385AC1F From: "goldstein.w.n at gmail dot com" To: glibc-bugs@sourceware.org Subject: [Bug libc/29313] MIN/MAX macros can cause double evaluation of arguments Date: Fri, 01 Jul 2022 17:00:14 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: libc X-Bugzilla-Version: 2.36 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: goldstein.w.n at gmail dot com X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: WORKSFORME X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: glibc-bugs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-bugs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Jul 2022 17:00:15 -0000 https://sourceware.org/bugzilla/show_bug.cgi?id=3D29313 Noah Goldstein changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |goldstein.w.n at gmail dot= com --- Comment #3 from Noah Goldstein --- (In reply to Vasilis Gkoumplias from comment #0) > * MIN, MAX macros as defined in misc/sys/param.h > https://sourceware.org/git/?p=3Dglibc.git;a=3Dblob_plain;f=3Dmisc/sys/par= am.h; > hb=3DHEAD > can cause the double evaluation of one of the passed in arguments. > Currently MAX is defined as: > #define MAX(a,b) (((a)>(b))?(a):(b)) > This will evaluate the maximum argument twice. >=20 > * A test case to reproduce this would go like this: > int a =3D 1000; > int b =3D 10; > MAX(a++, b++) ; > // This will fail as a will be 1002 due to double evaluation by the MAX m= acro > EXPECT_EQ(a, 1001) >=20 > * solution > #define max(a,b) \ > ({ __typeof__ (a) _a =3D (a); \ > __typeof__ (b) _b =3D (b); \ > _a > _b ? _a : _b; }) >=20 > * A discussion (and a solution) for the matter can be found here > https://stackoverflow.com/questions/3437404/min-and-max-in-c >=20 I don't think we can change the existing macros to that because they will no longer be usable outside of functions. #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define max(a, b)=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 \ ({=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 \ __typeof__(a) _a =3D (a);=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20 \ __typeof__(b) _b =3D (b);=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20 \ _a > _b ? _a : _b;=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20 \ }) enum { A =3D 1, B =3D 2 }; // Okay enum { C =3D MAX(A, B) }; // Compile error enum { D =3D max(A, B) }; >=20 > Thanks --=20 You are receiving this mail because: You are on the CC list for the bug.=