Add testcase for multiple clookies in a header. Fix error parsing a literal

string (writing nul terminator to read only memory)
This commit is contained in:
Richard Frith-Macdonald 2020-04-16 20:19:53 +01:00
parent 3d1e84f6fe
commit 6ee0cfff00
2 changed files with 15 additions and 11 deletions

View file

@ -902,7 +902,7 @@ GSCookieStrings(NSString *string)
* separate cookie or not. We look for something of the form * separate cookie or not. We look for something of the form
* ' token =' where a space represents any optional whitespace. * ' token =' where a space represents any optional whitespace.
*/ */
saved = pos; saved = pos++;
while (pos < end && isspace(ptr[pos])) while (pos < end && isspace(ptr[pos]))
{ {
pos++; pos++;
@ -939,11 +939,13 @@ GSCookieStrings(NSString *string)
} }
if (saved > start) if (saved > start)
{ {
const char *utf8 = (const char*)(ptr + start); NSString *str = [NSString alloc];
ptr[saved] = '\0'; str = [str initWithBytes: ptr + start
[cookies addObject: length: saved - start
[NSString stringWithUTF8String: utf8]]; encoding: NSUTF8StringEncoding];
[cookies addObject: str];
RELEASE(str);
} }
start = saved = pos; start = saved = pos;
} }
@ -966,14 +968,14 @@ GSCookieStrings(NSString *string)
} }
if (saved > start) if (saved > start)
{ {
NSString *str; NSString *str = [NSString alloc];
/* There may not be room to add a nul terminator, so we use an /* There may not be room to add a nul terminator, so we use an
* initialiser which doesn't need one. * initialiser which doesn't need one.
*/ */
str = [[NSString alloc] initWithBytes: ptr + start str = [str initWithBytes: ptr + start
length: saved - start length: saved - start
encoding: NSUTF8StringEncoding]; encoding: NSUTF8StringEncoding];
[cookies addObject: str]; [cookies addObject: str];
RELEASE(str); RELEASE(str);
} }

View file

@ -39,14 +39,15 @@ int main()
PASS(cookie == nil, "cookie without path returns nil"); PASS(cookie == nil, "cookie without path returns nil");
dict = [NSDictionary dictionaryWithObject: dict = [NSDictionary dictionaryWithObject:
@"S=calendar=R7tjDKqNB5L8YTZSvf29Bg;Expires=Wed, 09-Mar-2011 23:00:35 GMT" @"S=calendar=R7tjDKqNB5L8YTZSvf29Bg;Expires=Wed, 09-Mar-2011 23:00:35 GMT, "
@"S=xxxxxxxx=R7tjDKqNB5L8YTZSvf29Bg;Expires=Thu, 10-Mar-2011 23:00:35 GMT"
forKey: @"Set-Cookie"]; forKey: @"Set-Cookie"];
url = [NSURL URLWithString: @"http://www.google.com/calendar/feeds/default/"]; url = [NSURL URLWithString: @"http://www.google.com/calendar/feeds/default/"];
cookies= [NSHTTPCookie cookiesWithResponseHeaderFields: dict forURL: url]; cookies= [NSHTTPCookie cookiesWithResponseHeaderFields: dict forURL: url];
TEST_FOR_CLASS(@"NSArray", cookies, TEST_FOR_CLASS(@"NSArray", cookies,
"NSHTTPCookie +cookiesWithResponseHeaderFields: returns an NSArray"); "NSHTTPCookie +cookiesWithResponseHeaderFields: returns an NSArray");
PASS([cookies count ] == 1, "cookies array contains a cookie"); PASS([cookies count ] == 2, "cookies array contains two cookies");
cookie = [cookies objectAtIndex: 0]; cookie = [cookies objectAtIndex: 0];
PASS([[cookie name] isEqual: @"S"], "NSHTTPCookie returns proper name"); PASS([[cookie name] isEqual: @"S"], "NSHTTPCookie returns proper name");
PASS([[cookie value] isEqual: @"calendar=R7tjDKqNB5L8YTZSvf29Bg"], PASS([[cookie value] isEqual: @"calendar=R7tjDKqNB5L8YTZSvf29Bg"],
@ -56,6 +57,7 @@ int main()
PASS(![cookie isSecure], "Cookie is not secure"); PASS(![cookie isSecure], "Cookie is not secure");
PASS(![cookie isHTTPOnly], "Cookie is not http only"); PASS(![cookie isHTTPOnly], "Cookie is not http only");
cookies = [cookies subarrayWithRange: NSMakeRange(0, 1)];
dict = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies]; dict = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
PASS_EQUAL([dict objectForKey: @"Cookie"], PASS_EQUAL([dict objectForKey: @"Cookie"],
@"S=calendar=R7tjDKqNB5L8YTZSvf29Bg", @"S=calendar=R7tjDKqNB5L8YTZSvf29Bg",