From d794c15da1e16cd404a0fb3c0e2bf26fdbfa799b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Oct 2012 01:50:07 +0300 Subject: [PATCH] Make get_device_properties robust against long property values --- main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index ee55972..62d6a6c 100644 --- a/main.c +++ b/main.c @@ -68,7 +68,8 @@ Property *get_device_properties(char *node) } else if(pid>0) { - char buf[256]; + char *buf; + int bufsize; int pos = 0; int eof = 0; Property *props = NULL; @@ -76,6 +77,9 @@ Property *get_device_properties(char *node) close(pipe_fd[1]); + bufsize = 256; + buf = (char *)malloc(bufsize); + while(1) { int newline; @@ -86,7 +90,7 @@ Property *get_device_properties(char *node) { int len; - len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos); + len = read(pipe_fd[0], buf+pos, bufsize-pos); if(len==0) eof = 1; else if(len==-1) @@ -100,7 +104,13 @@ Property *get_device_properties(char *node) newline = i; if(newline<0) - break; + { + if(eof) + break; + bufsize *= 2; + buf = (char *)realloc(buf, bufsize); + continue; + } if(parse_property(buf, newline, &prop)==0) { @@ -115,6 +125,8 @@ Property *get_device_properties(char *node) break; } + free(buf); + if(props) { props[n_props].name = NULL; -- 2.43.0