From 1ced507bf4ba384f91be4f28b3967f7a5a305952 Mon Sep 17 00:00:00 2001 From: Stefan Schmidt Date: Sat, 26 May 2012 22:50:06 +0000 Subject: [PATCH] tempget: Check return values from fgets() Without this checking we might operate on wrong data read in. Better check. SVN revision: 71445 --- src/modules/temperature/tempget.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/modules/temperature/tempget.c b/src/modules/temperature/tempget.c index 28d3be240..f1746f2c1 100644 --- a/src/modules/temperature/tempget.c +++ b/src/modules/temperature/tempget.c @@ -401,7 +401,8 @@ check(void) { char dummy[4096]; - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) goto error; + buf[sizeof(buf) - 1] = 0; if (sscanf(buf, "%s %s %i", dummy, dummy, &temp) == 3) ret = 1; @@ -417,7 +418,8 @@ check(void) f = fopen(sensor_path, "rb"); if (f) { - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) goto error; + fclose(f); buf[sizeof(buf) - 1] = 0; if (sscanf(buf, "%i", &temp) == 1) @@ -434,7 +436,8 @@ check(void) f = fopen(sensor_path, "r"); if (f) { - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) goto error; + buf[sizeof(buf) - 1] = 0; /* actually read the temp */ @@ -453,7 +456,8 @@ check(void) f = fopen(sensor_path, "r"); if (f) { - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) goto error; + buf[sizeof(buf) - 1] = 0; /* actually read the temp */ @@ -474,7 +478,8 @@ check(void) { char *p, *q; - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) goto error; + buf[sizeof(buf) - 1] = 0; fclose(f); p = strchr(buf, ':'); @@ -497,7 +502,8 @@ check(void) f = fopen(sensor_path, "r"); if (f) { - fgets(buf, sizeof(buf), f); + if (fgets(buf, sizeof(buf), f) == NULL) goto error; + buf[sizeof(buf) - 1] = 0; fclose(f); temp = atoi(buf);