9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: [9fans] c compiler size bug fix
Date: Tue, 15 Sep 2009 18:14:29 -0400	[thread overview]
Message-ID: <da75105ee2bd2ebbc5784f863df55d8b@quanstro.net> (raw)

unfortunately i let this one fester until it cost
us some time.  this problem has cropped up
again http://9fans.net/archive/2009/07/305
(it appears that my worry about expandable arrays
was unfounded.)

the problem is that the size of an undefined
struct as the last element of a struct is not
counted because it is not known at the time.
rather than diagnosing an incomplete reference,
the program compiles and prints an unespected
size, "sizeof A = 4 4".  i don't believe it should
compile at all.

the fix is simply this:

/n/dump/2009/0915/sys/src/cmd/cc/dcl.c:541,547 - dcl.c:541,547
  				l->offset = o;
  			} else {
  				if(l->width <= 0)
- 				if(l->down != T)
+ 				if(l->down != T || l->width < 0)
  					if(l->sym)
  						diag(Z, "incomplete structure element: %s",
  							l->sym->name);

thanks to bwc for identifying the problem (again).
patch submitted: /n/sources/patch/incmpltdiag

- erik

---

#include <u.h>
#include <libc.h>

typedef struct A A;
typedef struct B B;

struct A {
	int	a;
	B;
};

struct B {
	uchar	buf[512];
};

void
main(void)
{
	A a;
	B b;

	print("sizeof A = %d %d\n", sizeof(A), sizeof a);
	print("sizeof B = %d %d\n", sizeof(B), sizeof b);
}



                 reply	other threads:[~2009-09-15 22:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=da75105ee2bd2ebbc5784f863df55d8b@quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).