From d42bc5e1786dd5131ca46ab7c0d3662d31f73b67 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Thu, 23 Aug 2012 23:52:45 +0300 Subject: [PATCH] Handle long configuration file lines more gracefully Skip the end of long configuration lines that get truncated in wpa_config_get_line(). Without this, the rest of the file was being parsed as another line and that resulted in an unexpected error message (wrong line number and wrong starting point). In addition, increase the read buffer for network blocks from 256 to 2000 since p2p_client_list parameter can have long values. These were supposed to truncated cleanly, but the unexpected processing of the end of the file (which is now fixed in this commit) could cause configuration file to be rejected. Signed-hostap: Jouni Malinen intended-for: hostap-1 --- wpa_supplicant/config_file.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/wpa_supplicant/config_file.c b/wpa_supplicant/config_file.c index 8eb9ba5b2..bd59a8073 100644 --- a/wpa_supplicant/config_file.c +++ b/wpa_supplicant/config_file.c @@ -19,6 +19,29 @@ #include "p2p/p2p.h" +static int newline_terminated(const char *buf, size_t buflen) +{ + size_t len = os_strlen(buf); + if (len == 0) + return 0; + if (len == buflen - 1 && buf[buflen - 1] != '\r' && + buf[len - 1] != '\n') + return 0; + return 1; +} + + +static void skip_line_end(FILE *stream) +{ + char buf[100]; + while (fgets(buf, sizeof(buf), stream)) { + buf[sizeof(buf) - 1] = '\0'; + if (newline_terminated(buf, sizeof(buf))) + return; + } +} + + /** * wpa_config_get_line - Read the next configuration file line * @s: Buffer for the line @@ -41,6 +64,15 @@ static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line, while (fgets(s, size, stream)) { (*line)++; s[size - 1] = '\0'; + if (!newline_terminated(s, size)) { + /* + * The line was truncated - skip rest of it to avoid + * confusing error messages. + */ + wpa_printf(MSG_INFO, "Long line in configuration file " + "truncated"); + skip_line_end(stream); + } pos = s; /* Skip white space from the beginning of line. */ @@ -117,7 +149,7 @@ static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id) { struct wpa_ssid *ssid; int errors = 0, end = 0; - char buf[256], *pos, *pos2; + char buf[2000], *pos, *pos2; wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block", *line);