Xcode 4.5.1 new warning (potentially insecure) - objective-c

I upgraded my Xcode to Version 4.5.1 and suddenly previous app development had 'potentially insecure' during build.
it generally applies to declaring this type of construct:
NSString *userChoice = [NSString stringWithFormat:label1a.text]; //Warning at pulling string from label1a.text
[usrAnswer setObject:userChoice forKey:#"1"];
is this a serious warning? How do i rectify this?

You are not using a format string. Just do this:
NSString *userChoice = label1a.text;
I see a lot of people create strings with stringWithFormat:. Only use that when you are actually creating a string with format specifiers.

Related

How to convert c++ std::u32string to NSString?

I am developing a bridge between C++ and swift. And I need to convert C++ std::u32string to NSString. Here the code I tried:
u32string str = U"some string";
NSLog(#"%#", [[NSString alloc] initWithBytes:str.data() length:str.size() * sizeof(char32_t) encoding:NSUTF32StringEncoding]);
However initWithBytes returns nil.
Don't ask me why, but it seems to work if:
I insert a byte order mark (BOM) at the start of the string:u32string str = U"\uFEFFsome string";
or I use NSUTF32LittleEndianStringEncoding instead of NSUTF32StringEncoding.
Inserting byte order marks all over the place isn't terribly practical, so I guess you need to define your own constant which evaluates to either the little or big endian versions of the encoding constant depending on the platform being compiled.
This appears to be a quirk in Foundation, nothing specific to Objective-C++ or your use of std::u32string.

Compiling issues on STTwitter for OSX App

I've been working on an app for OSX (not my own, one that I am to maintain) that needs Twitter integrations and found the STTwitter wrapper. It says it's compatible down to OSX 10.7. However, when I try to compile it, I run into several compilation issues that I've tracked I think I've tracked down to not being able to compile with Objective-C literal subscripts.
I tries using the workaround suggested across the web for adding my own interfaces, but that didn't seem to help.
The first compilation error I get is "Array subscript is not an integer" at the following chunk of code in NSError+STTwitter.m:
NSMutableDictionary *md = [NSMutableDictionary dictionary];
md[NSLocalizedDescriptionKey] = message;
if(underlyingError) md[NSUnderlyingErrorKey] = underlyingError;
if(rateLimitLimit) md[kSTTwitterRateLimitLimit] = rateLimitLimit;
if(rateLimitRemaining) md[kSTTwitterRateLimitRemaining] = rateLimitRemaining;
if(rateLimitResetDate) md[kSTTwitterRateLimitResetDate] = rateLimitResetDate;
If I comment that code out, just to see what happens (the literal substring I believe), I get more issues in STTwitterOS.m
NSString *value = [keyValue[1] stringByReplacingOccurrencesOfString:#"\"" withString:#""];
[md setObject:value forKey:keyValue[0]];
Those give "Bad receiver type NSArray" and "Sending NSArray to parameter of incompatible type 'id
Any help would be appreciated. My objective c coding isn't that great....

use '[]' syntax to access containers in XCode 4.4 seems not working

Has anyone tried new syntax sugar introduced in Xcode 4.4 (iOS 5.1), like automatically calls #synthesize or Literal Syntax for NSArray ? They are quite handy.
But I can't get it right for this one , "use '[]' syntax to access". I tried following and they did not work. What did I do wrong ? Thanks.
NSArray *tmp = #[#"hello",#"world"]; //This one works fine
NSString *i = tmp[0]; // or tmp[#0]; this one does not work.
You're using two different features there. Your first line (tmp = #[#"hello",#"world"]) are literals. That should work in Xcode 4.4.
The second line (i = tmp[0]) needs runtime support (there are a couple of extra methods needed to make it work) and so won't work in iOS 5 and below. See this answer for more details.

Objective-C, Buzztouch coding alerts - Data argument not used by format string and Semantic issue. Can someone explain what is going on?

I'm currently running Mountain Lion OS X 10.8 with Xcode 4.4 installed. I'm running the iOS 5.1 simulator. I'm using Buzztouch as a learning tool while I'm studying Objective-C and Xcode. I get the following alerts when I compile, but the build succeeds. However, I would like to know exactly what is going on and how I can remedy the situation. Thank you for any assistance you can provide. Here's the code and the alerts I'm getting:
BT_fileManager.m
Data argument not used by format string
[BT_debugger showIt:self:[NSString stringWithFormat:#"readTextFileFromBundleWithEncoding ERROR using encoding NSUTF8StringEncoding, trying NSISOLatin1StringEncoding", #""]];
Data argument not used by format string
[BT_debugger showIt:self:[NSString stringWithFormat:#"readTextFileFromCacheWithEncoding ERROR using encoding NSUTF8StringEncoding, trying NSISOLatin1StringEncoding", #""]];
BT_camera_email.m
Semantic Issue
Sending 'BT_camera_email *' to parameter of incompatible type 'id'
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(#"is camera ok");
UIActionSheet *photoSourceSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image Source"
delegate:self
Again, thanks.
Greg
I have no idea what Buzztouch might be, however.... :-)
The first warning is fairly simple. In a format string there are placeholders beginning with a '%' sign to indicate where data values should be substituted. For example, to substitute a string, one would use '%#'. In the examples you show, there are no placeholders but there are data values -- empty strings. The compiler is warning that something you indicate you want to have put into the new string created by stringWithFormat: won't be.
To be sure about the second one, I'd want to see the .h file that declares BT_camera_email but my best guess is that it doesn't adopt the UIActionSheetDelegate protocol. The description of initWithTitle:... says the second parameter should be id<UIActionSheetDelegate> and that's probably what is being complained about.

Adding string objects to NSMutableArray?

I have a little foundation tool test (Objective-C) that I am playing with and I have a few questions ...
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int planetLoop;
NSString *tempString;
NSMutableArray *planetArray = [[NSMutableArray alloc] init];
NSLog(#"STRING ARRAY ... Start");
for(planetLoop=0; planetLoop<10; planetLoop++) {
tempString = [NSString stringWithFormat: #"Planet_%03d", planetLoop+1];
NSLog(#"Planet_%03d", planetLoop+1);
[planetArray addObject:tempString];
}
[planetArray release];
[pool drain];
return 0;
}
First, usually I release an object after adding it to an array, but am I right in thinking that what I have currently is right because "tempString" is a string literal, and as such does not need to be allocated or released?
Secondly, when I run this (prior to execution) I get the following eror "unable to read unknown load command 0x80000022" if this a problem with my code? from searching on google it looks like it might be a bug in xCode 3.1.2, anyone have any ideas?
Finally anything I am doing wrong, the idea is to fill an array with 10 string "Planet_001" through to "Planet_010"
EDIT: Ah, I see, thats because of the "= [NSString" bit i.e.
// Autoreleased object
tempString = [NSString stringWithFormat: #"Planet_%03d", planetLoop+1];
// String literal
tempString = #"Planet_";
many thanks, much appreciated -gary-
tempString isn't actually a string literal. #"Planet_%03d" is a string literal. tempString is an autoreleased object, meaning that it will be released when the NSAutoreleasePool is drained. Basically, the memory is already managed and you don't have to do anything.
The rule is: If you new, alloc, copy or retain an object, then you have to release it. Otherwise, the memory is already managed, probably by an autorelease.
Also, you forgot to release pool. Other than that, it looks fine.
One possible reason for the "unable to read unknown load command 0x80000022" error appears to be that I've upgraded to Snow Leopard without upgrading the developers tools at the same time. It looks like the error might be caused by trying to use the 10.5 version to XCode to compile in a 10.6 environment. I will look into this tomorrow.
Xcode 3.2 is now available in the Snow Leopard (Mac OS X 10.6) release. After installing Snow Leopard, upgrade to Xcode 3.2 by installing it separately from the Xcode Tools disk image. You can install it over prior versions of Xcode, or move them aside prior to installing.
PS: When I got the "unable to read unknown load command 0x80000022" error I was running OSX 10.6.1 with xCode 3.1.2
cheers -gary-
That "load command" error is due to the fact that the executable format has changed from iPhone OS 3.0 to iPhone OS 3.1.
http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html