Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 578d533

Browse files
authored
Merge pull request #295 from gao-feng/dns
use memcpy to replace snprintf
2 parents 6376e77 + dceff04 commit 578d533

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/net.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -689,32 +689,29 @@ int hyper_write_dns_file(int fd, char *field, char **data, int num)
689689
return 0;
690690

691691
size = strlen(field);
692-
buf = malloc(size);
692+
// 1 for last '\n'
693+
buf = malloc(size + 1);
693694
if (buf == NULL) {
694695
fprintf(stderr, "fail to malloc buff for %s\n", field);
695696
goto out;
696697
}
697698
memcpy(buf, field, size);
698699
for (i = 0; i < num; i++) {
699-
int new_len = strlen(data[i]) + 1 + 1;
700-
char *format = " %s";
701-
if (i + 1 == num) {
702-
new_len += 1;
703-
format = " %s\n";
704-
}
705-
buf = realloc(buf, size + new_len);
700+
// 1 for space
701+
int new_size = size + strlen(data[i]) + 1;
702+
buf = realloc(buf, new_size);
706703
if (buf == NULL) {
707704
fprintf(stderr, "fail to realloc buff for %s\n", field);
708705
goto out;
709706
}
710-
if (snprintf(buf + size, new_len, format, data[i]) < 0) {
711-
fprintf(stderr, "sprintf search entry failed\n");
712-
goto out;
713-
}
707+
buf[size]= ' ';
708+
memcpy(buf + size + 1, data[i], strlen(data[i]));
714709
fprintf(stdout, "%s: data: %s\n", field, buf);
715-
size += new_len;
710+
size = new_size;
716711
}
717712

713+
buf[size] = '\n';
714+
size += 1;
718715
while (len < size) {
719716
i = write(fd, buf + len, size - len);
720717
if (i < 0) {

0 commit comments

Comments
 (0)