httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Richards <p...@originat.demon.co.uk>
Subject Re: cvs commit: apache/src mod_proxy.c (fwd)
Date Wed, 03 Jul 1996 21:42:12 GMT
In reply to Ben Laurie who said
> 
> But, but ... to a C compiler:
> 
> p="xyzzy";
> 
> and
> 
> p=malloc(6);
> 
> are essentially identical (as far as constness and areas of memory are
> concerned).

No they're not. The first (in most implementations) will place the data in
read-only memory, the second points to writable memory.

> "xyyzy"[0]='a';
> 
> is legal, if bizarre, C. That may seem like a tangent, but it isn't (honest).

and on any sane implementation will core dump. I'd want to see a
warning rather than a core dump any day.

Likewise,

const char *p;

p = "xxyyz";

p[0] = '\0';

will also core dump (and you get a warning from the compiler which you
should take heed of).

Now, with gcc prior to 2.7

const char *p;

p = "xxyyzz";

p = malloc(5);

p = '\0';

will still give you a compiler warning for the second assignement
but not for the malloc and will not core dump. I think the new
behaviour is more correct in that gcc now warns me that I'm abusing
a const pointer in more cases (e.g. the malloc), even if they
subsequently work. In a real program if I declare something const
then it's for my protection (I don't want it pointing to writable
memory) and if I then misuse the pointer I've probably made a
mistake. Hence I think the Apache code is still wrong and the gcc
2.7 behaviour is correct and more beneficial than previously.

e.g. (bad use of variable names but programmers make these mistakes)

const char *msg = "Error no:";
char *mesg;

	msg = malloc(strlen(msg) + 5);
	^^^
	sprintf(mesg, "%s%d", msg, errno);

Now clearly, the programmer made a typo and a warning would be appropriate
but under gcc prior to 2.7 there wouldn't have been one.

To make things interesting, under 2.6

const char msg[] = "Error no:";

	msg = malloc(5);

does give a warning and

char msg[] = "Error no:";

	msg = malloc();

gives an error so it looks like they've basically fixed a bug in the
compiler.

-- 
  Paul Richards, Originative Solutions Ltd.
  Internet: paul@netcraft.co.uk, http://www.netcraft.co.uk
  Phone: 0370 462071 (Mobile), +44 1225 447500 (work)

Mime
View raw message