From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5E1C73857C59; Sat, 8 Aug 2020 22:51:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E1C73857C59 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596927091; bh=fpjxY4nDObmgXeFQNQJWr7LLCoFMQ0h7iW+rPoLLS0A=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ON64q3yKKfuyhMmksb7Zj95DqC7UJ/sAalVmeyqxvRNduPEvV6Ss+h3aKVoEyreO1 u+vHYchiI6f7i6hcJIQhgyU+fBNlokYd+d0VYZZvokgNa/n/CwuOz/bEJu80ZqI7l7 2jUJ0OXkL+yLqawFZiwjRVK+uDSqJfMnk/kmnoQY= From: "pinskia at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/96538] Integer overflow when there are multiple operands in addition operation. Date: Sat, 08 Aug 2020 22:51:31 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pinskia at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_status resolution Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 08 Aug 2020 22:51:31 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96538 Andrew Pinski changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |RESOLVED Resolution|--- |INVALID --- Comment #2 from Andrew Pinski --- There is NO BUG here: sum =3D arr[1]+arr[2]+arr[3]; Is done in integer type and then casted to long. that is it is the same as: sum =3D (long)(arr[1]+arr[2]+arr[3]); While: for(int j=3D0;j<5;j++) { sum =3D sum+ arr[j]; } Is Done in long type. You can fix the original code by doing: sum =3D ((long)arr[1])+((long)arr[2])+((long)arr[3]);=