From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id ACB5A3858D28; Sat, 28 Jan 2023 07:42:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ACB5A3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674891726; bh=O/PSO/Z6f8f73XHE3c+5oQwwNipD+LI5mliS+cAk1tQ=; h=From:To:Subject:Date:From; b=FIVDLlMfsK8kzY3kql5O7Pi/Uxh6/Q1btrdBnWPFCMSuSv22zMOMy9gTvo/o3rGgu CXuJxiG23d4ZcZ3HJ0HVOkaTclJXjrfsggpEquyBN+8boR8rVLIxW27b7qFBSAG0yv fI3FjkCO6j0o3awtB2cZGAEGKwSCTl5Ymnq14WVg= From: "postmaster at raasu dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/108580] New: gcc treats shifts as signed operation, does wrong promotion Date: Sat, 28 Jan 2023 07:42:06 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: postmaster at raasu dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: 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_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108580 Bug ID: 108580 Summary: gcc treats shifts as signed operation, does wrong promotion Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: postmaster at raasu dot org Target Milestone: --- I have a simple program that fails to compile correctly on any common compi= ler: int main() { int bits =3D 8; char* a =3D (char*)malloc(1 << bits); char* b =3D (char*)malloc(1 << bits); memcpy(b, a, 1 << bits); return 0; } when assembled with "gcc -S", the result is main: .LFB6: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $32, %rsp movl $8, -20(%rbp) movl -20(%rbp), %eax movl $1, %edx movl %eax, %ecx sall %cl, %edx movl %edx, %eax cltq movq %rax, %rdi call malloc@PLT movq %rax, -16(%rbp) movl -20(%rbp), %eax movl $1, %edx movl %eax, %ecx sall %cl, %edx movl %edx, %eax cltq movq %rax, %rdi call malloc@PLT movq %rax, -8(%rbp) movl -20(%rbp), %eax movl $1, %edx movl %eax, %ecx sall %cl, %edx movl %edx, %eax movslq %eax, %rdx movq -16(%rbp), %rcx movq -8(%rbp), %rax movq %rcx, %rsi movq %rax, %rdi call memcpy@PLT movl $0, %eax leave .cfi_def_cfa 7, 8 ret .cfi_endproc The part that is incorrect is: sall %cl, %edx movl %edx, %eax cltq movq %rax, %rdi It should zero-extend before the shift, but instead it sign-extends after t= he shift... Bit shifting is always unsigned operation. It correctly determines= the function requires 64-bit parameter, but fails to determine it's unsigned. Integer promotion rules say that unsigned type in expression must be promot= ed to larger unsigned type if it can hold the result. As bit shift is unsigned operation, the temporary should also be unsigned. Stock gcc headers don't have UINTPTR_C() macro which could be used to explicitly cast the constant "1" to pointer size to give hint that the shif= t is indeed unsigned operation. gcc version is: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0=