unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Compiler warnings in dispnew.c
       [not found] <87fsrg7gdt.fsf.ref@yahoo.com>
@ 2021-11-28  9:56 ` Po Lu
  2021-11-28 10:48   ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Po Lu @ 2021-11-28  9:56 UTC (permalink / raw)
  To: emacs-devel

Building with checking, I get:

  CC       dispnew.o
In function ‘copy_row_except_pointers’,
    inlined from ‘assign_row’ at dispnew.c:1050:3,
    inlined from ‘mirror_make_current’ at dispnew.c:2761:3:
dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘current_row’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]
 1037 |   memcpy (&to->x, &from->x, sizeof *to - off);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from termchar.h:23,
                 from dispnew.c:28:
dispnew.c: In function ‘mirror_make_current’:
dispextern.h:885:7: note: subobject ‘x’ declared here
  885 |   int x, y;
      |       ^
In function ‘copy_row_except_pointers’,
    inlined from ‘assign_row’ at dispnew.c:1050:3,
    inlined from ‘make_current’ at dispnew.c:2719:3:
dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘current_row’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]
 1037 |   memcpy (&to->x, &from->x, sizeof *to - off);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from termchar.h:23,
                 from dispnew.c:28:
dispnew.c: In function ‘make_current’:
dispextern.h:885:7: note: subobject ‘x’ declared here
  885 |   int x, y;
      |       ^
In function ‘copy_row_except_pointers’,
    inlined from ‘assign_row’ at dispnew.c:1050:3,
    inlined from ‘scrolling_window’ at dispnew.c:4445:4,
    inlined from ‘update_window’ at dispnew.c:3651:13:
dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘c’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]
 1037 |   memcpy (&to->x, &from->x, sizeof *to - off);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from termchar.h:23,
                 from dispnew.c:28:
dispnew.c: In function ‘update_window’:
dispextern.h:885:7: note: subobject ‘x’ declared here
  885 |   int x, y;
      |       ^
In function ‘copy_row_except_pointers’,
    inlined from ‘assign_row’ at dispnew.c:1050:3,
    inlined from ‘scrolling_window’ at dispnew.c:4768:6,
    inlined from ‘update_window’ at dispnew.c:3651:13:
dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘to’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]
 1037 |   memcpy (&to->x, &from->x, sizeof *to - off);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from termchar.h:23,
                 from dispnew.c:28:
dispnew.c: In function ‘update_window’:
dispextern.h:885:7: note: subobject ‘x’ declared here
  885 |   int x, y;

I think it would be prudent to fix this, thanks.



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

* Re: Compiler warnings in dispnew.c
  2021-11-28  9:56 ` Compiler warnings in dispnew.c Po Lu
@ 2021-11-28 10:48   ` Eli Zaretskii
  2021-11-28 10:57     ` Andreas Schwab
                       ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Eli Zaretskii @ 2021-11-28 10:48 UTC (permalink / raw)
  To: Po Lu; +Cc: emacs-devel

> From: Po Lu <luangruo@yahoo.com>
> Date: Sun, 28 Nov 2021 17:56:30 +0800
> 
> Building with checking, I get:
> 
>   CC       dispnew.o
> In function ‘copy_row_except_pointers’,
>     inlined from ‘assign_row’ at dispnew.c:1050:3,
>     inlined from ‘mirror_make_current’ at dispnew.c:2761:3:
> dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘current_row’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]
>  1037 |   memcpy (&to->x, &from->x, sizeof *to - off);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from termchar.h:23,
>                  from dispnew.c:28:
> dispnew.c: In function ‘mirror_make_current’:
> dispextern.h:885:7: note: subobject ‘x’ declared here
>   885 |   int x, y;
>       |       ^
> 
> I think it would be prudent to fix this, thanks.

Do you understand the problem?  It looks like a compiler bug to me.
We copy into the address of glyph_row->x and the size of the copy is
the size of glyph_row structure minus the offset of x from the
beginning.  So what is wrong with that? what am I missing?

When you say "building with checking", what do you mean, exactly?  Did
you use some non-default warning switches, and if so, which ones?

And what version of GCC is that?



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 10:48   ` Eli Zaretskii
@ 2021-11-28 10:57     ` Andreas Schwab
  2021-11-28 11:02     ` Po Lu
  2021-11-28 11:03     ` Eli Zaretskii
  2 siblings, 0 replies; 14+ messages in thread
