Subject: bug#11675: closed (Re: bug#11675: stty bad C semantics)

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 12 Jun 2012 16:45:02 +0000

Your bug report

#11675: stty bad C semantics

which was filed against the coreutils package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
11675: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11675
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#11675: stty bad C semantics
From: Jim Meyering <[email-masked]>
To: Paul Eggert <[email-masked]>
Cc: Edward Schwartz <[email-masked]>, [email-masked]
Date: Tue, 12 Jun 2012 18:41:21 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Paul Eggert wrote:
> On 06/12/2012 07:33 AM, Jim Meyering wrote:
>> Here's a way to solve the problem that doesn't require restoring
>> the memset calls.  It feels slightly hackish
>
> But it's hackish in a good way!  It's a bit faster
> and smaller and more portable than the existing code,
> and it's faster and smaller than all the other proposed
> fixes.  It's clearly the best idea yet.  Thanks for
> cutting the Gordian knot.

Thanks.  With that, I've pushed it and marked this as done.

Subject: stty bad C semantics
From: Edward Schwartz <[email-masked]>
To: [email-masked]
Date: Mon, 11 Jun 2012 15:16:39 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

I think there is a bug in main() of stty in coreutils 8.17.  The gist
of the problem is that two structures are initialized:

   struct termios mode = { 0, };

and

  struct termios new_mode = { 0, };

They are then both modified, and then compared with memcmp.  The
problem is that the structs contain padding bytes.  The C99 standard
says "The value of padding bytes when storing values in structures or
unions (6.2.6.1)." is unspecified, so the padding bytes may not be set
to zero.

I don't have any problem compiling with gcc.  On my machine, gcc
initializes the entire struct memory with a loop that writes 0.

I came across the bug when compiling coreutils under CIL, which
rewrites many C language constructs to make them easier to analyze.
CIL writes 0 to each struct field, leaving padding bytes untouched.
Both are correct, under my interpretation of the C99 standard.
However, CIL's behavior violates the assumptions of stty's memcmp,
which assumes padding bytes are set to zero.

The problem is easily fixed by using memset, instead of implied
initializations.  I am attaching a patch that does this.  While it
won't affect most coreutils users, it might save some time for someone
using a non-standard compiler or analysis platform.

Thanks,
Ed