Re: errno in debug vs. release
Re: errno in debug vs. release
- Subject: Re: errno in debug vs. release
- From: Eric Albert <email@hidden>
- Date: Tue, 7 Feb 2006 12:52:21 -0800
On Feb 7, 2006, at 12:21 PM, John W Noerenberg II wrote:
I've been looking at problems with integer arithmetic and
discovered a problem between the default Xcode (v2.2.1) release
configuration and the debug configuration.
My test program is designed to illustrate how integer arithmetic
can introduce subtle vulnerabilities into your code. Here's my
little test program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
int main (int argc, const char * argv[]) {
char *buf = 0x0;
unsigned short len = 0xffff;
char *bigTrouble = "Gotcha!";
len +=1;
free (buf);
printf (" after free, errno = %d\n", errno);
if (errno) perror("Error - free");
buf = malloc (len);
printf ("after malloc, errno = %d\n", errno);
if (errno) {
perror("Error - malloc");
printf ("Saved by the malloc!\n");
} else {
strcpy (buf, bigTrouble);
printf ("Not good. buf= 0x%x and %s\n", buf, buf);
}
return 0;
}
When I build this with the debug configuration, I see the following
in gdb:
Program loaded.
sharedlibrary apply-load-rules all
run
[Switching to process 1491 local thread 0xf03]
RunningÅ
Pending breakpoint 2 - ""main.c:13" resolved
(gdb) continue
after free, errno = 9
free(3) is not documented to set errno, so there's no guarantee that
errno is actually set at this point in your code. Given that, you're
probably getting a random value for errno. I'd suggest changing your
code to only read errno after making function calls that are
documented as setting it.
-Eric
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden