diff options
-rw-r--r-- | include/usbg/usbg.h | 13 | ||||
-rw-r--r-- | src/usbg.c | 47 | ||||
-rw-r--r-- | src/usbg_schemes_libconfig.c | 7 | ||||
-rw-r--r-- | tests/test.c | 16 |
4 files changed, 55 insertions, 28 deletions
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index 86b9da7..180a265 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -28,6 +28,7 @@ #include <limits.h> #include <stdbool.h> #include <stdio.h> /* For FILE * */ +#include <malloc.h> #ifdef __cplusplus extern "C" { @@ -147,9 +148,9 @@ typedef enum { */ struct usbg_gadget_strs { - char manufacturer[USBG_MAX_STR_LENGTH]; - char product[USBG_MAX_STR_LENGTH]; - char serial[USBG_MAX_STR_LENGTH]; + char *manufacturer; + char *product; + char *serial; }; /** @@ -569,6 +570,12 @@ extern int usbg_get_gadget_strs(usbg_gadget *g, int lang, */ static inline void usbg_free_gadget_strs(struct usbg_gadget_strs *g_strs) { + if (g_strs) + return; + + free(g_strs->manufacturer); + free(g_strs->product); + free(g_strs->serial); } /** @@ -525,7 +525,7 @@ static int usbg_parse_config_strs(const char *path, const char *name, ret = USBG_ERROR_PATH_TOO_LONG; goto out; } - + /* Check if directory exist */ dir = opendir(spath); if (!dir) { @@ -773,18 +773,29 @@ static int usbg_parse_gadget_strs(const char *path, const char *name, int lang, } closedir(dir); - ret = usbg_read_string(spath, "", "manufacturer", g_strs->manufacturer); + + g_strs->manufacturer = g_strs->product = g_strs->serial = NULL; + + ret = usbg_read_string_alloc(spath, "", "manufacturer", + &g_strs->manufacturer); if (ret != USBG_SUCCESS) goto out; - ret = usbg_read_string(spath, "", "product", g_strs->product); + ret = usbg_read_string_alloc(spath, "", "product", &g_strs->product); if (ret != USBG_SUCCESS) - goto out; + goto free_mnf; - ret = usbg_read_string(spath, "", "serialnumber", g_strs->serial); + ret = usbg_read_string_alloc(spath, "", "serialnumber", + &g_strs->serial); if (ret != USBG_SUCCESS) - goto out; + goto free_product; + + return ret; +free_product: + free(g_strs->product); +free_mnf: + free(g_strs->manufacturer); out: return ret; } @@ -1691,15 +1702,18 @@ int usbg_set_gadget_strs(usbg_gadget *g, int lang, if (ret != USBG_SUCCESS) goto out; - ret = usbg_write_string(path, "", "manufacturer", g_strs->manufacturer); - if (ret != USBG_SUCCESS) - goto out; - - ret = usbg_write_string(path, "", "product", g_strs->product); - if (ret != USBG_SUCCESS) - goto out; +#define SET_GADGET_STR(file, field) \ + if (g_strs->field) { \ + ret = usbg_write_string(path, "", #file, \ + g_strs->field); \ + if (ret != USBG_SUCCESS) \ + goto out; \ + } - ret = usbg_write_string(path, "", "serialnumber", g_strs->serial); + SET_GADGET_STR(manufacturer, manufacturer); + SET_GADGET_STR(product, product); + SET_GADGET_STR(serialnumber, serial); +#undef SET_GADGET_STR out: return ret; } @@ -2024,7 +2038,7 @@ int usbg_set_config_string(usbg_config *c, int lang, const char *str) ret = usbg_write_string(path, "", "configuration", str); - + out: return ret; } @@ -2143,7 +2157,7 @@ int usbg_enable_gadget(usbg_gadget *g, usbg_udc *udc) g->udc->gadget = NULL; g->udc = udc; udc->gadget = g; - + out: return ret; } @@ -2246,4 +2260,3 @@ usbg_udc *usbg_get_next_udc(usbg_udc *u) { return u ? TAILQ_NEXT(u, unode) : NULL; } - diff --git a/src/usbg_schemes_libconfig.c b/src/usbg_schemes_libconfig.c index 53364df..dcf6aeb 100644 --- a/src/usbg_schemes_libconfig.c +++ b/src/usbg_schemes_libconfig.c @@ -1236,8 +1236,7 @@ static int usbg_import_gadget_strs_lang(config_setting_t *root, usbg_gadget *g) { config_setting_t *node; int lang; - const char *str; - struct usbg_gadget_strs g_strs = {{0}}; + struct usbg_gadget_strs g_strs = {0}; int ret = USBG_ERROR_INVALID_TYPE; node = config_setting_get_member(root, USBG_LANG_TAG); @@ -1258,9 +1257,7 @@ static int usbg_import_gadget_strs_lang(config_setting_t *root, usbg_gadget *g) if (node) { \ if (!usbg_config_is_string(node)) \ goto out; \ - str = config_setting_get_string(node); \ - strncpy(g_strs.FIELD, str, USBG_MAX_STR_LENGTH); \ - g_strs.FIELD[USBG_MAX_STR_LENGTH - 1] = '\0'; \ + g_strs.FIELD = (char *)config_setting_get_string(node); \ } \ } while (0) diff --git a/tests/test.c b/tests/test.c index 01beb24..1b86324 100644 --- a/tests/test.c +++ b/tests/test.c @@ -422,6 +422,16 @@ static int setup_long_udc_state(void **state) return 0; } +static void alloc_random_len_str(char **str) +{ + int len; + + len = rand() % USBG_MAX_FILE_SIZE; + *str = safe_malloc(len); + memset(*str, 'x', len - 1); + (*str)[len - 1] = '\0'; +} + /** * @brief Setup state with gadget strings of random length * @param[out] state Pointer to pointer to test_gadget_strs_data structure @@ -438,9 +448,9 @@ static int setup_random_len_gadget_strs_data(void **state) srand(time(NULL)); - memset(strs->serial, 'x', rand() % USBG_MAX_STR_LENGTH); - memset(strs->manufacturer, 'x', rand() % USBG_MAX_STR_LENGTH); - memset(strs->product, 'x', rand() % USBG_MAX_STR_LENGTH); + alloc_random_len_str(&strs->manufacturer); + alloc_random_len_str(&strs->product); + alloc_random_len_str(&strs->serial); data->strs = strs; |