From d7d9e9062e125f725848017b6657b7eff0c266b1 Mon Sep 17 00:00:00 2001 From: vg Date: Fri, 23 Feb 2024 15:07:10 +0100 Subject: 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. --- small-codes/syslog-listen.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'small-codes') 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; -- cgit v1.2.3