-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve nsexec logging #2836
Merged
cyphar
merged 2 commits into
opencontainers:master
from
kolyshkin:improve-nsexec-logging
Apr 13, 2021
Merged
Improve nsexec logging #2836
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#ifdef ESCAPE_TEST | ||
# include <assert.h> | ||
# define test_assert(arg) assert(arg) | ||
#else | ||
# define test_assert(arg) | ||
#endif | ||
|
||
#define DEL '\x7f' | ||
|
||
/* | ||
* Poor man version of itoa with base=16 and input number from 0 to 15, | ||
* represented by a char. Converts it to a single hex digit ('0' to 'f'). | ||
*/ | ||
static char hex(char i) | ||
{ | ||
test_assert(i >= 0 && i < 16); | ||
|
||
if (i >= 0 && i < 10) { | ||
return '0' + i; | ||
} | ||
if (i >= 10 && i < 16) { | ||
return 'a' + i - 10; | ||
} | ||
return '?'; | ||
} | ||
|
||
/* | ||
* Given the character, tells how many _extra_ characters are needed | ||
* to JSON-escape it. If 0 is returned, the character does not need to | ||
* be escaped. | ||
*/ | ||
static int need_escape(char c) | ||
{ | ||
switch (c) { | ||
case '\\': | ||
case '"': | ||
case '\b': | ||
case '\n': | ||
case '\r': | ||
case '\t': | ||
case '\f': | ||
return 1; | ||
case DEL: // -> \u007f | ||
return 5; | ||
default: | ||
if (c > 0 && c < ' ') { | ||
// ASCII decimal 01 to 31 -> \u00xx | ||
return 5; | ||
} | ||
return 0; | ||
} | ||
} | ||
|
||
/* | ||
* Escape the string so it can be used as a JSON string (per RFC4627, | ||
* section 2.5 minimal requirements, plus the DEL (0x7f) character). | ||
* | ||
* It is expected that the argument is a string allocated via malloc. | ||
* In case no escaping is needed, the original string is returned as is; | ||
* otherwise, the original string is free'd, and the newly allocated | ||
* escaped string is returned. Thus, in any case, the value returned | ||
* need to be free'd by the caller. | ||
*/ | ||
char *escape_json_string(char *s) | ||
{ | ||
int i, j, len; | ||
char *c, *out; | ||
|
||
/* | ||
* First, check if escaping is at all needed -- if not, we can avoid | ||
* malloc and return the argument as is. While at it, count how much | ||
* extra space is required. | ||
* | ||
* XXX: the counting code must be in sync with the escaping code | ||
* (checked by test_assert()s below). | ||
*/ | ||
for (i = j = 0; s[i] != '\0'; i++) { | ||
j += need_escape(s[i]); | ||
} | ||
if (j == 0) { | ||
// nothing to escape | ||
return s; | ||
} | ||
|
||
len = i + j + 1; | ||
out = malloc(len); | ||
if (!out) { | ||
free(s); | ||
// As malloc failed, strdup can fail, too, so in the worst case | ||
// scenario NULL will be returned from here. | ||
return strdup("escape_json_string: out of memory"); | ||
} | ||
for (c = s, j = 0; *c != '\0'; c++) { | ||
switch (*c) { | ||
case '"': | ||
case '\\': | ||
test_assert(need_escape(*c) == 1); | ||
out[j++] = '\\'; | ||
out[j++] = *c; | ||
continue; | ||
} | ||
if ((*c < 0 || *c >= ' ') && (*c != DEL)) { | ||
// no escape needed | ||
test_assert(need_escape(*c) == 0); | ||
out[j++] = *c; | ||
continue; | ||
} | ||
out[j++] = '\\'; | ||
switch (*c) { | ||
case '\b': | ||
out[j++] = 'b'; | ||
break; | ||
case '\n': | ||
out[j++] = 'n'; | ||
break; | ||
case '\r': | ||
out[j++] = 'r'; | ||
break; | ||
case '\t': | ||
out[j++] = 't'; | ||
break; | ||
case '\f': | ||
out[j++] = 'f'; | ||
break; | ||
default: | ||
test_assert(need_escape(*c) == 5); | ||
out[j++] = 'u'; | ||
out[j++] = '0'; | ||
out[j++] = '0'; | ||
out[j++] = hex(*c >> 4); | ||
out[j++] = hex(*c & 0x0f); | ||
} | ||
} | ||
test_assert(j + 1 == len); | ||
out[j] = '\0'; | ||
|
||
free(s); | ||
return out; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it makes more sense to always return NULL (and then in the caller provide the fallback error message), but it doesn't really matter imho.