ObjFW  Check-in [58a082efe4]

Overview
Comment:Use OFInvalidFormatException for -[(hexa)decimalValue] on bad strings.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 58a082efe4d2821486cd55569eb99423997a9b5ef6777967878dd54aa613f787
User & Date: js on 2011-02-01 20:58:43
Other Links: manifest | tags
Context
2011-02-01
21:07
Fix lookup of service when getaddrinfo is unavailable. check-in: 11a9f98b7a user: js tags: trunk
20:58
Use OFInvalidFormatException for -[(hexa)decimalValue] on bad strings. check-in: 58a082efe4 user: js tags: trunk
20:53
Rename OF_BSWAP_* to of_bswap_* as they are not really macros. check-in: 8384ca4273 user: js tags: trunk
Changes

Modified src/OFString.m from [d45226b27d] to [8bb8574c87].

980
981
982
983
984
985
986
987

988
989
990
991
992
993
994
980
981
982
983
984
985
986

987
988
989
990
991
992
993
994







-
+







			if (INTMAX_MAX / 10 < num ||
			    INTMAX_MAX - num * 10 < string[i] - '0')
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			num = (num * 10) + (string[i] - '0');
		} else
			@throw [OFInvalidEncodingException newWithClass: isa];
			@throw [OFInvalidFormatException newWithClass: isa];
	}

	if (string[0] == '-')
		num *= -1;

	return num;
}
1004
1005
1006
1007
1008
1009
1010
1011

1012
1013
1014
1015
1016
1017

1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029

1030
1031
1032
1033
1034
1035
1036
1004
1005
1006
1007
1008
1009
1010

1011
1012
1013
1014
1015
1016

1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031
1032
1033
1034
1035
1036







-
+





-
+











-
+








	if (length >= 2 && string[0] == '0' && string[1] == 'x')
		i = 2;
	else if (length >= 1 && (string[0] == 'x' || string[0] == '$'))
		i = 1;

	if (i == length)
		@throw [OFInvalidEncodingException newWithClass: isa];
		@throw [OFInvalidFormatException newWithClass: isa];

	for (; i < length; i++) {
		uintmax_t newnum;

		if (suffix)
			@throw [OFInvalidEncodingException newWithClass: isa];
			@throw [OFInvalidFormatException newWithClass: isa];

		if (string[i] >= '0' && string[i] <= '9')
			newnum = (num << 4) | (string[i] - '0');
		else if (string[i] >= 'A' && string[i] <= 'F')
			newnum = (num << 4) | (string[i] - 'A' + 10);
		else if (string[i] >= 'a' && string[i] <= 'f')
			newnum = (num << 4) | (string[i] - 'a' + 10);
		else if (string[i] == 'h') {
			suffix = YES;
			continue;
		} else
			@throw [OFInvalidEncodingException newWithClass: isa];
			@throw [OFInvalidFormatException newWithClass: isa];

		if (newnum < num)
			@throw [OFOutOfRangeException newWithClass: isa];

		num = newnum;
	}

Modified tests/OFStringTests.m from [64f535dbb6] to [7424fc4c5b].

237
238
239
240
241
242
243
244

245
246

247
248
249

250
251

252
253

254
255
256
257
258
259
260
237
238
239
240
241
242
243

244
245

246
247
248

249
250

251
252

253
254
255
256
257
258
259
260







-
+

-
+


-
+

-
+

-
+







	    [@"0xABcd" hexadecimalValue] == 0xABCD &&
	    [@"xbCDE" hexadecimalValue] == 0xBCDE &&
	    [@"$CdEf" hexadecimalValue] == 0xCDEF &&
	    [@"Feh" hexadecimalValue] == 0xFE &&
	    [@"" hexadecimalValue] == 0)

	EXPECT_EXCEPTION(@"Detect invalid characters in -[decimalValue] #1",
	    OFInvalidEncodingException, [@"abc" decimalValue])
	    OFInvalidFormatException, [@"abc" decimalValue])
	EXPECT_EXCEPTION(@"Detect invalid characters in -[decimalValue] #2",
	    OFInvalidEncodingException, [@"0a" decimalValue])
	    OFInvalidFormatException, [@"0a" decimalValue])

	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #1",
	    OFInvalidEncodingException, [@"0xABCDEFG" hexadecimalValue])
	    OFInvalidFormatException, [@"0xABCDEFG" hexadecimalValue])
	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #2",
	    OFInvalidEncodingException, [@"0x" hexadecimalValue])
	    OFInvalidFormatException, [@"0x" hexadecimalValue])
	EXPECT_EXCEPTION(@"Detect invalid chars in -[hexadecimalValue] #3",
	    OFInvalidEncodingException, [@"$" hexadecimalValue])
	    OFInvalidFormatException, [@"$" hexadecimalValue])

	EXPECT_EXCEPTION(@"Detect out of range in -[decimalValue]",
	    OFOutOfRangeException,
	    [@"12345678901234567890123456789012345678901234567890"
	     @"12345678901234567890123456789012345678901234567890"
	    decimalValue])