JSON: fix crashes in ${extract jsons } and ${extract json {digits} }
authorJeremy Harris <jgh146exb@wizmail.org>
Sat, 9 Feb 2019 14:57:46 +0000 (14:57 +0000)
committerJeremy Harris <jgh146exb@wizmail.org>
Sat, 9 Feb 2019 15:25:54 +0000 (15:25 +0000)
Broken-by: 386ab6457b and 8fdf20fd84
src/src/expand.c
test/scripts/0000-Basic/0002
test/stdout/0002

index ec5660adbdefb5752aa627ea831eb322a0f46250..aa4d0289e3804fc42af14dac32da0b249bc2ef9e 100644 (file)
@@ -5811,10 +5811,11 @@ while (*s != 0)
              }
            while (field_number > 0 && (item = json_nextinlist(&list)))
              field_number--;
-           s = item;
-           lookup_value = s;
-           while (*s) s++;
-           while (--s >= lookup_value && isspace(*s)) *s = '\0';
+           if ((lookup_value = s = item))
+             {
+             while (*s) s++;
+             while (--s >= lookup_value && isspace(*s)) *s = '\0';
+             }
            }
          else
            {
@@ -5850,14 +5851,16 @@ while (*s != 0)
            }
          }
 
-         if (fmt == extract_jsons)
-           if (!(lookup_value = dewrap(lookup_value, US"\"\"")))
-             {
-             expand_string_message =
-               string_sprintf("%s wrapping string result for extract jsons",
-                 expand_string_message);
-             goto EXPAND_FAILED_CURLY;
-             }
+         if (  fmt == extract_jsons
+            && lookup_value
+            && !(lookup_value = dewrap(lookup_value, US"\"\"")))
+           {
+           expand_string_message =
+             string_sprintf("%s wrapping string result for extract jsons",
+               expand_string_message);
+           goto EXPAND_FAILED_CURLY;
+           }
+         break;        /* json/s */
        }
 
       /* If no string follows, $value gets substituted; otherwise there can
index ad1910510a4af16ebe2d62c4492287bed68b9461..65ad69047d48ae0822e482dc9e9ff7a16d68611e 100644 (file)
@@ -895,6 +895,8 @@ ${extract json {2} {${extract json{IDs} {\{"other":"foo", "IDs": [116, 943, 234]
 
 ${extract json {2} {["red", "green", "blue", "black"]} }
 ${extract jsons{2} {["red", "green", "blue", "black"]} }
+<${extract jsons{5} {["red", "green", "blue", "black"]} }>
+expect: <>
 
 ${extract json {seconds} { \{"hours":0, "mins":0, "seconds":59\} }}
 ${extract json {seconds} {${extract json {2} { ["irrelevant", \{"hours":0, "mins":0, "seconds":59\}] }}}}
@@ -907,6 +909,8 @@ expect: {"1":116, "2":943, "3":234}
 
 <${extract json{nonexistent}{ \{"id": \{"a":101, "b":102\}, "IDs": \{"1":116, "2":943, "3":234\}\} }}>
 expect: <>
+<${extract jsons{nonexistent}{ \{"id": \{"a":101, "b":102\}, "IDs": \{"1":116, "2":943, "3":234\}\} }}>
+expect: <>
 
 ${if forany_json {[1, 2, 3]}{={$item}{1}}{yes}{no}}
 ${if forany_jsons{["A", "B", "C"]}{eq{$item}{B}}{yes}{no}}
index f53d9d563801b88c24484e9676ddcfc72f81119e..f5a97289a56fe37e129e642d6d14abd8edda705d 100644 (file)
@@ -838,6 +838,8 @@ xyz
 > 
 > "green"
 > green
+> <>
+> expect: <>
 > 
 > 59
 > 59
@@ -850,6 +852,8 @@ xyz
 > 
 > <>
 > expect: <>
+> <>
+> expect: <>
 > 
 > yes
 > yes