From: Andreas Schwab @ 2021-11-28 10:57 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Po Lu, emacs-devel

On Nov 28 2021, Eli Zaretskii wrote:

> Do you understand the problem?  It looks like a compiler bug to me.
> We copy into the address of glyph_row->x and the size of the copy is
> the size of glyph_row structure minus the offset of x from the
> beginning.  So what is wrong with that? what am I missing?

It is copying beyond the object that is glyph_row->x.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 10:48   ` Eli Zaretskii
  2021-11-28 10:57     ` Andreas Schwab
@ 2021-11-28 11:02     ` Po Lu
  2021-11-28 11:07       ` Andreas Schwab
  2021-11-28 11:03     ` Eli Zaretskii
  2 siblings, 1 reply; 14+ messages in thread
From: Po Lu @ 2021-11-28 11:02 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Po Lu <luangruo@yahoo.com>
>> Date: Sun, 28 Nov 2021 17:56:30 +0800
>> 
>> Building with checking, I get:
>> 
>>   CC       dispnew.o
>> In function ‘copy_row_except_pointers’,
>>     inlined from ‘assign_row’ at dispnew.c:1050:3,
>>     inlined from ‘mirror_make_current’ at dispnew.c:2761:3:
>> dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘current_row’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]
>>  1037 |   memcpy (&to->x, &from->x, sizeof *to - off);
>>       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> In file included from termchar.h:23,
>>                  from dispnew.c:28:
>> dispnew.c: In function ‘mirror_make_current’:
>> dispextern.h:885:7: note: subobject ‘x’ declared here
>>   885 |   int x, y;
>>       |       ^
>> 
>> I think it would be prudent to fix this, thanks.

> Do you understand the problem?  It looks like a compiler bug to me.
> We copy into the address of glyph_row->x and the size of the copy is
> the size of glyph_row structure minus the offset of x from the
> beginning.  So what is wrong with that? what am I missing?

I don't see anything wrong with it, but I think someone has to find a
way to silence the warning.

I could not find some reasonable way to do so that did not involve
#pragma GCC, which I think is not allowed in Emacs code (is that
correct?)

Thanks.

> When you say "building with checking", what do you mean, exactly?  Did
> you use some non-default warning switches, and if so, which ones?

I meant to say with '--enable-checking="yes,glyphs"'.

> And what version of GCC is that?

It's GCC 11.2.1.

Thanks.



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 10:48   ` Eli Zaretskii
  2021-11-28 10:57     ` Andreas Schwab
  2021-11-28 11:02     ` Po Lu
