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 *
626
                 continue;
626
                 continue;
627
             }
627
             }
628
             
628
             
629
-            /* Load optional fields */
629
+            /* Load optional fields - clear errors after each since missing optional fields are not errors */
630
-            if (toml_get_string(doc, sections[i], "description", 
630
+            if (toml_get_string(doc, sections[i], "description",
631
                                account.description, sizeof(account.description)) != 0) {
631
                                account.description, sizeof(account.description)) != 0) {
632
                 /* Use name as description if not provided */
632
                 /* Use name as description if not provided */
633
                 safe_strncpy(account.description, account.name, sizeof(account.description));
633
                 safe_strncpy(account.description, account.name, sizeof(account.description));
634
+                clear_error();
634
             }
635
             }
635
-            
636
+
636
             if (toml_get_string(doc, sections[i], "preferred_scope", temp_str, sizeof(temp_str)) == 0) {
637
             if (toml_get_string(doc, sections[i], "preferred_scope", temp_str, sizeof(temp_str)) == 0) {
637
                 account.preferred_scope = config_parse_scope(temp_str);
638
                 account.preferred_scope = config_parse_scope(temp_str);
639
+            } else {
640
+                clear_error();
638
             }
641
             }
639
-            
642
+
640
             /* SSH configuration */
643
             /* SSH configuration */
641
-            if (toml_get_string(doc, sections[i], "ssh_key", 
644
+            if (toml_get_string(doc, sections[i], "ssh_key",
642
                                account.ssh_key_path, sizeof(account.ssh_key_path)) == 0 &&
645
                                account.ssh_key_path, sizeof(account.ssh_key_path)) == 0 &&
643
                 strlen(account.ssh_key_path) > 0) {
646
                 strlen(account.ssh_key_path) > 0) {
644
                 account.ssh_enabled = true;
647
                 account.ssh_enabled = true;
645
-                
648
+
646
                 /* Expand path if needed */
649
                 /* Expand path if needed */
647
                 char expanded_path[MAX_PATH_LEN];
650
                 char expanded_path[MAX_PATH_LEN];
648
                 if (expand_path(account.ssh_key_path, expanded_path, sizeof(expanded_path)) == 0) {
651
                 if (expand_path(account.ssh_key_path, expanded_path, sizeof(expanded_path)) == 0) {
649
                     safe_strncpy(account.ssh_key_path, expanded_path, sizeof(account.ssh_key_path));
652
                     safe_strncpy(account.ssh_key_path, expanded_path, sizeof(account.ssh_key_path));
650
                 }
653
                 }
651
-                
654
+
652
                 /* Optional SSH host alias */
655
                 /* Optional SSH host alias */
653
-                toml_get_string(doc, sections[i], "ssh_host", 
656
+                if (toml_get_string(doc, sections[i], "ssh_host",
654
-                               account.ssh_host_alias, sizeof(account.ssh_host_alias));
657
+                                   account.ssh_host_alias, sizeof(account.ssh_host_alias)) != 0) {
658
+                    clear_error();
659
+                }
660
+            } else {
661
+                clear_error();
655
             }
662
             }
656
-            
663
+
657
             /* GPG configuration */
664
             /* GPG configuration */
658
-            if (toml_get_string(doc, sections[i], "gpg_key", 
665
+            if (toml_get_string(doc, sections[i], "gpg_key",
659
                                account.gpg_key_id, sizeof(account.gpg_key_id)) == 0 &&
666
                                account.gpg_key_id, sizeof(account.gpg_key_id)) == 0 &&
660
                 strlen(account.gpg_key_id) > 0) {
667
                 strlen(account.gpg_key_id) > 0) {
661
                 account.gpg_enabled = true;
668
                 account.gpg_enabled = true;
662
-                
669
+
663
                 /* GPG signing preference */
670
                 /* 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 {
665
                     account.gpg_signing_enabled = temp_bool;
674
                     account.gpg_signing_enabled = temp_bool;
666
                 }
675
                 }
676
+            } else {
677
+                clear_error();
667
             }
678
             }
668
             
679
             
669
             /* Validate and add account */
680
             /* 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
238
 }
238
 }
239
 
239
 
240
 /* Get string value from TOML document */
240
 /* 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,
242
                     const char *key, char *value, size_t value_size) {
242
                     const char *key, char *value, size_t value_size) {
243
     const toml_section_t *sec;
243
     const toml_section_t *sec;
244
     const toml_keyvalue_t *kv;
244
     const toml_keyvalue_t *kv;
245
-    
245
+
246
     if (!doc || !section || !key || !value || value_size == 0) {
246
     if (!doc || !section || !key || !value || value_size == 0) {
247
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_string");
247
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_string");
248
         return -1;
248
         return -1;
249
     }
249
     }
250
-    
250
+
251
     if (!doc->is_valid) {
251
     if (!doc->is_valid) {
252
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
252
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
253
         return -1;
253
         return -1;
254
     }
254
     }
255
-    
255
+
256
     sec = find_section((toml_document_t *)doc, section);
256
     sec = find_section((toml_document_t *)doc, section);
257
     if (!sec) {
257
     if (!sec) {
258
-        set_error(ERR_CONFIG_INVALID, "Section not found: %s", section);
258
+        /* Section not found - return silently, caller handles missing data */
259
         return -1;
259
         return -1;
260
     }
260
     }
261
-    
261
+
262
     kv = find_key((toml_section_t *)sec, key);
262
     kv = find_key((toml_section_t *)sec, key);
263
     if (!kv || !kv->is_set) {
263
     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 */
265
         return -1;
265
         return -1;
266
     }
266
     }
267
-    
267
+
268
     if (kv->type != TOML_TYPE_STRING) {
268
     if (kv->type != TOML_TYPE_STRING) {
269
         set_error(ERR_CONFIG_INVALID, "Key %s.%s is not a string", section, key);
269
         set_error(ERR_CONFIG_INVALID, "Key %s.%s is not a string", section, key);
270
         return -1;
270
         return -1;
271
     }
271
     }
272
-    
272
+
273
     /* Sanitize the value before returning */
273
     /* Sanitize the value before returning */
274
     return toml_sanitize_string(kv->value, value, value_size);
274
     return toml_sanitize_string(kv->value, value, value_size);
275
 }
275
 }
276
 
276
 
277
 /* Get integer value from TOML document */
277
 /* 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,
279
                      const char *key, int *value) {
279
                      const char *key, int *value) {
280
     const toml_section_t *sec;
280
     const toml_section_t *sec;
281
     const toml_keyvalue_t *kv;
281
     const toml_keyvalue_t *kv;
282
     char *endptr;
282
     char *endptr;
283
     long parsed_value;
283
     long parsed_value;
284
-    
284
+
285
     if (!doc || !section || !key || !value) {
285
     if (!doc || !section || !key || !value) {
286
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_integer");
286
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_integer");
287
         return -1;
287
         return -1;
288
     }
288
     }
289
-    
289
+
290
     if (!doc->is_valid) {
290
     if (!doc->is_valid) {
291
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
291
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
292
         return -1;
292
         return -1;
293
     }
293
     }
294
-    
294
+
295
     sec = find_section((toml_document_t *)doc, section);
295
     sec = find_section((toml_document_t *)doc, section);
296
     if (!sec) {
296
     if (!sec) {
297
-        set_error(ERR_CONFIG_INVALID, "Section not found: %s", section);
297
+        /* Section not found - return silently, caller handles missing data */
298
         return -1;
298
         return -1;
299
     }
299
     }
300
-    
300
+
301
     kv = find_key((toml_section_t *)sec, key);
301
     kv = find_key((toml_section_t *)sec, key);
302
     if (!kv || !kv->is_set) {
302
     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 */
304
         return -1;
304
         return -1;
305
     }
305
     }
306
     
306
     
@@ -327,30 +327,30 @@ int toml_get_integer(const toml_document_t *doc, const char *section,
327
 }
327
 }
328
 
328
 
329
 /* Get boolean value from TOML document */
329
 /* 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,
331
                      const char *key, bool *value) {
331
                      const char *key, bool *value) {
332
     const toml_section_t *sec;
332
     const toml_section_t *sec;
333
     const toml_keyvalue_t *kv;
333
     const toml_keyvalue_t *kv;
334
-    
334
+
335
     if (!doc || !section || !key || !value) {
335
     if (!doc || !section || !key || !value) {
336
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_boolean");
336
         set_error(ERR_INVALID_ARGS, "Invalid arguments to toml_get_boolean");
337
         return -1;
337
         return -1;
338
     }
338
     }
339
-    
339
+
340
     if (!doc->is_valid) {
340
     if (!doc->is_valid) {
341
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
341
         set_error(ERR_CONFIG_INVALID, "TOML document is not valid");
342
         return -1;
342
         return -1;
343
     }
343
     }
344
-    
344
+
345
     sec = find_section((toml_document_t *)doc, section);
345
     sec = find_section((toml_document_t *)doc, section);
346
     if (!sec) {
346
     if (!sec) {
347
-        set_error(ERR_CONFIG_INVALID, "Section not found: %s", section);
347
+        /* Section not found - return silently, caller handles missing data */
348
         return -1;
348
         return -1;
349
     }
349
     }
350
-    
350
+
351
     kv = find_key((toml_section_t *)sec, key);
351
     kv = find_key((toml_section_t *)sec, key);
352
     if (!kv || !kv->is_set) {
352
     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 */
354
         return -1;
354
         return -1;
355
     }
355
     }
356
     
356