Browse Source

Make the old json conf parser parse dictionaries forward so that --cleanconf stops reversing dicts, 2. make sure old parser is used any time there is no v2, to ensure continuity of behavior

Caleb James DeLisle 5 years ago
parent
commit
dd41671f2f
2 changed files with 20 additions and 10 deletions
  1. 12 9
      benc/serialization/json/JsonBencSerializer.c
  2. 8 1
      client/cjdroute2.c

+ 12 - 9
benc/serialization/json/JsonBencSerializer.c

@@ -397,8 +397,8 @@ static int32_t parseDictionary(struct Reader* reader,
 
     String* key;
     Object* value;
-    struct Dict_Entry* entryPointer;
-    struct Dict_Entry* lastEntryPointer = NULL;
+    struct Dict_Entry* first = NULL;
+    struct Dict_Entry* last = NULL;
     int ret = 0;
 
     for (;;) {
@@ -410,7 +410,7 @@ static int32_t parseDictionary(struct Reader* reader,
 
                 case '}':
                     Reader_skip(reader, 1);
-                    *output = lastEntryPointer;
+                    *output = first;
                     return 0;
 
                 case '/':
@@ -440,12 +440,15 @@ static int32_t parseDictionary(struct Reader* reader,
         }
 
         /* Allocate the entry. */
-        entryPointer = Allocator_malloc(allocator, sizeof(struct Dict_Entry));
-
-        entryPointer->next = lastEntryPointer;
-        entryPointer->key = key;
-        entryPointer->val = value;
-        lastEntryPointer = entryPointer;
+        struct Dict_Entry* entry = Allocator_calloc(allocator, sizeof(struct Dict_Entry), 1);
+        entry->key = key;
+        entry->val = value;
+        if (last) {
+            last->next = entry;
+        } else {
+            first = entry;
+        }
+        last = entry;
     }
 }
 

+ 8 - 1
client/cjdroute2.c

@@ -406,7 +406,7 @@ static int genconf(struct Random* rand, bool eth)
     printf("\n"
            "    // This is to make the configuration be parsed in strict mode, which allows\n"
            "    // it to be edited externally using cjdnsconf.\n"
-           "    \"version\": 2");
+           "    \"version\": 2\n");
     printf("}\n");
 
     return 0;
@@ -635,6 +635,11 @@ int main(int argc, char** argv)
     Dict _config;
     Dict* config = &_config;
     char* err = JsonBencMessageReader_readNoExcept(confMsg, allocator, &config, false);
+    if (!err) {
+        // If old version is specified, always use old parser so there is no possible error
+        uint64_t* v = Dict_getIntC(config, "version");
+        if (!v || *v < 2) { err = "using old parser"; }
+    }
     if (err) {
         if (JsonBencSerializer_get()->parseDictionary(confReader, allocator, &_config)) {
             fprintf(stderr, "Failed to parse configuration.\n%s\n", err);
@@ -648,6 +653,8 @@ int main(int argc, char** argv)
     }
 
     if (argc == 2 && CString_strcmp(argv[1], "--cleanconf") == 0) {
+        // Slip a v2 in there because at this point, the conf file is definitely v2 valid
+        Dict_putIntC(config, "version", 2, allocator);
         struct Writer* stdoutWriter = FileWriter_new(stdout, allocator);
         JsonBencSerializer_get()->serializeDictionary(stdoutWriter, config);
         printf("\n");