From 899f529d0c5f83abea42759719abf1130f4b4c5b Mon Sep 17 00:00:00 2001 From: Herman Semenov Date: Wed, 9 Apr 2025 00:49:42 +0300 Subject: [PATCH] Optimize putname function using out-of-bound while loop strlen() My benchmarks here: https://github.com/yarrick/iodine/issues/110 --- src/read.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/read.c b/src/read.c index b33a376..d9bbc30 100644 --- a/src/read.c +++ b/src/read.c @@ -158,7 +158,7 @@ int putname(char **buf, size_t buflen, const char *host) { char *word; - int left; + size_t left; char *h; char *p; @@ -167,18 +167,21 @@ putname(char **buf, size_t buflen, const char *host) p = *buf; word = strtok(h, "."); + size_t len_word = strlen(word); while(word) { - if (strlen(word) > 63 || strlen(word) > left) { + if (len_word > 63 || len_word > left) { free(h); return -1; } - left -= (strlen(word) + 1); - *p++ = (char)strlen(word); - memcpy(p, word, strlen(word)); - p += strlen(word); + len_word = strlen(word); + left -= len_word + 1; + *p++ = (char)len_word; + memcpy(p, word, len_word); + p += len_word; word = strtok(NULL, "."); + len_word = strlen(word); } *p++ = 0;