• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: errno in debug vs. release
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: errno in debug vs. release
      • From: John W Noerenberg II <email@hidden>
References: 
 >errno in debug vs. release (From: John W Noerenberg II <email@hidden>)

  • Prev by Date: Re: crash in __static_initialization_and_destruction on 10.3.9 but not 10.4.4?
  • Next by Date: Symbol LEHB0 already defined error with GCC 3.3...
  • Previous by thread: errno in debug vs. release
  • Next by thread: Re: errno in debug vs. release
  • Index(es):
    • Date
    • Thread