errno in debug vs. release
errno in debug vs. release
- Subject: errno in debug vs. release
- From: John W Noerenberg II <email@hidden>
- Date: Tue, 7 Feb 2006 12:21:56 -0800
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
Error - free: Bad file descriptor
after malloc, errno = 9
Error - malloc: Bad file descriptor
Saved by the malloc!
Debugger stopped.
Program exited with status value:0.
Looks good.
But when I switch to the release config, build and run the program:
[host112:~/mallocFail/build/Release] jwn2% ./mallocFail
after free, errno = 0
after malloc, errno = 0
Not good. buf= 0x500160 and Gotcha!
[host112:~/mallocFail/build/Release] jwn2%
So with the release configuration free releases unallocated memory
and malloc clobbers the heap.
I haven't looked to see if there is a simple way to edit the build
configs (not being an Xcode wizard). Does anyone know how to fix
this so the release config invoke versions of malloc and free that do
the right thing?
Thanks!
--
john noerenberg
----------------------------------------------------------------------
It took long enough in all conscience for realization to come that
the externals of civilization - technology, industry, commerce, and
so on - also require a common basis of intellectual honesty and morality.
-- Herman Hesse, The Glass Bead Game, 1943
----------------------------------------------------------------------
_______________________________________________
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