Update with max string lengths

This commit is contained in:
Tony Kuker
2024-06-01 01:56:31 +00:00
parent 56b8a5779e
commit a46957c573
5 changed files with 14 additions and 12 deletions

View File

@@ -809,13 +809,13 @@ static const char *bcm2712_gpio_get_name(void *priv, unsigned gpio)
if (inst->flags & FLAGS_AON) if (inst->flags & FLAGS_AON)
{ {
if (bank == 1) if (bank == 1)
sprintf(name_buf, "AON_SGPIO%d", gpio_offset); snprintf(name_buf, sizeof(name_buf), "AON_SGPIO%d", gpio_offset);
else else
sprintf(name_buf, "AON_GPIO%d", gpio_offset); snprintf(name_buf, sizeof(name_buf), "AON_GPIO%d", gpio_offset);
} }
else else
{ {
sprintf(name_buf, "GPIO%d", gpio); snprintf(name_buf, sizeof(name_buf), "GPIO%d", gpio);
} }
return name_buf; return name_buf;
} }

View File

@@ -287,7 +287,7 @@ static const char *bcm2835_gpio_get_name(void *priv, unsigned gpio)
{ {
static char name_buf[16]; static char name_buf[16];
UNUSED(priv); UNUSED(priv);
sprintf(name_buf, "GPIO%d", gpio); snprintf(name_buf, sizeof(name_buf), "GPIO%d", gpio);
return name_buf; return name_buf;
} }

View File

@@ -419,7 +419,7 @@ static const char *rp1_gpio_get_name(void *priv, unsigned gpio)
if (gpio >= RP1_NUM_GPIOS) if (gpio >= RP1_NUM_GPIOS)
return NULL; return NULL;
sprintf(name_buf, "GPIO%d", gpio); snprintf(name_buf, sizeof(name_buf), "GPIO%d", gpio);
return name_buf; return name_buf;
} }

View File

@@ -315,7 +315,9 @@ unsigned gpio_get_gpio_by_name(const char *name, int name_len)
unsigned gpio; unsigned gpio;
if (!name_len) if (!name_len)
name_len = strlen(name); // Note: max length of name is limited by the maximum argument
// length. However, FILENAME_MAX is a more reasonable limit.
name_len = strnlen(name, FILENAME_MAX);
for (gpio = 0; gpio < num_gpios; gpio++) for (gpio = 0; gpio < num_gpios; gpio++)
{ {
const char *gpio_name = gpio_names[gpio]; const char *gpio_name = gpio_names[gpio];
@@ -443,8 +445,8 @@ int gpiolib_init(void)
for (i = 0; ; i++) for (i = 0; ; i++)
{ {
sprintf(pathbuf, "gpio%d", i); snprintf(pathbuf, sizeof(pathbuf), "gpio%d", i);
sprintf(gpiomem_idx, "%d", i); snprintf(gpiomem_idx, sizeof(gpiomem_idx), "%d", i);
alias = dt_read_prop("/aliases", pathbuf, NULL); alias = dt_read_prop("/aliases", pathbuf, NULL);
if (!alias && i == 0) if (!alias && i == 0)
{ {
@@ -457,7 +459,7 @@ int gpiolib_init(void)
compatible = dt_read_prop(alias, "compatible", NULL); compatible = dt_read_prop(alias, "compatible", NULL);
if (!compatible) if (!compatible)
{ {
sprintf(pathbuf, "%s/..", alias); snprintf(pathbuf, sizeof(pathbuf), "%s/..", alias);
compatible = dt_read_prop(pathbuf, "compatible", NULL); compatible = dt_read_prop(pathbuf, "compatible", NULL);
} }
@@ -485,7 +487,7 @@ int gpiolib_init(void)
return -1; return -1;
} }
sprintf(pathbuf, "/dev/gpiomem%s", gpiomem_idx); snprintf(pathbuf, sizeof(pathbuf), "/dev/gpiomem%s", gpiomem_idx);
inst->mem_fd = open(pathbuf, O_RDWR|O_SYNC); inst->mem_fd = open(pathbuf, O_RDWR|O_SYNC);
} }
@@ -530,7 +532,7 @@ int gpiolib_init(void)
if (p && p < end) if (p && p < end)
{ {
name = p; name = p;
p += strlen(p) + 1; p += strnlen(p, FILENAME_MAX) + 1;
if (sscanf(name, "PIN%u", &pin) != 1 || if (sscanf(name, "PIN%u", &pin) != 1 ||
pin < 1 || pin > NUM_HDR_PINS) pin < 1 || pin > NUM_HDR_PINS)
{ {

View File

@@ -173,7 +173,7 @@ uint64_t dt_parse_addr(const char *node)
while (1) while (1)
{ {
char *tmp, *p; char *tmp, *p;
strcpy(parent, node); strncpy(parent, node, FILENAME_MAX);
p = strrchr(parent, '/'); p = strrchr(parent, '/');
if (!p) if (!p)
return INVALID_ADDRESS; return INVALID_ADDRESS;