mirror of
https://github.com/yarrick/iodine.git
synced 2025-06-05 02:03:43 +02:00
Optimize putname function using out-of-bound while loop strlen()
My benchmarks here: https://github.com/yarrick/iodine/issues/110
This commit is contained in:
parent
2b65972693
commit
899f529d0c
1 changed files with 9 additions and 6 deletions
15
src/read.c
15
src/read.c
|
@ -158,7 +158,7 @@ int
|
||||||
putname(char **buf, size_t buflen, const char *host)
|
putname(char **buf, size_t buflen, const char *host)
|
||||||
{
|
{
|
||||||
char *word;
|
char *word;
|
||||||
int left;
|
size_t left;
|
||||||
char *h;
|
char *h;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
|
@ -167,18 +167,21 @@ putname(char **buf, size_t buflen, const char *host)
|
||||||
p = *buf;
|
p = *buf;
|
||||||
|
|
||||||
word = strtok(h, ".");
|
word = strtok(h, ".");
|
||||||
|
size_t len_word = strlen(word);
|
||||||
while(word) {
|
while(word) {
|
||||||
if (strlen(word) > 63 || strlen(word) > left) {
|
if (len_word > 63 || len_word > left) {
|
||||||
free(h);
|
free(h);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
left -= (strlen(word) + 1);
|
len_word = strlen(word);
|
||||||
*p++ = (char)strlen(word);
|
left -= len_word + 1;
|
||||||
memcpy(p, word, strlen(word));
|
*p++ = (char)len_word;
|
||||||
p += strlen(word);
|
memcpy(p, word, len_word);
|
||||||
|
p += len_word;
|
||||||
|
|
||||||
word = strtok(NULL, ".");
|
word = strtok(NULL, ".");
|
||||||
|
len_word = strlen(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
*p++ = 0;
|
*p++ = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue