-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add JQ_COLORS env var for color config (fix #1252) #1397
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
Conversation
This addresses #1252. |
src/jv_print.c
Outdated
if (c == NULL) | ||
return 1; | ||
colors = def_colors; | ||
memset(color_bufs, 0, sizeof(colors)); |
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.
Should this be sizeof(color_bufs)
?
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.
Oy, yes.
src/jv_print.c
Outdated
for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]) && *c != '\0'; i++, c = e) { | ||
if ((e = strchr(c, ':')) == NULL) | ||
e = c + strlen(c); | ||
if ((size_t)(e - c) > sizeof(color_bufs[i]) - 1) |
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.
This should be sizeof(color_bufs[i])-1-3
(one for the NUL
, and 3 for the \033[m
)
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.
Thanks! (Though I attempted to test this.)
src/jv_print.c
Outdated
return 0; | ||
color_bufs[i][0] = ESC[0]; | ||
color_bufs[i][1] = '['; | ||
(void) strncpy(&color_bufs[i][2], c, e - c); |
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.
This can overflow without the fix above (admittedly only by two bytes, but that's an overflow)
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.
It certainly doesn't overflow now (there's a test).
src/jv_print.c
Outdated
(void) strncpy(&color_bufs[i][2], c, e - c); | ||
if (strspn(&color_bufs[i][2], "0123456789;") < strlen(&color_bufs[i][2])) | ||
return 0; | ||
color_bufs[i][2 + e - c] = 'm'; |
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.
Same. 2+15 = 17
, which is 2 past the end of your 16-byte buffer.
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.
Also, you're not guaranteed to write a NUL
byte into color_bufs[i][3 + e - c]
. Let's explicitly do that so we can't run off the end 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.
That's what the memset() is for.
src/jv_print.c
Outdated
memset(color_bufs, 0, sizeof(colors)); | ||
for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]); i++) | ||
color_bufps[i] = def_colors[i]; | ||
for (i = 0; i < sizeof(def_colors) / sizeof(def_colors[0]) && *c != '\0'; i++, c = e) { |
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.
Here, are we wanting to iterate across all 8 possible color_buf
s or only the 7 we default with?
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.
Will fix.
src/jv_print.c
Outdated
@@ -27,11 +27,46 @@ | |||
static const jv_kind color_kinds[] = | |||
{JV_KIND_NULL, JV_KIND_FALSE, JV_KIND_TRUE, JV_KIND_NUMBER, | |||
JV_KIND_STRING, JV_KIND_ARRAY, JV_KIND_OBJECT}; | |||
static const char* const colors[] = | |||
static char color_bufs[8][16]; |
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.
Why 8 if there are only 7 def_colors
?
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.
Uh, no reason. I'll change it.
tests/shtest
Outdated
cmp $d/color $d/expect | ||
JQ_COLORS='1;31234567890123456789:0;31:0;32:0;33:0;34:1;35:1;36' \ | ||
$JQ -Ccn '[{"a":true,"b":false},123,null]' > $d/color | ||
cmp $d/color $d/expect |
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.
Let's also add a test where we expect failure with a strings like 12345678901234:12345678901234:12345678901234:12345678901234:12345678901234:12345678901234:12345678901234
(for buffer overrun)
and 1234567890123:1234567890123:1234567890123:1234567890123:1234567890123:1234567890123:1234567890123
(for lack of trailing NUL)
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.
Good idea.
Fixes pushed. |
Thanks! |
This commit adds an env var,
JQ_COLORS
, that allows the user to specify an alternative color scheme.You can't see it here, but this produces something of a rainbow: