* Auxiliary function to sanity-check and split the key into the section * identifier and variable name. * * Returns 0 on success, -1 when there is an invalid character in the key and * -2 if there is no section name in the key. * * store_key - pointer to char* which will hold a copy of the key with * lowercase section and variable name, can be NULL to skip * allocat
| 543 | * quiet - when non-zero, suppress error() reports on rejection |
| 544 | */ |
| 545 | static int do_parse_config_key(const char *key, char **store_key, |
| 546 | size_t *baselen_, int quiet) |
| 547 | { |
| 548 | size_t i, baselen; |
| 549 | int dot; |
| 550 | const char *last_dot = strrchr(key, '.'); |
| 551 | |
| 552 | /* |
| 553 | * Since "key" actually contains the section name and the real |
| 554 | * key name separated by a dot, we have to know where the dot is. |
| 555 | */ |
| 556 | |
| 557 | if (last_dot == NULL || last_dot == key) { |
| 558 | if (!quiet) |
| 559 | error(_("key does not contain a section: %s"), key); |
| 560 | return -CONFIG_NO_SECTION_OR_NAME; |
| 561 | } |
| 562 | |
| 563 | if (!last_dot[1]) { |
| 564 | if (!quiet) |
| 565 | error(_("key does not contain variable name: %s"), key); |
| 566 | return -CONFIG_NO_SECTION_OR_NAME; |
| 567 | } |
| 568 | |
| 569 | baselen = last_dot - key; |
| 570 | if (baselen_) |
| 571 | *baselen_ = baselen; |
| 572 | |
| 573 | /* |
| 574 | * Validate the key and while at it, lower case it for matching. |
| 575 | */ |
| 576 | if (store_key) |
| 577 | *store_key = xmallocz(strlen(key)); |
| 578 | |
| 579 | dot = 0; |
| 580 | for (i = 0; key[i]; i++) { |
| 581 | unsigned char c = key[i]; |
| 582 | if (c == '.') |
| 583 | dot = 1; |
| 584 | /* Leave the extended basename untouched.. */ |
| 585 | if (!dot || i > baselen) { |
| 586 | if (!iskeychar(c) || |
| 587 | (i == baselen + 1 && !isalpha(c))) { |
| 588 | if (!quiet) |
| 589 | error(_("invalid key: %s"), key); |
| 590 | goto out_free_ret_1; |
| 591 | } |
| 592 | c = tolower(c); |
| 593 | } else if (c == '\n') { |
| 594 | if (!quiet) |
| 595 | error(_("invalid key (newline): %s"), key); |
| 596 | goto out_free_ret_1; |
| 597 | } |
| 598 | if (store_key) |
| 599 | (*store_key)[i] = c; |
| 600 | } |
| 601 | |
| 602 | return 0; |
no test coverage detected