aboutsummaryrefslogtreecommitdiffstats
path: root/arch/tile
diff options
context:
space:
mode:
authorChris Metcalf <cmetcalf@tilera.com>2014-09-02 16:25:22 -0400
committerChris Metcalf <cmetcalf@tilera.com>2014-10-02 10:19:34 -0400
commitbceb7efa6a7e656bfaa67b6f54925e7db75bcd52 (patch)
treeb43b312e9f61c5e9b5fc0ad91fc522fd372f10b0 /arch/tile
parent514b82a52b7aaa413ab8f0e7db93a5ccd0540d09 (diff)
tile gxio: use better string copy primitive
Both strncpy and strlcpy suffer from the fact that they do partial copies of strings into the destination when the target buffer is too small. This is frequently pointless since an overflow of the target buffer may make the result invalid. strncpy() makes it relatively hard to even detect the error condition, and with strlcpy() you have to duplicate the buffer size parameter to test to see if the result exceeds it. By returning zero in the failure case, we both make testing for it easy, and by simply not copying anything in that case, we make it mandatory for callers to test the error code. To catch lazy programmers who don't check, we also place a NUL at the start of the destination buffer (if there is space) to ensure that the result is an invalid string. At some point it may make sense to promote strscpy() to a global platform-independent function, but other than the reviewers, no one was interested on LKML, so for now leave the strscpy() function as file-static. Reviewed-by: Randy Dunlap <rdunlap@infradead.org> Reviewed-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se> Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
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)