@ 2021-11-28 11:03     ` Eli Zaretskii
  2021-11-28 11:10       ` Andreas Schwab
  2021-11-28 11:11       ` Po Lu
  2 siblings, 2 replies; 14+ messages in thread
From: Eli Zaretskii @ 2021-11-28 11:03 UTC (permalink / raw)
  To: luangruo; +Cc: emacs-devel

> Date: Sun, 28 Nov 2021 12:48:00 +0200
> From: Eli Zaretskii <eliz@gnu.org>
> Cc: emacs-devel@gnu.org
> 
> > From: Po Lu <luangruo@yahoo.com>
> > Date: Sun, 28 Nov 2021 17:56:30 +0800
> > 
> > Building with checking, I get:
> > 
> >   CC       dispnew.o
> > In function ‘copy_row_except_pointers’,
> >     inlined from ‘assign_row’ at dispnew.c:1050:3,
> >     inlined from ‘mirror_make_current’ at dispnew.c:2761:3:
> > dispnew.c:1037:3: warning: ‘memcpy’ offset [48, 255] from the object at ‘current_row’ is out of the bounds of referenced subobject ‘x’ with type ‘int’ at offset 44 [-Warray-bounds]

I don't understand the data the compiler prints, specifically why it
says "offset [48, 255]" and then "at offset 44".

If you put a breakpoint at line 2761 of mirror_make_current, and when it
breaks, type

 (gdb) ptype /o current_row
 (gdb) ptype /o desired_row

what do you see in offsets of x and in the total size of the
structure?

(Note: to hit the breakpoint, you need to invoke "emacs -Q -nw", the
"-nw" part is important.)



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:02     ` Po Lu
@ 2021-11-28 11:07       ` Andreas Schwab
  2021-11-28 15:29         ` Eli Zaretskii
  2021-11-29 17:49         ` Stefan Monnier
  0 siblings, 2 replies; 14+ messages in thread
From: Andreas Schwab @ 2021-11-28 11:07 UTC (permalink / raw)
  To: Po Lu; +Cc: Eli Zaretskii, emacs-devel

Does that work?

diff --git a/src/dispnew.c b/src/dispnew.c
index f3f110a8f2..a976bf94c5 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -1034,7 +1034,7 @@ copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
 {
   enum { off = offsetof (struct glyph_row, x) };
 
-  memcpy (&to->x, &from->x, sizeof *to - off);
+  memcpy ((char *) to + off, (char *) from + off, sizeof *to - off);
 }
 

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:03     ` Eli Zaretskii
@ 2021-11-28 11:10       ` Andreas Schwab
  2021-11-28 11:18         ` Eli Zaretskii
  2021-11-28 11:11       ` Po Lu
  1 sibling, 1 reply; 14+ messages in thread
From: Andreas Schwab @ 2021-11-28 11:10 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: luangruo, emacs-devel

On Nov 28 2021, Eli Zaretskii wrote:

> I don't understand the data the compiler prints, specifically why it
> says "offset [48, 255]" and then "at offset 44".

48 is the first offset that is out of range, 44 is the offset of x (thus
44 + 4 is outside of it).

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:03     ` Eli Zaretskii
  2021-11-28 11:10       ` Andreas Schwab
@ 2021-11-28 11:11       ` Po Lu
  1 sibling, 0 replies; 14+ messages in thread
From: Po Lu @ 2021-11-28 11:11 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>  (gdb) ptype /o current_row
>  (gdb) ptype /o desired_row
>
> what do you see in offsets of x and in the total size of the
> structure?

The offset of x is 44.  The total size of the size of the structure is
256.

