Skip to content

json output not properly quoted #340

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

Open
baikal opened this issue Apr 4, 2025 · 7 comments
Open

json output not properly quoted #340

baikal opened this issue Apr 4, 2025 · 7 comments

Comments

@baikal
Copy link

baikal commented Apr 4, 2025

duc json somepath

emits json where at least " and are not properly quoted by in the json, which makes json invalid ...

@l8gravely
Copy link
Collaborator

l8gravely commented Apr 4, 2025 via email

@baikal
Copy link
Author

baikal commented Apr 4, 2025

The following patch solves it for " (double quote), TAB, CR and LF characters. Although there might be more (all < ascii 32 ?).

diff --git a/src/duc/cmd-json.c b/src/duc/cmd-json.c
index be26851..d257ed4 100644
--- a/src/duc/cmd-json.c
+++ b/src/duc/cmd-json.c
@@ -33,10 +33,10 @@ static void print_escaped(const char *s)
 {
        while(*s) {
                switch(*s) {
-                       case '"': printf("\""); break;
-                       case '\t': putchar('\t'); break;
-                       case '\n': putchar('\n'); break;
-                       case '\r': putchar('\r'); break;
+                        case '"': printf("\\\""); break;
+                        case '\t': printf("\\t"); break;
+                        case '\n': printf("\\n"); break;
+                        case '\r': printf("\\r"); break;
                        default: putchar(*s); break;
                }
                s++;

@baikal
Copy link
Author

baikal commented Apr 4, 2025

wait, make that the following, according to here:

diff --git a/src/duc/cmd-json.c b/src/duc/cmd-json.c
index be26851..d8c4fa1 100644
--- a/src/duc/cmd-json.c
+++ b/src/duc/cmd-json.c
@@ -33,10 +33,13 @@ static void print_escaped(const char *s)
 {
        while(*s) {
                switch(*s) {
-                       case '"': printf("\""); break;
-                       case '\t': putchar('\t'); break;
-                       case '\n': putchar('\n'); break;
-                       case '\r': putchar('\r'); break;
+                        case '"': printf("\\\""); break;
+                        case '\\': printf("\\\\"); break;
+                        case '\t': printf("\\t"); break;
+                        case '\n': printf("\\n"); break;
+                        case '\r': printf("\\r"); break;
+                        case '\b': printf("\\b"); break;
+                        case '\f': printf("\\f"); break;
                        default: putchar(*s); break;
                }
                s++;

@baikal
Copy link
Author

baikal commented Apr 4, 2025

testfiles.zip

@zevv
Copy link
Owner

zevv commented Apr 4, 2025

haha yes the original escaping was just utter nonsense, i wonder how we let that creep in without noticing. thanks for reporting!

@l8gravely
Copy link
Collaborator

l8gravely commented Apr 5, 2025 via email

@zevv
Copy link
Owner

zevv commented Apr 5, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants