From 75f0cfee9e5d1eb6c2f76d1898976391c1ab5031 Mon Sep 17 00:00:00 2001 From: Stephen Cprek Date: Wed, 22 Nov 2017 15:22:51 -0600 Subject: Fix strncpy from padding past the actual max size In the padding portion of strncpy an array was used on a pointer that was incremented N times. Resulting in padding N bytes past the max Change-Id: Icb075a234f456f9763c9712e1d6c78c947efdce1 Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/50001 Reviewed-by: Martin Gloff Tested-by: Jenkins Server Tested-by: Jenkins OP Build CI Tested-by: Jenkins OP HW Tested-by: FSP CI Jenkins Reviewed-by: Michael Baiocchi Reviewed-by: Nicholas E. Bofferding Reviewed-by: Daniel M. Crowell --- src/lib/string_utils.C | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'src/lib') diff --git a/src/lib/string_utils.C b/src/lib/string_utils.C index 27d9228fb..6e7bcd804 100644 --- a/src/lib/string_utils.C +++ b/src/lib/string_utils.C @@ -159,20 +159,17 @@ extern "C" int strncmp(const char* a, const char* b, size_t l) extern "C" char* strncpy(char* d, const char* s, size_t l) { char* d1 = d; - size_t len = 0; - - do + while ( (l > 0) && (*s != '\0') ) { - if (len++ >= l) break; - *d1 = *s; - if (*s == '\0') break; - d1++; s++; - } while(1); + *d1++ = *s++; + --l; + } // pad the remainder - while( len < l ) + while (l > 0) { - d1[len++] = '\0'; + *d1++ = '\0'; + --l; } return d; -- cgit v1.2.1