• 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: NSApplicationMain const warning
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSApplicationMain const warning


  • Subject: Re: NSApplicationMain const warning
  • From: Allan Odgaard <email@hidden>
  • Date: Wed, 5 May 2004 20:19:25 +0200

On 5. May 2004, at 17:48, Reni Puls wrote:

So NSApplicationMain requires 'argv' to be an array of const char pointers. Why does it matter if I pass it a non-const array? It's not like I am forcing the function to modify the strings--if it wants to consider them constant, that is fine with me. Shouldn't the C compiler silently ignore this?

No it should not! :) The problem is that NSApplicationMain takes an array of constant strings, but the array itself is not const, so it could do this:

int NSApplicationMain (int argc, char const* argv[])
{
char const* str = "bar"; // constant string!
argv[0] = str; // legal, because we can modify argv!
}

If the char** to char const** conversion was legal, then we could exploit it like this:

void dummy ()
{
char* array[] = { strdup("foo") };
NSApplicationMain(1, array); // this changes array[0] to a char const*

char* str = array[0]; // legal!
str[0] = 'c' // ups! we just mutated the const string
// placed here by NSApplicationMain!

printf("%s\n", str); // prints 'car' if the previous line did not
// cause a 'bus error'
}

So NSApplicationMain should actually be prototyped like this:

int NSApplicationMain (int argc, char const* const argv[])

This would make sure it could not modify the elements in argv, and you would then not get a warning when passing a char** as argument (as the conversion would then be allowed by the C standard).
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: NSApplicationMain const warning
      • From: René Puls <email@hidden>
References: 
 >NSApplicationMain const warning (From: René Puls <email@hidden>)

  • Prev by Date: Re: Scanning directory contents
  • Next by Date: Re: plugging NSString * leaks
  • Previous by thread: Re: NSApplicationMain const warning
  • Next by thread: Re: NSApplicationMain const warning
  • Index(es):
    • Date
    • Thread