Skip to contentSkip to conversation

Remark: src/tools/show.c

DISCUSSION Markus Grundmann 2 participants 2 messages Aug 31, 2019

lore
Share & tools
lore
#0 lore raw

I think the explicit termination (buf[] = '\0') is not needed. Personaly I would prefer memset() to clean up the buffer. strncopy() reduced by two instead of a single chars protects the last string terminator. But we will not die when it not was changed

[src/tools/show.c]
==> endpoint() <==
122,123c122
- strncpy(buf, gai_strerror(ret), sizeof(buf) - 1);
- buf[sizeof(buf) - 1] = '\0';
---
+ strncpy(buf, gai_strerror(ret), sizeof(buf) - 2);
162a162
+ memset(buf, 0, sizeof(buf));
164c164
- strncpy(buf, "Now", sizeof(buf) - 1);
---
+ strncpy(buf, "Now", sizeof(buf) - 2);

==> ago() <==
166c166
- strncpy(buf, "(" TERMINAL_FG_RED "System clock wound backward;
connection problems may ensue." TERMINAL_RESET ")", sizeof(buf) - 1);
---
+ strncpy(buf, "(" TERMINAL_FG_RED "System clock wound backward;
connection problems may ensue." TERMINAL_RESET ")", sizeof(buf) - 2); 169c169
- strncpy(buf + offset, " ago", sizeof(buf) - offset - 1);
---
- strncpy(buf + offset, " ago", sizeof(buf) - offset - 2);
171d170
- buf[sizeof(buf) - 1] = '\0';

Signature · 13 lines
Best regards,
Markus

Better Privacy with PGP encrypted Mail: http://activezone.de/pgp/
Fingerprint: 58C5 8BAF 6FCE B24F 1881 B5B8 F2A8 E1D0 484B 0054
Threema ID: 7ZRET2JY

„Wer die Freiheit aufgibt um Sicherheit zu gewinnen,
 der wird am Ende beides verlieren.“ -- Benjamin Franklin
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
#1 lore raw

Forcing later callers to assume state about a buffer is not better. Re: strncpy, the explicit termination *is* required, since the destination will not be null terminated if the source is the same size. Also, doesn't this patch remove the "ago" response? ~Derrick

Quoted 32 lines · Markus Grundmann

I think the explicit termination (buf[] = '\0') is not needed. Personaly I would prefer memset() to clean up the buffer. strncopy() reduced by two instead of a single chars protects the last string terminator. But we will not die when it not was changed

[src/tools/show.c]
==> endpoint() <==
122,123c122

(quoted diff) +1 -3
- 		strncpy(buf, gai_strerror(ret), sizeof(buf) - 1);
- 		buf[sizeof(buf) - 1] = '\0';
---
+ 		strncpy(buf, gai_strerror(ret), sizeof(buf) - 2);

162a162

(quoted diff) +1 -0
+ 	memset(buf, 0, sizeof(buf));

164c164

(quoted diff) +1 -2
- 		strncpy(buf, "Now", sizeof(buf) - 1);
---
+ 		strncpy(buf, "Now", sizeof(buf) - 2);

==> ago() <==
166c166

(quoted diff) +0 -1
- 		strncpy(buf, "(" TERMINAL_FG_RED "System clock wound backward;

connection problems may ensue." TERMINAL_RESET ")", sizeof(buf) - 1);
---

(quoted diff) +1 -0
+ 		strncpy(buf, "(" TERMINAL_FG_RED "System clock wound backward;

connection problems may ensue." TERMINAL_RESET ")", sizeof(buf) - 2); 169c169

(quoted diff) +0 -3
- 		strncpy(buf + offset, " ago", sizeof(buf) - offset - 1);
---
- 		strncpy(buf + offset, " ago", sizeof(buf) - offset - 2);

171d170

Review

Quoted 1 line
(quoted diff) +0 -1
- 	buf[sizeof(buf) - 1] = '\0';

_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard