Trap fgets() failure in *_scan()

Ensure we don't read arbitrary data when fgets() returns a NULL string
(meaning that either the EOF is encountered or an error occurred). This
also fixes a couple of compiler warnings seen with "-Wunused-result".

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-02-17 16:49:35 +01:00
parent 47ceb96e13
commit 894ac2d70d
3 changed files with 12 additions and 4 deletions

View File

@ -443,7 +443,9 @@ apoint_scan (FILE *f, struct tm start, struct tm end, char state, char *note)
localtime (&t); localtime (&t);
/* Read the appointment description */ /* Read the appointment description */
fgets (buf, sizeof buf, f); if (!fgets (buf, sizeof buf, f))
return NULL;
newline = strchr (buf, '\n'); newline = strchr (buf, '\n');
if (newline) if (newline)
*newline = '\0'; *newline = '\0';

View File

@ -149,7 +149,9 @@ event_scan (FILE *f, struct tm start, int id, char *note)
localtime (&t); localtime (&t);
/* Read the event description */ /* Read the event description */
fgets (buf, sizeof buf, f); if (!fgets (buf, sizeof buf, f))
return NULL;
nl = strchr (buf, '\n'); nl = strchr (buf, '\n');
if (nl) if (nl)
{ {

View File

@ -374,7 +374,9 @@ recur_apoint_scan (FILE *f, struct tm start, struct tm end, char type,
time_t tstart, tend, tuntil; time_t tstart, tend, tuntil;
/* Read the appointment description */ /* Read the appointment description */
fgets (buf, sizeof buf, f); if (!fgets (buf, sizeof buf, f))
return NULL;
nl = strchr (buf, '\n'); nl = strchr (buf, '\n');
if (nl) if (nl)
{ {
@ -419,7 +421,9 @@ recur_event_scan (FILE *f, struct tm start, int id, char type, int freq,
time_t tstart, tuntil; time_t tstart, tuntil;
/* Read the event description */ /* Read the event description */
fgets (buf, sizeof buf, f); if (!fgets (buf, sizeof buf, f))
return NULL;
nl = strchr (buf, '\n'); nl = strchr (buf, '\n');
if (nl) if (nl)
{ {