summaryrefslogtreecommitdiff
path: root/cconv.c
diff options
context:
space:
mode:
authorBart Van Assche <bvanassche@acm.org>2019-08-14 13:10:08 -0700
committerJens Axboe <axboe@kernel.dk>2019-08-14 15:01:29 -0600
commit36833fb04b5f9a734e96a571dfb52fc54b5b95e7 (patch)
tree6600f0f62d861bac57b8f6b5d101a18d43610a66 /cconv.c
parent27bbef0cb264b7d66a5f93424d6fc6ff973e22a2 (diff)
Optimize the code that copies strings
Using strncpy() to copy strings is suboptimal because strncpy writes a bunch of additional unnecessary null bytes. Use snprintf() instead of strncpy(). An additional advantage of snprintf() is that it guarantees that the output string is '\0'-terminated. This patch is an improvement for commit 32e31c8c5f7b ("Fix string copy compilation warnings"). Cc: Damien Le Moal <damien.lemoal@wdc.com> Signed-off-by: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'cconv.c')
-rw-r--r--cconv.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/cconv.c b/cconv.c
index 50e45c6..0e65724 100644
--- a/cconv.c
+++ b/cconv.c
@@ -13,10 +13,9 @@ static void string_to_cpu(char **dst, const uint8_t *src)
static void __string_to_net(uint8_t *dst, const char *src, size_t dst_size)
{
- if (src) {
- dst[dst_size - 1] = '\0';
- strncpy((char *) dst, src, dst_size - 1);
- } else
+ if (src)
+ snprintf((char *) dst, dst_size, "%s", src);
+ else
dst[0] = '\0';
}