From 628a1be7997f13cf7c44dc3df215557e32d4e74f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Tue, 20 Jul 2021 22:52:39 +0800 Subject: Anticipate situation before there's any address info --- pikul.c | 62 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/pikul.c b/pikul.c index 4a8245e..0a170b7 100644 --- a/pikul.c +++ b/pikul.c @@ -191,27 +191,34 @@ char *pikul_html(const char *origin, const char *destination, double weight, char *html; if (!strcmp(widget, "select")) { char *options = NULL; - for (size_t i = 0; i < services->length; i++) { - struct pikul_service *service = services->list[i]; - char *code_prefix = code_prefixes[shipping.company]; - char *name_prefix = name_prefixes[shipping.company]; - size_t code_length = strlen(code_prefix) + strlen(service->code); - char code[code_length + 1]; - sprintf(code, "%s%s", code_prefix, service->code); - _Bool selected = !strcmp(code, value); - size_t length = strlen(OPTION) + code_length + (selected ? strlen(" selected") : 0) - + strlen(name_prefix) + strlen(service->name) - - OPTION_NUM_PARAMS * strlen("%s"); - char option[length + 1]; - sprintf(option, OPTION, code, selected ? " selected" : "", - name_prefix, service->name); - if (options) - options = realloc(options, strlen(options) + length + 1); - else { - options = malloc(length + 1); - memset(options, '\0', strlen(options)); - } - strcat(options, option); + if (!services || !services->length) { + static const char *empty = ""; + options = malloc(strlen(empty) + 1); + strcpy(options, empty); + } else { + for (size_t i = 0; i < services->length; i++) { + struct pikul_service *service = services->list[i]; + char *code_prefix = code_prefixes[shipping.company]; + char *name_prefix = name_prefixes[shipping.company]; + size_t code_length = strlen(code_prefix) + strlen(service->code); + char code[code_length + 1]; + sprintf(code, "%s%s", code_prefix, service->code); + _Bool selected = !strcmp(code, value); + size_t length = strlen(OPTION) + code_length + + (selected ? strlen(" selected") : 0) + + strlen(name_prefix) + strlen(service->name) + - OPTION_NUM_PARAMS * strlen("%s"); + char option[length + 1]; + sprintf(option, OPTION, code, selected ? " selected" : "", + name_prefix, service->name); + if (options) + options = realloc(options, strlen(options) + length + 1); + else { + options = malloc(length + 1); + memset(options, '\0', strlen(options)); + } + strcat(options, option); + } } html = malloc(strlen(SELECT) + strlen(name) + (extra ? strlen(extra) : 0) + strlen(options) - SELECT_NUM_PARAMS * strlen("%s") + 1); @@ -293,6 +300,19 @@ void pikul_cleanup() default: break; } + /* + if (shipping.data) + switch (shipping.mode) { + case SERVICES: + pikul_free_services(shipping.data); + break; + case ORDER: + free(shipping.data); + break; + default: + break; + } + */ free(shipping.base); json_tokener_free(tokener); curl_slist_free_all(shipping.headers); -- cgit v1.2.3