public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1)
@ 2022-01-08 17:11 admin@tho-otto.de
  2022-01-08 17:16 ` [Bug c/103950] " admin@tho-otto.de
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: admin@tho-otto.de @ 2022-01-08 17:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

            Bug ID: 103950
           Summary: printf("\xff") incorrectly optimized to putchar(-1)
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: admin@tho-otto.de
  Target Milestone: ---

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/103950] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
@ 2022-01-08 17:16 ` admin@tho-otto.de
  2022-01-08 18:00 ` [Bug middle-end/103950] " pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: admin@tho-otto.de @ 2022-01-08 17:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

--- Comment #1 from Thorsten Otto <admin@tho-otto.de> ---
In gimple_fold_builtin_printf(), a call to printf() with a
single-character-string is optimized to putchar(). However that is also done
with non-ascii-characters, which in the case of printf("\ff") will call putchar
with a value of (-1) == EOF. That happens even if -funsigned-char is used.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
  2022-01-08 17:16 ` [Bug c/103950] " admin@tho-otto.de
@ 2022-01-08 18:00 ` pinskia at gcc dot gnu.org
  2022-01-08 18:08 ` [Bug middle-end/103950] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-08 18:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-08
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
      Known to fail|                            |4.1.2, 5.1.0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
Simplified testcase:
int main(void)
{
  __builtin_printf("\xff");
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
  2022-01-08 17:16 ` [Bug c/103950] " admin@tho-otto.de
  2022-01-08 18:00 ` [Bug middle-end/103950] " pinskia at gcc dot gnu.org
@ 2022-01-08 18:08 ` pinskia at gcc dot gnu.org
  2022-01-08 18:18 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-08 18:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |9.5
            Summary|printf("\xff") incorrectly  |[9/10/11/12 Regression]
                   |optimized to putchar(-1)    |printf("\xff") incorrectly
                   |                            |optimized to putchar(-1)
      Known to work|                            |4.0.0

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The optimization was introduced in r0-69361 and so it is a regression from GCC
4.0.0 and before.
I think the following will fix the issue:
apinski@xeond:~/src/upstream-gcc/gcc/gcc$ git diff gimple-fold.c
diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index 77c551af942..6f158d301c5 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -3907,7 +3907,7 @@ gimple_fold_builtin_printf (gimple_stmt_iterator *gsi,
tree fmt,
          /* Given printf("c"), (where c is any one character,)
             convert "c"[0] to an int and pass that to the replacement
             function.  */
-         newarg = build_int_cst (integer_type_node, str[0]);
+         newarg = build_int_cst (integer_type_node, (unsigned char)str[0]);
          if (fn_putchar)
            {
              gcall *repl = gimple_build_call (fn_putchar, 1, newarg);

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (2 preceding siblings ...)
  2022-01-08 18:08 ` [Bug middle-end/103950] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
@ 2022-01-08 18:18 ` pinskia at gcc dot gnu.org
  2022-01-10 11:12 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-08 18:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>That happens even if -funsigned-char is used.

Yes because in this case, GCC is using char internally and forgetting it is
signed by default when calling a function as shown by my patch.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (3 preceding siblings ...)
  2022-01-08 18:18 ` pinskia at gcc dot gnu.org
@ 2022-01-10 11:12 ` rguenth at gcc dot gnu.org
  2022-01-10 11:18 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-10 11:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (4 preceding siblings ...)
  2022-01-10 11:12 ` rguenth at gcc dot gnu.org
@ 2022-01-10 11:18 ` pinskia at gcc dot gnu.org
  2022-01-10 12:24 ` admin@tho-otto.de
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-10 11:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I will fully test my fix over next weekend.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (5 preceding siblings ...)
  2022-01-10 11:18 ` pinskia at gcc dot gnu.org
@ 2022-01-10 12:24 ` admin@tho-otto.de
  2022-01-10 12:44 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: admin@tho-otto.de @ 2022-01-10 12:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

--- Comment #6 from Thorsten Otto <admin@tho-otto.de> ---
A similar fix will be needed in gimple_fold_builtin_fputs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (6 preceding siblings ...)
  2022-01-10 12:24 ` admin@tho-otto.de
@ 2022-01-10 12:44 ` pinskia at gcc dot gnu.org
  2022-01-24  2:51 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-10 12:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Thorsten Otto from comment #6)
> A similar fix will be needed in gimple_fold_builtin_fputs

Yes agreed.

gimple_fold_builtin_strstr should do the same, though I don't know if it will
matter but it is better to be safe than sorry there.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (7 preceding siblings ...)
  2022-01-10 12:44 ` pinskia at gcc dot gnu.org
@ 2022-01-24  2:51 ` pinskia at gcc dot gnu.org
  2022-01-24  2:52 ` pinskia at gcc dot gnu.org
  2022-01-24  3:03 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-24  2:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So I looked up the standard and it has the following to say about putchar/putc.
TC3 draft:
7.19.7.3/2:
The fputc function writes the character specified by c (converted to an
unsigned
char) 

7.19.7.9/2:
The putchar function is equivalent to putc with the second argument stdout.

C90 has the same wording.
4.9.7.3/2:

The fputc function writes the character specified by c (converted to an
unsigned char ) to the output stream pointed to by stream ...


So actually GCC is doing is correct.
If there is a bug it is in the libc you are using.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (8 preceding siblings ...)
  2022-01-24  2:51 ` pinskia at gcc dot gnu.org
@ 2022-01-24  2:52 ` pinskia at gcc dot gnu.org
  2022-01-24  3:03 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-24  2:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
strchr has the same wording too:
The strchr function locates the first occurrence of c (converted to a char ) in
the string pointed to by s .

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug middle-end/103950] [9/10/11/12 Regression] printf("\xff") incorrectly optimized to putchar(-1)
  2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
                   ` (9 preceding siblings ...)
  2022-01-24  2:52 ` pinskia at gcc dot gnu.org
@ 2022-01-24  3:03 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-24  3:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103950

--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the reason why the standard is written this way is because in K&R C, every
function argument was promoted to int as there were no prototypes and then the
function would cast it back to char/unsigned char, etc. So this is carrying
forward backwards compatibility.

So again what GCC is doing is correct even if it seems wrong and the functions
(or macros) need to do the casting to follow the C standard and all.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-01-24  3:03 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-08 17:11 [Bug c/103950] New: printf("\xff") incorrectly optimized to putchar(-1) admin@tho-otto.de
2022-01-08 17:16 ` [Bug c/103950] " admin@tho-otto.de
2022-01-08 18:00 ` [Bug middle-end/103950] " pinskia at gcc dot gnu.org
2022-01-08 18:08 ` [Bug middle-end/103950] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2022-01-08 18:18 ` pinskia at gcc dot gnu.org
2022-01-10 11:12 ` rguenth at gcc dot gnu.org
2022-01-10 11:18 ` pinskia at gcc dot gnu.org
2022-01-10 12:24 ` admin@tho-otto.de
2022-01-10 12:44 ` pinskia at gcc dot gnu.org
2022-01-24  2:51 ` pinskia at gcc dot gnu.org
2022-01-24  2:52 ` pinskia at gcc dot gnu.org
2022-01-24  3:03 ` pinskia at gcc dot gnu.org

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).