aboutsummaryrefslogtreecommitdiffstats
path: root/small-codes
diff options
context:
space:
mode:
authorvg <vgm+dev@devys.org>2024-02-23 15:07:10 +0100
committervg <vgm+dev@devys.org>2024-02-23 15:10:29 +0100
commitd7d9e9062e125f725848017b6657b7eff0c266b1 (patch)
tree25618eba2fcfe95a81287ff1dc14a35ad6beb0da /small-codes
parent3bff4cdafe01df66496f649a02ba2a3a01573e29 (diff)
downloadscripts-d7d9e9062e125f725848017b6657b7eff0c266b1.tar.gz
scripts-d7d9e9062e125f725848017b6657b7eff0c266b1.tar.bz2
scripts-d7d9e9062e125f725848017b6657b7eff0c266b1.zip
fix message output
- avoid buffer overflow if received message is taking up to maximum buffer size, then latest char assign would have been set after the table length. - avoid using printf for quicker simple string output, write can be used since length is already known. - removed debug prefix from the output.
Diffstat (limited to 'small-codes')
-rw-r--r--small-codes/syslog-listen.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/small-codes/syslog-listen.c b/small-codes/syslog-listen.c
index c878076..e248608 100644
--- a/small-codes/syslog-listen.c
+++ b/small-codes/syslog-listen.c
@@ -34,13 +34,13 @@ int main(int argc, const char** argv)
int ret = bind(sock, (struct sockaddr*)&sock_file, sizeof(sock_file));
PERROR(ret, "bind");
- char buf[8192];
- int receive;
+ char buf[8192 + 1];
+ int received;
while (1) {
- receive = recv(sock, buf, sizeof(buf), 0);
- PERROR(receive, "recv");
- buf[receive] = '\0';
- printf("received: %s\n", buf);
+ received = recv(sock, buf, sizeof(buf)-1, 0);
+ PERROR(received, "recv");
+ buf[received] = '\n';
+ write(1, buf, received+1);
}
return 0;