-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7831 from ipfs/fix/escape-nonprintable-chars
Escape non-printable characters in user output
- Loading branch information
Showing
13 changed files
with
131 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmdenv | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestEscNonPrint(t *testing.T) { | ||
b := []byte("hello") | ||
b[2] = 0x7f | ||
s := string(b) | ||
if !needEscape(s) { | ||
t.Fatal("string needs escaping") | ||
} | ||
if !hasNonPrintable(s) { | ||
t.Fatal("expected non-printable") | ||
} | ||
if hasNonPrintable(EscNonPrint(s)) { | ||
t.Fatal("escaped string has non-printable") | ||
} | ||
if EscNonPrint(`hel\lo`) != `hel\\lo` { | ||
t.Fatal("backslash not escaped") | ||
} | ||
|
||
s = `hello` | ||
if needEscape(s) { | ||
t.Fatal("string does not need escaping") | ||
} | ||
if EscNonPrint(s) != s { | ||
t.Fatal("string should not have changed") | ||
} | ||
s = `"hello"` | ||
if EscNonPrint(s) != s { | ||
t.Fatal("string should not have changed") | ||
} | ||
if EscNonPrint(`"hel\"lo"`) != `"hel\\"lo"` { | ||
t.Fatal("did not get expected escaped string") | ||
} | ||
} | ||
|
||
func hasNonPrintable(s string) bool { | ||
for _, r := range s { | ||
if !strconv.IsPrint(r) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.