From d6039235778a7dacacf1abf2e1d6e34c8dfbf065 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 8 Dec 2010 15:19:36 +0000 Subject: Fix problems parsing multiple line input. --- .gitignore | 2 ++ tools/notify-dns-slaves.c | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index ecd03bf..d9f9c32 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ Makefile Makefile.in run-tests* *.tar.gz +/.cproject +/.project /aclocal.m4 /autom4te.cache diff --git a/tools/notify-dns-slaves.c b/tools/notify-dns-slaves.c index e2c3325..65e5a5a 100644 --- a/tools/notify-dns-slaves.c +++ b/tools/notify-dns-slaves.c @@ -725,41 +725,48 @@ static void stdin_callback (int fd, int type, void *arg) { char *line; - int num; + int num, rc; assert (fd == 0); - num = read (fd, stdin_buffer + stdin_offset, LINE_LENGTH - stdin_offset); - if (num < 0) { + rc = read (fd, stdin_buffer + stdin_offset, LINE_LENGTH - stdin_offset); + if (rc < 0) { if (errno == EAGAIN || errno == EINTR) return; warningx ("couldn't read from stdin"); input_complete = 1; - } else if (num == 0) { + } else if (rc == 0) { debug ("input closed"); input_complete = 1; } else { - stdin_offset += num; + stdin_offset += rc; + assert (stdin_offset <= LINE_LENGTH); } do { - line = strchr (stdin_buffer, '\n'); + line = memchr (stdin_buffer, '\n', stdin_offset); + + if (line) { + num = (line - stdin_buffer) + 1; - if (!line && input_complete) + } else if (!line && input_complete) { line = stdin_buffer + stdin_offset; + num = line - stdin_buffer; - if (!line && stdin_offset >= LINE_LENGTH) { + } else if (!line && stdin_offset >= LINE_LENGTH) { warningx ("input line too long"); line = stdin_buffer + LINE_LENGTH; - } + num = LINE_LENGTH; - if (!line) /* Wait for more data */ + } else { + /* Wait for more data */ break; + } *line = 0; - num = (line + 1) - stdin_buffer; process_stdin_line (stdin_buffer); + assert (stdin_offset >= num); stdin_offset = stdin_offset - num; if (stdin_offset) memmove (stdin_buffer, stdin_buffer + num, stdin_offset); -- cgit v1.2.3