From: Ben Pfaff Date: Thu, 9 Oct 2008 14:37:43 +0000 (-0700) Subject: Make str_copy_rpad() behave properly with a DST_SIZE of 0. X-Git-Tag: v0.7.1~50^2~36 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5165d8c7ed098d87a481f08652f3e1aa6d82c366;p=pspp-builds.git Make str_copy_rpad() behave properly with a DST_SIZE of 0. --- diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 9a7c6da8..d0826720 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -194,15 +194,18 @@ buf_copy_rpad (char *dst, size_t dst_size, void str_copy_rpad (char *dst, size_t dst_size, const char *src) { - size_t src_len = strlen (src); - if (src_len < dst_size - 1) + if (dst_size > 0) { - memcpy (dst, src, src_len); - memset (&dst[src_len], ' ', dst_size - 1 - src_len); + size_t src_len = strlen (src); + if (src_len < dst_size - 1) + { + memcpy (dst, src, src_len); + memset (&dst[src_len], ' ', dst_size - 1 - src_len); + } + else + memcpy (dst, src, dst_size - 1); + dst[dst_size - 1] = 0; } - else - memcpy (dst, src, dst_size - 1); - dst[dst_size - 1] = 0; } /* Copies SRC to DST, which is in a buffer DST_SIZE bytes long.