tenseleyflow/gitswitch / 799ab87

Browse files

fix: silence errors for missing optional config fields

- toml_get_string/integer/boolean return -1 silently for missing keys
- config.c clears errors after optional field lookups for safety
Authored by espadonne
SHA
799ab87f3693db2423f8cccdfdce0fe7ed255971
Parents
63963be
Tree
5b1f2ab

2 changed files

StatusFile+-
M src/config.c 24 13
M src/toml_parser.c 23 23
src/config.cmodified
@@ -626,44 +626,55 @@ static int load_accounts_from_toml(gitswitch_ctx_t *ctx, const toml_document_t *
626626
                 continue;
627627
             }
628628
             
629
-            /* Load optional fields */
630
-            if (toml_get_string(doc, sections[i], "description", 
629
+            /* Load optional fields - clear errors after each since missing optional fields are not errors */
630
+            if (toml_get_string(doc, sections[i], "description",
631631
                                account.description, sizeof(account.description)) != 0) {
632632
                 /* Use name as description if not provided */
633633
                 safe_strncpy(account.description, account.name, sizeof(account.description));
634
+                clear_error();
634635
             }
635
-            
636
+
636637
             if (toml_get_string(doc, sections[i], "preferred_scope", temp_str, sizeof(temp_str)) == 0) {
637638
                 account.preferred_scope = config_parse_scope(temp_str);
639
+            } else {
640
+                clear_error();
638641
             }
639
-            
642
+
640643
             /* SSH configuration */
641
-            if (toml_get_string(doc, sections[i], "ssh_key", 
644
+            if (toml_get_string(doc, sections[i], "ssh_key",
642645
                                account.ssh_key_path, sizeof(account.ssh_key_path)) == 0 &&
643646
                 strlen(account.ssh_key_path) > 0) {
644647
                 account.ssh_enabled = true;
645
-                
648
+
646649
                 /* Expand path if needed */
647650
                 char expanded_path[MAX_PATH_LEN];
648651
                 if (expand_path(account.ssh_key_path, expanded_path, sizeof(expanded_path)) == 0) {
649652
                     safe_strncpy(account.ssh_key_path, expanded_path, sizeof(account.ssh_key_path));
650653
                 }
651
-                
654
+
652655
                 /* Optional SSH host alias */
653
-                toml_get_string(doc, sections[i], "ssh_host", 
654
-                               account.ssh_host_alias, sizeof(account.ssh_host_alias));
656
+                if (toml_get_string(doc, sections[i], "ssh_host",
657
+                                   account.ssh_host_alias, sizeof(account.ssh_host_alias)) != 0) {
658
+                    clear_error();
659
+                }
660
+            } else {
661
+                clear_error();
655662
             }
656
-            
663
+
657664
             /* GPG configuration */
658
-            if (toml_get_string(doc, sections[i], "gpg_key", 
665
+            if (toml_get_string(doc, sections[i], "gpg_key",
659666
                                account.gpg_key_id, sizeof(account.gpg_key_id)) == 0 &&
660667
                 strlen(account.gpg_key_id) > 0) {
661668
                 account.gpg_enabled = true;
662
-                
669
+
663670
                 /* GPG signing preference */
664
-                if (toml_get_boolean(doc, sections[i], "gpg_signing_enabled", &temp_bool) == 0) {
671
+                if (toml_get_boolean(doc, sections[i], "gpg_signing_enabled", &temp_bool) != 0) {
672
+                    clear_error();
673
+                } else {
665674
                     account.gpg_signing_enabled = temp_bool;
666675
                 }
676
+            } else {
677
+                clear_error();
667678
             }
668679
             
669680
             /* Validate and add account */
src/toml_parser.cmodified
@@ -238,69 +238,69 @@ int toml_parse_string(const char *toml_string, size_t length, toml_document_t *d
238238
 }
239239
 
240240
 /* Get string value from TOML document */
241
-int toml_get_string(const toml_document_t *doc, const char *section, 
241
+int toml_get_string(const toml_document_t *doc, const char *section,
242242
                     const char *key, char *value, size_t value_size) {
243243
     const toml_section_t *sec;
244244
     const toml_keyvalue_t *kv;
245
-    
245
+
246246
     if (!doc || !section || !key || !value || value_size == 0) {
247247
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_string");
248248
         return -1;
249249
     }
250
-    
250
+
251251
     if (!doc->is_valid) {
252252
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
253253
         return -1;
254254
     }
255
-    
255
+
256256
     sec = find_section((toml_document_t *)doc, section);
257257
     if (!sec) {
258
-        set_error(ERR_CONFIG_INVALID, "Section not found: %s", section);
258
+        /* Section not found - return silently, caller handles missing data */
259259
         return -1;
260260
     }
261
-    
261
+
262262
     kv = find_key((toml_section_t *)sec, key);
263263
     if (!kv || !kv->is_set) {
264
-        set_error(ERR_CONFIG_INVALID, "Key not found: %s.%s", section, key);
264
+        /* Key not found - return silently, caller handles missing data */
265265
         return -1;
266266
     }
267
-    
267
+
268268
     if (kv->type != TOML_TYPE_STRING) {
269269
         set_error(ERR_CONFIG_INVALID, "Key %s.%s is not a string", section, key);
270270
         return -1;
271271
     }
272
-    
272
+
273273
     /* Sanitize the value before returning */
274274
     return toml_sanitize_string(kv->value, value, value_size);
275275
 }
276276
 
277277
 /* Get integer value from TOML document */
278
-int toml_get_integer(const toml_document_t *doc, const char *section, 
278
+int toml_get_integer(const toml_document_t *doc, const char *section,
279279
                      const char *key, int *value) {
280280
     const toml_section_t *sec;
281281
     const toml_keyvalue_t *kv;
282282
     char *endptr;
283283
     long parsed_value;
284
-    
284
+
285285
     if (!doc || !section || !key || !value) {
286286
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_integer");
287287
         return -1;
288288
     }
289
-    
289
+
290290
     if (!doc->is_valid) {
291291
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
292292
         return -1;
293293
     }
294
-    
294
+
295295
     sec = find_section((toml_document_t *)doc, section);
296296
     if (!sec) {
297
-        set_error(ERR_CONFIG_INVALID, "Section not found: %s", section);
297
+        /* Section not found - return silently, caller handles missing data */
298298
         return -1;
299299
     }
300
-    
300
+
301301
     kv = find_key((toml_section_t *)sec, key);
302302
     if (!kv || !kv->is_set) {
303
-        set_error(ERR_CONFIG_INVALID, "Key not found: %s.%s", section, key);
303
+        /* Key not found - return silently, caller handles missing data */
304304
         return -1;
305305
     }
306306
     
@@ -327,30 +327,30 @@ int toml_get_integer(const toml_document_t *doc, const char *section,
327327
 }
328328
 
329329
 /* Get boolean value from TOML document */
330
-int toml_get_boolean(const toml_document_t *doc, const char *section, 
330
+int toml_get_boolean(const toml_document_t *doc, const char *section,
331331
                      const char *key, bool *value) {
332332
     const toml_section_t *sec;
333333
     const toml_keyvalue_t *kv;
334
-    
334
+
335335
     if (!doc || !section || !key || !value) {
336336
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_boolean");
337337
         return -1;
338338
     }
339
-    
339
+
340340
     if (!doc->is_valid) {
341341
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
342342
         return -1;
343343
     }
344
-    
344
+
345345
     sec = find_section((toml_document_t *)doc, section);
346346
     if (!sec) {
347
-        set_error(ERR_CONFIG_INVALID, "Section not found: %s", section);
347
+        /* Section not found - return silently, caller handles missing data */
348348
         return -1;
349349
     }
350
-    
350
+
351351
     kv = find_key((toml_section_t *)sec, key);
352352
     if (!kv || !kv->is_set) {
353
-        set_error(ERR_CONFIG_INVALID, "Key not found: %s.%s", section, key);
353
+        /* Key not found - return silently, caller handles missing data */
354354
         return -1;
355355
     }
356356