Thanks.



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:10       ` Andreas Schwab
@ 2021-11-28 11:18         ` Eli Zaretskii
  2021-11-28 11:44           ` Andreas Schwab
  0 siblings, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2021-11-28 11:18 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: luangruo, emacs-devel

> From: Andreas Schwab <schwab@linux-m68k.org>
> Cc: luangruo@yahoo.com,  emacs-devel@gnu.org
> Date: Sun, 28 Nov 2021 12:10:15 +0100
> 
> On Nov 28 2021, Eli Zaretskii wrote:
> 
> > I don't understand the data the compiler prints, specifically why it
> > says "offset [48, 255]" and then "at offset 44".
> 
> 48 is the first offset that is out of range, 44 is the offset of x (thus
> 44 + 4 is outside of it).

Silly compiler.



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:18         ` Eli Zaretskii
@ 2021-11-28 11:44           ` Andreas Schwab
  0 siblings, 0 replies; 14+ messages in thread
From: Andreas Schwab @ 2021-11-28 11:44 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: luangruo, emacs-devel

On Nov 28 2021, Eli Zaretskii wrote:

>> From: Andreas Schwab <schwab@linux-m68k.org>
>> Cc: luangruo@yahoo.com,  emacs-devel@gnu.org
>> Date: Sun, 28 Nov 2021 12:10:15 +0100
>> 
>> On Nov 28 2021, Eli Zaretskii wrote:
>> 
>> > I don't understand the data the compiler prints, specifically why it
>> > says "offset [48, 255]" and then "at offset 44".
>> 
>> 48 is the first offset that is out of range, 44 is the offset of x (thus
>> 44 + 4 is outside of it).
>
> Silly compiler.

Nope.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:07       ` Andreas Schwab
@ 2021-11-28 15:29         ` Eli Zaretskii
  2021-11-29  0:35           ` Po Lu
  2021-11-29 17:49         ` Stefan Monnier
  1 sibling, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2021-11-28 15:29 UTC (permalink / raw)
  To: luangruo; +Cc: Andreas Schwab, emacs-devel

> From: Andreas Schwab <schwab@linux-m68k.org>
> Date: Sun, 28 Nov 2021 12:07:01 +0100
> Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
> 
> Does that work?
> 
> diff --git a/src/dispnew.c b/src/dispnew.c
> index f3f110a8f2..a976bf94c5 100644
> --- a/src/dispnew.c
> +++ b/src/dispnew.c
> @@ -1034,7 +1034,7 @@ copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
>  {
>    enum { off = offsetof (struct glyph_row, x) };
>  
> -  memcpy (&to->x, &from->x, sizeof *to - off);
> +  memcpy ((char *) to + off, (char *) from + off, sizeof *to - off);
>  }

Po Lu, please try this and tell if the warning goes away.

Thanks.



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 15:29         ` Eli Zaretskii
@ 2021-11-29  0:35           ` Po Lu
  0 siblings, 0 replies; 14+ messages in thread
From: Po Lu @ 2021-11-29  0:35 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Andreas Schwab, emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Andreas Schwab <schwab@linux-m68k.org>
>> Date: Sun, 28 Nov 2021 12:07:01 +0100
>> Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
>> 
>> Does that work?
>> 
>> diff --git a/src/dispnew.c b/src/dispnew.c
>> index f3f110a8f2..a976bf94c5 100644
>> --- a/src/dispnew.c
>> +++ b/src/dispnew.c
>> @@ -1034,7 +1034,7 @@ copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
>>  {
>>    enum { off = offsetof (struct glyph_row, x) };
>>  
>> -  memcpy (&to->x, &from->x, sizeof *to - off);
>> +  memcpy ((char *) to + off, (char *) from + off, sizeof *to - off);
>>  }
>
> Po Lu, please try this and tell if the warning goes away.
>
> Thanks.

Yes, this makes it go away.  Thanks.



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

* Re: Compiler warnings in dispnew.c
  2021-11-28 11:07       ` Andreas Schwab
  2021-11-28 15:29         ` Eli Zaretskii
@ 2021-11-29 17:49         ` Stefan Monnier
  2021-11-29 17:53           ` Andreas Schwab
  1 sibling, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2021-11-29 17:49 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Po Lu, Eli Zaretskii, emacs-devel

Andreas Schwab [2021-11-28 12:07:01] wrote:
> -  memcpy (&to->x, &from->x, sizeof *to - off);
> +  memcpy ((char *) to + off, (char *) from + off, sizeof *to - off);

I wonder: is the (char*) cast sufficient or is it necessary to
"obfuscate" the code by writing `to + off` instead of `&to->x`?
[ By "obfuscate" I mean here to hide from the compiler, rather than from
  humans, because I'm not sure which of the two is more clear to
  a human, here.  ]


        Stefan




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

* Re: Compiler warnings in dispnew.c
  2021-11-29 17:49         ` Stefan Monnier
@ 2021-11-29 17:53           ` Andreas Schwab
  0 siblings, 0 replies; 14+ messages in thread
From: Andreas Schwab @ 2021-11-29 17:53 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Po Lu, Eli Zaretskii, emacs-devel

On Nov 29 2021, Stefan Monnier wrote:

> Andreas Schwab [2021-11-28 12:07:01] wrote:
>> -  memcpy (&to->x, &from->x, sizeof *to - off);
>> +  memcpy ((char *) to + off, (char *) from + off, sizeof *to - off);
>
> I wonder: is the (char*) cast sufficient or is it necessary to
> "obfuscate" the code by writing `to + off` instead of `&to->x`?

I think the point is that the address expression is not based on the
subobject.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

end of thread, other threads:[~2021-11-29 17:53 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <87fsrg7gdt.fsf.ref@yahoo.com>
2021-11-28  9:56 ` Compiler warnings in dispnew.c Po Lu
2021-11-28 10:48   ` Eli Zaretskii
2021-11-28 10:57     ` Andreas Schwab
2021-11-28 11:02     ` Po Lu
2021-11-28 11:07       ` Andreas Schwab
2021-11-28 15:29         ` Eli Zaretskii
2021-11-29  0:35           ` Po Lu
2021-11-29 17:49         ` Stefan Monnier
2021-11-29 17:53           ` Andreas Schwab
2021-11-28 11:03     ` Eli Zaretskii
2021-11-28 11:10       ` Andreas Schwab
2021-11-28 11:18         ` Eli Zaretskii
2021-11-28 11:44           ` Andreas Schwab
2021-11-28 11:11       ` Po Lu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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