aboutsummaryrefslogtreecommitdiffstats
path: root/arch/tile
diff options
context:
space:
mode:
Diffstat (limited to 'arch/tile')
-rw-r--r--arch/tile/gxio/mpipe.c37
1 files changed, 32 insertions, 5 deletions
diff --git a/arch/tile/gxio/mpipe.c b/arch/tile/gxio/mpipe.c
index 5301a9ffbae1..320ff5e6e61e 100644
--- a/arch/tile/gxio/mpipe.c
+++ b/arch/tile/gxio/mpipe.c
@@ -29,6 +29,32 @@
29/* HACK: Avoid pointless "shadow" warnings. */ 29/* HACK: Avoid pointless "shadow" warnings. */
30#define link link_shadow 30#define link link_shadow
31 31
32/**
33 * strscpy - Copy a C-string into a sized buffer, but only if it fits
34 * @dest: Where to copy the string to
35 * @src: Where to copy the string from
36 * @size: size of destination buffer
37 *
38 * Use this routine to avoid copying too-long strings.
39 * The routine returns the total number of bytes copied
40 * (including the trailing NUL) or zero if the buffer wasn't
41 * big enough. To ensure that programmers pay attention
42 * to the return code, the destination has a single NUL
43 * written at the front (if size is non-zero) when the
44 * buffer is not big enough.
45 */
46static size_t strscpy(char *dest, const char *src, size_t size)
47{
48 size_t len = strnlen(src, size) + 1;
49 if (len > size) {
50 if (size)
51 dest[0] = '\0';
52 return 0;
53 }
54 memcpy(dest, src, len);
55 return len;
56}
57
32int gxio_mpipe_init(gxio_mpipe_context_t *context, unsigned int mpipe_index) 58int gxio_mpipe_init(gxio_mpipe_context_t *context, unsigned int mpipe_index)
33{ 59{
34 char file[32]; 60 char file[32];
@@ -511,8 +537,8 @@ int gxio_mpipe_link_instance(const char *link_name)
511 if (!context) 537 if (!context)
512 return GXIO_ERR_NO_DEVICE; 538 return GXIO_ERR_NO_DEVICE;
513 539
514 strncpy(name.name, link_name, sizeof(name.name)); 540 if (strscpy(name.name, link_name, sizeof(name.name)) == 0)
515 name.name[GXIO_MPIPE_LINK_NAME_LEN - 1] = '\0'; 541 return GXIO_ERR_NO_DEVICE;
516 542
517 return gxio_mpipe_info_instance_aux(context, name); 543 return gxio_mpipe_info_instance_aux(context, name);
518} 544}
@@ -529,7 +555,8 @@ int gxio_mpipe_link_enumerate_mac(int idx, char *link_name, uint8_t *link_mac)
529 555
530 rv = gxio_mpipe_info_enumerate_aux(context, idx, &name, &mac); 556 rv = gxio_mpipe_info_enumerate_aux(context, idx, &name, &mac);
531 if (rv >= 0) { 557 if (rv >= 0) {
532 strncpy(link_name, name.name, sizeof(name.name)); 558 if (strscpy(link_name, name.name, sizeof(name.name)) == 0)
559 return GXIO_ERR_INVAL_MEMORY_SIZE;
533 memcpy(link_mac, mac.mac, sizeof(mac.mac)); 560 memcpy(link_mac, mac.mac, sizeof(mac.mac));
534 } 561 }
535 562
@@ -545,8 +572,8 @@ int gxio_mpipe_link_open(gxio_mpipe_link_t *link,
545 _gxio_mpipe_link_name_t name; 572 _gxio_mpipe_link_name_t name;
546 int rv; 573 int rv;
547 574
548 strncpy(name.name, link_name, sizeof(name.name)); 575 if (strscpy(name.name, link_name, sizeof(name.name)) == 0)
549 name.name[GXIO_MPIPE_LINK_NAME_LEN - 1] = '\0'; 576 return GXIO_ERR_NO_DEVICE;
550 577
551 rv = gxio_mpipe_link_open_aux(context, name, flags); 578 rv = gxio_mpipe_link_open_aux(context, name, flags);
552 if (rv < 0) 579 if (rv < 0)