Skip to content

Commit

Permalink
Escaped @ ("\@") is treated as "@" (doesn't read file)
Browse files Browse the repository at this point in the history
	closes #103
  • Loading branch information
jpmens committed Nov 4, 2019
1 parent 1ca2a07 commit ef3b05f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
72 changes: 38 additions & 34 deletions jo.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "base64.h"

/*
* Copyright (C) 2016 Jan-Piet Mens <[email protected]>
* Copyright (C) 2016-2019 Jan-Piet Mens <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -255,43 +255,47 @@ JsonNode *vnode(char *str, int flags)
}
}

if (*str == '@' || *str == '%' || *str == ':') {
char *filename = str + 1, *content;
bool binmode = (*str == '%');
bool jsonmode = (*str == ':');
size_t len = 0;
JsonNode *j = NULL;

if ((content = slurp_file(filename, &len, false)) == NULL) {
errx(1, "Error reading file %s", filename);
}

if (binmode) {
char *encoded;

if ((encoded = base64_encode(content, len)) == NULL) {
errx(1, "Cannot base64-encode file %s", filename);
if (*str == '\\') {
++str;
} else {
if (*str == '@' || *str == '%' || *str == ':') {
char *filename = str + 1, *content;
bool binmode = (*str == '%');
bool jsonmode = (*str == ':');
size_t len = 0;
JsonNode *j = NULL;

if ((content = slurp_file(filename, &len, false)) == NULL) {
errx(1, "Error reading file %s", filename);
}

j = json_mkstring(encoded);
free(encoded);
} else if (jsonmode) {
j = json_decode(content);

if (binmode) {
char *encoded;

if ((encoded = base64_encode(content, len)) == NULL) {
errx(1, "Cannot base64-encode file %s", filename);
}

j = json_mkstring(encoded);
free(encoded);
} else if (jsonmode) {
j = json_decode(content);
if (j == NULL) {
errx(1, "Cannot decode JSON in file %s", filename);
}
}

// If it got this far without valid JSON, just consider it a string
if (j == NULL) {
errx(1, "Cannot decode JSON in file %s", filename);
char *bp = content + strlen(content) - 1;

if (*bp == '\n') *bp-- = 0;
if (*bp == '\r') *bp = 0;
j = json_mkstring(content);
}
free(content);
return (j);
}

// If it got this far without valid JSON, just consider it a string
if (j == NULL) {
char *bp = content + strlen(content) - 1;

if (*bp == '\n') *bp-- = 0;
if (*bp == '\r') *bp = 0;
j = json_mkstring(content);
}
free(content);
return (j);
}

if (*str == '{' || *str == '[') {
Expand Down
1 change: 1 addition & 0 deletions tests/jo.22.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"key":"@timestamp"}
4 changes: 4 additions & 0 deletions tests/jo.22.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# avoid reading from file if escaped \@

${JO:-jo} key="\@timestamp"

0 comments on commit ef3b05f

Please sign in to comment.