adium 2615:a5c697502410: Corrected the value of regexpUnsafeNature.
commits at adium.im
commits at adium.im
Sat Aug 22 03:22:07 UTC 2009
details: http://hg.adium.im/adium/rev/a5c697502410
revision: 2615:a5c697502410
author: Peter Hosey <hg at boredzo.org>
date: Fri Aug 21 20:25:33 2009 -0700
Corrected the value of regexpUnsafeNature.
enum counts up by 1, which means that regexpUnsafeNature had value 3 (0b11), which is the value you get by setting the first two bits. In other words, regexpUnsafeNature = whitespaceNature (0b1) + shellUnsafeNature (0b10), which is bad and wrong.
The correct value is 4, which is the next higher bit (0b100).
In order to prevent mistakes like this from happening in the future, I also changed how we express the values. Previously, we used hexadecimal literals. Now we use bit-shift expressions.
Subject: adium 2616:eb8a9aca7158: Corrected misspelling of “backslash” as “blakslash”.
details: http://hg.adium.im/adium/rev/eb8a9aca7158
revision: 2616:eb8a9aca7158
author: Peter Hosey <hg at boredzo.org>
date: Fri Aug 21 20:25:43 2009 -0700
Corrected misspelling of “backslash” as “blakslash”.
Subject: adium 2617:08d2f8e32c0f: Consolidate the two character-nature tables (the second one having been introduced in [490052ed56a6]) into one, and make it static. Also, banish all runtime initialization of it; instead, we use index designators, a feature of C99, to initialize the array at compile time. Also, with Stephen Holt's permission, moved the unified table (including the regexp-nature parts of its initialization) into BSD-licensed territory.
details: http://hg.adium.im/adium/rev/08d2f8e32c0f
revision: 2617:08d2f8e32c0f
author: Peter Hosey <hg at boredzo.org>
date: Fri Aug 21 20:25:44 2009 -0700
Consolidate the two character-nature tables (the second one having been introduced in [490052ed56a6]) into one, and make it static. Also, banish all runtime initialization of it; instead, we use index designators, a feature of C99, to initialize the array at compile time. Also, with Stephen Holt's permission, moved the unified table (including the regexp-nature parts of its initialization) into BSD-licensed territory.
I love C99.
Subject: adium 2618:3571e5f41300: Fix the error messages from some of the assertions in -testEscapingForShell to correctly name the character they're testing.
details: http://hg.adium.im/adium/rev/3571e5f41300
revision: 2618:3571e5f41300
author: Peter Hosey <hg at boredzo.org>
date: Fri Aug 21 20:25:45 2009 -0700
Fix the error messages from some of the assertions in -testEscapingForShell to correctly name the character they're testing.
diffs (182 lines):
diff -r 490052ed56a6 -r 3571e5f41300 Frameworks/AIUtilities Framework/Source/AIStringAdditions.m
--- a/Frameworks/AIUtilities Framework/Source/AIStringAdditions.m Fri Aug 21 22:10:39 2009 -0400
+++ b/Frameworks/AIUtilities Framework/Source/AIStringAdditions.m Fri Aug 21 20:25:45 2009 -0700
@@ -27,6 +27,43 @@
#endif //ndef BSD_LICENSE_ONLY
+enum characterNatureMask {
+ whitespaceNature = (1 << 0), //space + \t\n\r\f\a
+ shellUnsafeNature = (1 << 1), //backslash + !$`"'
+ regexpUnsafeNature = (1 << 2), //backslash + |.*+?{}()$^
+};
+static enum characterNatureMask characterNature[USHRT_MAX+1] = {
+ ['\a'] = whitespaceNature,
+ ['\t'] = whitespaceNature,
+ ['\n'] = whitespaceNature,
+ ['\v'] = whitespaceNature,
+ ['\f'] = whitespaceNature,
+ ['\r'] = whitespaceNature,
+ [' '] = whitespaceNature,
+
+ ['\''] = shellUnsafeNature,
+ ['"'] = shellUnsafeNature,
+ ['`'] = shellUnsafeNature,
+ ['!'] = shellUnsafeNature,
+ ['&'] = shellUnsafeNature,
+
+ ['\\'] = shellUnsafeNature | regexpUnsafeNature,
+ ['$'] = shellUnsafeNature | regexpUnsafeNature,
+ ['|'] = shellUnsafeNature | regexpUnsafeNature,
+
+ ['/'] = regexpUnsafeNature,
+ ['.'] = regexpUnsafeNature,
+ ['*'] = regexpUnsafeNature,
+ ['+'] = regexpUnsafeNature,
+ ['?'] = regexpUnsafeNature,
+ ['{'] = regexpUnsafeNature,
+ ['}'] = regexpUnsafeNature,
+ ['('] = regexpUnsafeNature,
+ [')'] = regexpUnsafeNature,
+ ['['] = regexpUnsafeNature,
+ ['^'] = regexpUnsafeNature,
+};
+
enum {
LINE_FEED = '\n',
FORM_FEED = '\f',
@@ -524,52 +561,8 @@
#ifndef BSD_LICENSE_ONLY
-enum characterNatureMask {
- whitespaceNature = 0x1, //space + \t\n\r\f\a
- shellUnsafeNature, //backslash + !$`"'
- regexpUnsafeNature, //blakslash + |.*+?{}()$^
-};
-static enum characterNatureMask characterNature[USHRT_MAX+1] = {
- //this array is initialised such that the space character (0x20)
- // does not have the whitespace nature.
- //this was done for brevity, as the entire array is bzeroed and then
- // properly initialised in -stringByEscapingForShell below.
- 0,0,0,0, 0,0,0,0, //0x00..0x07
- 0,0,0,0, 0,0,0,0, //0x08..0x0f
- 0,0,0,0, 0,0,0,0, //0x10..0x17
- 0,0,0, //0x18..0x20
-};
-
- (NSString *)stringByEscapingForShell
{
- if (!(characterNature[' '] & whitespaceNature)) {
- //if space doesn't have the whitespace nature, clearly we need to build the nature array.
-
- //first, set all characters to zero.
- bzero(&characterNature, sizeof(characterNature));
-
- //then memorise which characters have the whitespace nature.
- characterNature['\a'] = whitespaceNature;
- characterNature['\t'] = whitespaceNature;
- characterNature['\n'] = whitespaceNature;
- characterNature['\v'] = whitespaceNature;
- characterNature['\f'] = whitespaceNature;
- characterNature['\r'] = whitespaceNature;
- characterNature[' '] = whitespaceNature;
- //NOTE: if you give more characters the whitespace nature, be sure to
- // update escapeNames below.
-
- //finally, memorise which characters have the unsafe (for shells) nature.
- characterNature['\\'] = shellUnsafeNature;
- characterNature['\''] = shellUnsafeNature;
- characterNature['"'] = shellUnsafeNature;
- characterNature['`'] = shellUnsafeNature;
- characterNature['!'] = shellUnsafeNature;
- characterNature['$'] = shellUnsafeNature;
- characterNature['&'] = shellUnsafeNature;
- characterNature['|'] = shellUnsafeNature;
- }
-
unsigned myLength = [self length];
unichar *myBuf = malloc(sizeof(unichar) * myLength);
if (!myBuf) return nil;
@@ -651,46 +644,8 @@
return result;
}
-static enum characterNatureMask regexpCharacterNature[USHRT_MAX+1] = {
-//this array is initialised such that the space character (0x20)
-// does not have the whitespace nature.
-//this was done for brevity, as the entire array is bzeroed and then
-// properly initialised in -stringByEscapingForShell below.
-0,0,0,0, 0,0,0,0, //0x00..0x07
-0,0,0,0, 0,0,0,0, //0x08..0x0f
-0,0,0,0, 0,0,0,0, //0x10..0x17
-0,0,0, //0x18..0x20
-};
- (NSString *)stringByEscapingForRegexp
{
- if (!(regexpCharacterNature[' '] & whitespaceNature)) {
- //if space doesn't have the whitespace nature, clearly we need to build the nature array.
-
- //first, set all characters to zero.
- bzero(®expCharacterNature, sizeof(regexpCharacterNature));
-
- //then memorise which characters have the whitespace nature.
- regexpCharacterNature[' '] = whitespaceNature;
- //NOTE: if you give more characters the whitespace nature, be sure to
- // update escapeNames below.
-
- //finally, memorise which characters have the unsafe (for regexp) nature.
- regexpCharacterNature['\\'] = regexpUnsafeNature;
- regexpCharacterNature['/'] = regexpUnsafeNature;
- regexpCharacterNature['|'] = regexpUnsafeNature;
- regexpCharacterNature['.'] = regexpUnsafeNature;
- regexpCharacterNature['*'] = regexpUnsafeNature;
- regexpCharacterNature['+'] = regexpUnsafeNature;
- regexpCharacterNature['?'] = regexpUnsafeNature;
- regexpCharacterNature['{'] = regexpUnsafeNature;
- regexpCharacterNature['}'] = regexpUnsafeNature;
- regexpCharacterNature['('] = regexpUnsafeNature;
- regexpCharacterNature[')'] = regexpUnsafeNature;
- regexpCharacterNature['['] = regexpUnsafeNature;
- regexpCharacterNature['$'] = regexpUnsafeNature;
- regexpCharacterNature['^'] = regexpUnsafeNature;
- }
-
unsigned myLength = [self length];
unichar *myBuf = malloc(sizeof(unichar) * myLength);
if (!myBuf) return nil;
@@ -731,7 +686,7 @@
for (; myLength--; ++i) {
SBEFR_BOUNDARY_GUARD;
- if (regexpCharacterNature[*myBufPtr] & regexpUnsafeNature) {
+ if (characterNature[*myBufPtr] & regexpUnsafeNature) {
//escape this character
buf[i++] = '\\';
SBEFR_BOUNDARY_GUARD;
diff -r 490052ed56a6 -r 3571e5f41300 UnitTests/TestStringAdditions.m
--- a/UnitTests/TestStringAdditions.m Fri Aug 21 22:10:39 2009 -0400
+++ b/UnitTests/TestStringAdditions.m Fri Aug 21 20:25:45 2009 -0700
@@ -110,14 +110,14 @@
STAssertEqualObjects([@" " stringByEscapingForShell], @"\\ ", @"-stringByEscapingForShell didn't properly escape the space character");
//Other unsafe characters are simply backslash-escaped.
- STAssertEqualObjects([@"\\" stringByEscapingForShell], @"\\\\", @"-stringByEscapingForShell didn't properly escape the alert (bell) character");
- STAssertEqualObjects([@"'" stringByEscapingForShell], @"\\'", @"-stringByEscapingForShell didn't properly escape the horizontal tab character");
- STAssertEqualObjects([@"\"" stringByEscapingForShell], @"\\\"", @"-stringByEscapingForShell didn't properly escape the line-feed character");
- STAssertEqualObjects([@"`" stringByEscapingForShell], @"\\`", @"-stringByEscapingForShell didn't properly escape the vertical tab character");
- STAssertEqualObjects([@"!" stringByEscapingForShell], @"\\!", @"-stringByEscapingForShell didn't properly escape the form-feed character");
- STAssertEqualObjects([@"$" stringByEscapingForShell], @"\\$", @"-stringByEscapingForShell didn't properly escape the carriage-return character");
- STAssertEqualObjects([@"&" stringByEscapingForShell], @"\\&", @"-stringByEscapingForShell didn't properly escape the space character");
- STAssertEqualObjects([@"|" stringByEscapingForShell], @"\\|", @"-stringByEscapingForShell didn't properly escape the space character");
+ STAssertEqualObjects([@"\\" stringByEscapingForShell], @"\\\\", @"-stringByEscapingForShell didn't properly escape the backslash character");
+ STAssertEqualObjects([@"'" stringByEscapingForShell], @"\\'", @"-stringByEscapingForShell didn't properly escape the apostrophe/single-quotation-mark character");
+ STAssertEqualObjects([@"\"" stringByEscapingForShell], @"\\\"", @"-stringByEscapingForShell didn't properly escape the quotation-mark character");
+ STAssertEqualObjects([@"`" stringByEscapingForShell], @"\\`", @"-stringByEscapingForShell didn't properly escape the backquote character");
+ STAssertEqualObjects([@"!" stringByEscapingForShell], @"\\!", @"-stringByEscapingForShell didn't properly escape the bang character");
+ STAssertEqualObjects([@"$" stringByEscapingForShell], @"\\$", @"-stringByEscapingForShell didn't properly escape the dollar-sign character");
+ STAssertEqualObjects([@"&" stringByEscapingForShell], @"\\&", @"-stringByEscapingForShell didn't properly escape the ampersand character");
+ STAssertEqualObjects([@"|" stringByEscapingForShell], @"\\|", @"-stringByEscapingForShell didn't properly escape the pipe character");
}
- (void)testVolumePath
{
More information about the commits
mailing list