1
0
Fork 0
mirror of https://github.com/linux-usb-gadgets/libusbgx.git synced 2025-07-25 22:55:06 +03:00

libusbgx: Get rid of static buffers from usbg_gadget_strs

Remove static buffers which limits strings length from usbg_gadget_strs.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
This commit is contained in:
Krzysztof Opasiak 2016-12-14 17:01:51 +01:00
parent 751a88f764
commit 78c45fa80f
4 changed files with 55 additions and 28 deletions

View file

@ -28,6 +28,7 @@
#include <limits.h> #include <limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> /* For FILE * */ #include <stdio.h> /* For FILE * */
#include <malloc.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -147,9 +148,9 @@ typedef enum {
*/ */
struct usbg_gadget_strs struct usbg_gadget_strs
{ {
char manufacturer[USBG_MAX_STR_LENGTH]; char *manufacturer;
char product[USBG_MAX_STR_LENGTH]; char *product;
char serial[USBG_MAX_STR_LENGTH]; 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) 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);
} }
/** /**

View file

@ -525,7 +525,7 @@ static int usbg_parse_config_strs(const char *path, const char *name,
ret = USBG_ERROR_PATH_TOO_LONG; ret = USBG_ERROR_PATH_TOO_LONG;
goto out; goto out;
} }
/* Check if directory exist */ /* Check if directory exist */
dir = opendir(spath); dir = opendir(spath);
if (!dir) { if (!dir) {
@ -773,18 +773,29 @@ static int usbg_parse_gadget_strs(const char *path, const char *name, int lang,
} }
closedir(dir); 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) if (ret != USBG_SUCCESS)
goto out; 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) 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) if (ret != USBG_SUCCESS)
goto out; goto free_product;
return ret;
free_product:
free(g_strs->product);
free_mnf:
free(g_strs->manufacturer);
out: out:
return ret; return ret;
} }
@ -1691,15 +1702,18 @@ int usbg_set_gadget_strs(usbg_gadget *g, int lang,
if (ret != USBG_SUCCESS) if (ret != USBG_SUCCESS)
goto out; goto out;
ret = usbg_write_string(path, "", "manufacturer", g_strs->manufacturer); #define SET_GADGET_STR(file, field) \
if (ret != USBG_SUCCESS) if (g_strs->field) { \
goto out; ret = usbg_write_string(path, "", #file, \
g_strs->field); \
if (ret != USBG_SUCCESS) \
goto out; \
}
ret = usbg_write_string(path, "", "product", g_strs->product); SET_GADGET_STR(manufacturer, manufacturer);
if (ret != USBG_SUCCESS) SET_GADGET_STR(product, product);
goto out; SET_GADGET_STR(serialnumber, serial);
#undef SET_GADGET_STR
ret = usbg_write_string(path, "", "serialnumber", g_strs->serial);
out: out:
return ret; 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); ret = usbg_write_string(path, "", "configuration", str);
out: out:
return ret; return ret;
} }
@ -2143,7 +2157,7 @@ int usbg_enable_gadget(usbg_gadget *g, usbg_udc *udc)
g->udc->gadget = NULL; g->udc->gadget = NULL;
g->udc = udc; g->udc = udc;
udc->gadget = g; udc->gadget = g;
out: out:
return ret; return ret;
} }
@ -2246,4 +2260,3 @@ usbg_udc *usbg_get_next_udc(usbg_udc *u)
{ {
return u ? TAILQ_NEXT(u, unode) : NULL; return u ? TAILQ_NEXT(u, unode) : NULL;
} }

View file

@ -1236,8 +1236,7 @@ static int usbg_import_gadget_strs_lang(config_setting_t *root, usbg_gadget *g)
{ {
config_setting_t *node; config_setting_t *node;
int lang; 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; int ret = USBG_ERROR_INVALID_TYPE;
node = config_setting_get_member(root, USBG_LANG_TAG); 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 (node) { \
if (!usbg_config_is_string(node)) \ if (!usbg_config_is_string(node)) \
goto out; \ goto out; \
str = config_setting_get_string(node); \ g_strs.FIELD = (char *)config_setting_get_string(node); \
strncpy(g_strs.FIELD, str, USBG_MAX_STR_LENGTH); \
g_strs.FIELD[USBG_MAX_STR_LENGTH - 1] = '\0'; \
} \ } \
} while (0) } while (0)

View file

@ -422,6 +422,16 @@ static int setup_long_udc_state(void **state)
return 0; 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 * @brief Setup state with gadget strings of random length
* @param[out] state Pointer to pointer to test_gadget_strs_data structure * @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)); srand(time(NULL));
memset(strs->serial, 'x', rand() % USBG_MAX_STR_LENGTH); alloc_random_len_str(&strs->manufacturer);
memset(strs->manufacturer, 'x', rand() % USBG_MAX_STR_LENGTH); alloc_random_len_str(&strs->product);
memset(strs->product, 'x', rand() % USBG_MAX_STR_LENGTH); alloc_random_len_str(&strs->serial);
data->strs = strs; data->strs = strs;