Permalink
Browse files

Clean up some compile warnings (#330)

* Change releaseprep patch version string to "" for stable release. Fixes #303
* Check return value for fgets calls
* Check return value for write calls
* Check return value for freopen calls
* Check return value of ftruncate calls
* Revisit fgets occurrences.
* Properly check freopen.
* Check for write errors as well.
* Fix ftruncate errorchecking
* Properly output freopen errors.
* Add missing fclose and correct printf.
* Use version of write() that doesn't ignore its return value.
* Have language file reading errors output to MISC.
* Change write to safe_write.
* Replace LOG_DEBUG with LOG_MISC for errors.
  • Loading branch information...
1 parent d6165e2 commit 9bd9d1bd34bcdcf0f69ef59f549bc4cec8c22371 @vanosg vanosg committed on GitHub Feb 13, 2017
Showing with 350 additions and 309 deletions.
  1. +13 −11 src/botcmd.c
  2. +6 −6 src/dcc.c
  3. +5 −2 src/language.c
  4. +22 −12 src/main.c
  5. +75 −62 src/misc.c
  6. +59 −64 src/mod/filesys.mod/dbcompat.c
  7. +6 −2 src/mod/filesys.mod/filedb3.c
  8. +136 −135 src/mod/notes.mod/notes.c
  9. +15 −3 src/net.c
  10. +2 −2 src/patch.h
  11. +1 −0 src/proto.h
  12. +10 −10 src/users.c
View
@@ -1048,17 +1048,19 @@ static void bot_motd(int idx, char *par)
if (vv != NULL) {
botnet_send_priv(idx, botnetnick, who, NULL, "--- %s\n", MISC_MOTDFILE);
help_subst(NULL, NULL, 0, irc, NULL);
- while (!feof(vv)) {
- fgets(s, 120, vv);
- if (!feof(vv)) {
- if (s[strlen(s) - 1] == '\n')
- s[strlen(s) - 1] = 0;
- if (!s[0])
- strcpy(s, " ");
- help_subst(s, who, &fr, HELP_DCC, dcc[idx].nick);
- if (s[0])
- botnet_send_priv(idx, botnetnick, who, NULL, "%s", s);
- }
+ /* don't check for feof after fgets, skips last line if it has no \n (ie on windows) */
+ while (!feof(vv) && fgets(s, sizeof TBUF, vv) != NULL) {
+ if (s[strlen(s) - 1] == '\n')
+ s[strlen(s) - 1] = 0;
+ if (!s[0])
+ strcpy(s, " ");
+ help_subst(s, who, &fr, HELP_DCC, dcc[idx].nick);
+ if (s[0])
+ botnet_send_priv(idx, botnetnick, who, NULL, "%s", s);
+ }
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(vv)) {
+ putlog(LOG_MISC, "*", "Error reading MOTD file");
}
fclose(vv);
} else
View
@@ -128,20 +128,20 @@ static void strip_telnet(int sock, char *buf, int *len)
/* WILL X -> response: DONT X */
/* except WILL ECHO which we just smile and ignore */
if (*(p + 1) != TLN_ECHO) {
- write(sock, TLN_IAC_C TLN_DONT_C, 2);
- write(sock, p + 1, 1);
+ safe_write(sock, TLN_IAC_C TLN_DONT_C, 2);
+ safe_write(sock, p + 1, 1);
}
} else if (*p == TLN_DO) {
/* DO X -> response: WONT X */
/* except DO ECHO which we just smile and ignore */
if (*(p + 1) != TLN_ECHO) {
- write(sock, TLN_IAC_C TLN_WONT_C, 2);
- write(sock, p + 1, 1);
+ safe_write(sock, TLN_IAC_C TLN_WONT_C, 2);
+ safe_write(sock, p + 1, 1);
}
} else if (*p == TLN_AYT) {
/* "Are You There?" */
/* response is: "Hell, yes!" */
- write(sock, "\r\nHell, yes!\r\n", 14);
+ safe_write(sock, "\r\nHell, yes!\r\n", 14);
} else if (*p == TLN_IAC) {
/* IAC character in data, escaped with another IAC */
*o++ = *p++;
@@ -1335,7 +1335,7 @@ static void dcc_telnet_hostresolved(int i)
dcc_telnet_got_ident(i, userhost);
return;
}
-
+
changeover_dcc(i, &DCC_IDENTWAIT, 0);
dcc[i].timeval = now;
dcc[i].u.ident_sock = dcc[idx].sock;
View
@@ -239,10 +239,13 @@ static void read_lang(char *langfile)
lskip = 1;
}
if (lskip) {
- while (!strchr(lbuf, '\n')) {
- fgets(lbuf, 511, FLANG);
+ while (!strchr(lbuf, '\n') && fgets(lbuf, 511, FLANG) != NULL) {
lline++;
}
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(FLANG)) {
+ putlog(LOG_MISC, "*", "LANG: Error reading lang file.");
+ }
lline++;
lnew = 1;
continue;
View
@@ -1110,16 +1110,20 @@ int main(int arg_c, char **arg_v)
/* Check for pre-existing eggdrop! */
f = fopen(pid_file, "r");
if (f != NULL) {
- fgets(s, 10, f);
- xx = atoi(s);
- i = kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
- * is used */
- if (i == 0 || errno != ESRCH) {
- printf(EGG_RUNNING1, botnetnick);
- printf(EGG_RUNNING2, pid_file);
- bg_send_quit(BG_ABORT);
- exit(1);
+ if (fgets(s, 10, f) != NULL) {
+ xx = atoi(s);
+ i = kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
+ * is used */
+ if (i == 0 || errno != ESRCH) {
+ printf(EGG_RUNNING1, botnetnick);
+ printf(EGG_RUNNING2, pid_file);
+ bg_send_quit(BG_ABORT);
+ exit(1);
+ }
+ } else {
+ printf("Error checking for existing Eggdrop process.\n");
}
+ fclose(f);
}
/* Move into background? */
@@ -1154,9 +1158,15 @@ int main(int arg_c, char **arg_v)
setpgid(0, 0);
#endif
/* Tcl wants the stdin, stdout and stderr file handles kept open. */
- freopen("/dev/null", "r", stdin);
- freopen("/dev/null", "w", stdout);
- freopen("/dev/null", "w", stderr);
+ if (freopen("/dev/null", "r", stdin) == NULL) {
+ putlog(LOG_MISC, "*", "Error renaming stdin file handle: %s", strerror(errno));
+ }
+ if (freopen("/dev/null", "w", stdout) == NULL) {
+ putlog(LOG_MISC, "*", "Error renaming stdout file handle: %s", strerror(errno));
+ }
+ if (freopen("/dev/null", "w", stderr) == NULL) {
+ putlog(LOG_MISC, "*", "Error renaming stderr file handle: %s", strerror(errno));
+ }
#ifdef CYGWIN_HACKS
FreeConsole();
#endif
View
@@ -1065,27 +1065,29 @@ static void scan_help_file(struct help_ref *current, char *filename, int type)
struct help_list_t *list;
if (is_file(filename) && (f = fopen(filename, "r"))) {
- while (!feof(f)) {
- fgets(s, HELP_BUF_LEN, f);
- if (!feof(f)) {
- p = s;
- while ((q = strstr(p, "%{help="))) {
- q += 7;
- if ((p = strchr(q, '}'))) {
- *p = 0;
- list = nmalloc(sizeof *list);
-
- list->name = nmalloc(p - q + 1);
- strcpy(list->name, q);
- list->next = current->first;
- list->type = type;
- current->first = list;
- p++;
- } else
- p = "";
- }
+ /* don't check for feof after fgets, skips last line if it has no \n (ie on windows) */
+ while (!feof(f) && fgets(s, HELP_BUF_LEN, f) != NULL) {
+ p = s;
+ while ((q = strstr(p, "%{help="))) {
+ q += 7;
+ if ((p = strchr(q, '}'))) {
+ *p = 0;
+ list = nmalloc(sizeof *list);
+
+ list->name = nmalloc(p - q + 1);
+ strcpy(list->name, q);
+ list->next = current->first;
+ list->type = type;
+ current->first = list;
+ p++;
+ } else
+ p = "";
}
}
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(f)) {
+ putlog(LOG_MISC, "*", "Error reading help file");
+ }
fclose(f);
}
}
@@ -1214,20 +1216,22 @@ void showhelp(char *who, char *file, struct flag_record *flags, int fl)
if (f) {
help_subst(NULL, NULL, 0, HELP_IRC, NULL); /* Clear flags */
- while (!feof(f)) {
- fgets(s, HELP_BUF_LEN, f);
- if (!feof(f)) {
- if (s[strlen(s) - 1] == '\n')
- s[strlen(s) - 1] = 0;
- if (!s[0])
- strcpy(s, " ");
- help_subst(s, who, flags, 0, file);
- if ((s[0]) && (strlen(s) > 1)) {
- dprintf(DP_HELP, "NOTICE %s :%s\n", who, s);
- lines++;
- }
+ /* don't check for feof after fgets, skips last line if it has no \n (ie on windows) */
+ while (!feof(f) && fgets(s, HELP_BUF_LEN, f) != NULL) {
+ if (s[strlen(s) - 1] == '\n')
+ s[strlen(s) - 1] = 0;
+ if (!s[0])
+ strcpy(s, " ");
+ help_subst(s, who, flags, 0, file);
+ if ((s[0]) && (strlen(s) > 1)) {
+ dprintf(DP_HELP, "NOTICE %s :%s\n", who, s);
+ lines++;
}
}
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(f)) {
+ putlog(LOG_MISC, "*", "Error reading help file");
+ }
fclose(f);
}
if (!lines && !(fl & HELP_TEXT))
@@ -1243,20 +1247,22 @@ static int display_tellhelp(int idx, char *file, FILE *f,
if (f) {
help_subst(NULL, NULL, 0,
(dcc[idx].status & STAT_TELNET) ? 0 : HELP_IRC, NULL);
- while (!feof(f)) {
- fgets(s, HELP_BUF_LEN, f);
- if (!feof(f)) {
- if (s[strlen(s) - 1] == '\n')
- s[strlen(s) - 1] = 0;
- if (!s[0])
- strcpy(s, " ");
- help_subst(s, dcc[idx].nick, flags, 1, file);
- if (s[0]) {
- dprintf(idx, "%s\n", s);
- lines++;
- }
+ /* don't check for feof after fgets, skips last line if it has no \n (ie on windows) */
+ while (!feof(f) && fgets(s, HELP_BUF_LEN, f) != NULL) {
+ if (s[strlen(s) - 1] == '\n')
+ s[strlen(s) - 1] = 0;
+ if (!s[0])
+ strcpy(s, " ");
+ help_subst(s, dcc[idx].nick, flags, 1, file);
+ if (s[0]) {
+ dprintf(idx, "%s\n", s);
+ lines++;
}
}
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(f)) {
+ putlog(LOG_MISC, "*", "Error displaying help");
+ }
fclose(f);
}
return lines;
@@ -1373,17 +1379,19 @@ void show_motd(int idx)
/* reset the help_subst variables to their defaults */
help_subst(NULL, NULL, 0,
(dcc[idx].status & STAT_TELNET) ? 0 : HELP_IRC, NULL);
- while (!feof(vv)) {
- fgets(s, 120, vv);
- if (!feof(vv)) {
- if (s[strlen(s) - 1] == '\n')
- s[strlen(s) - 1] = 0;
- if (!s[0])
- strcpy(s, " ");
- help_subst(s, dcc[idx].nick, &fr, 1, botnetnick);
- if (s[0])
- dprintf(idx, "%s\n", s);
- }
+ /* don't check for feof after fgets, skips last line if it has no \n (ie on windows) */
+ while (!feof(vv) && fgets(s, sizeof s, vv) != NULL) {
+ if (s[strlen(s) - 1] == '\n')
+ s[strlen(s) - 1] = 0;
+ if (!s[0])
+ strcpy(s, " ");
+ help_subst(s, dcc[idx].nick, &fr, 1, botnetnick);
+ if (s[0])
+ dprintf(idx, "%s\n", s);
+ }
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(vv)) {
+ putlog(LOG_MISC, "*", "Error reading MOTD for DCC");
}
fclose(vv);
dprintf(idx, "\n");
@@ -1407,14 +1415,19 @@ void show_banner(int idx)
get_user_flagrec(dcc[idx].user, &fr, dcc[idx].u.chat->con_chan);
/* reset the help_subst variables to their defaults */
help_subst(NULL, NULL, 0, 0, NULL);
- while (!feof(vv)) {
- fgets(s, 120, vv);
- if (!feof(vv)) {
- if (!s[0])
- strcpy(s, " \n");
- help_subst(s, dcc[idx].nick, &fr, 0, botnetnick);
- dprintf(idx, "%s", s);
- }
+ /* don't check for feof after fgets, skips last line if it has no \n (ie on windows) */
+ while (!feof(vv) && fgets(s, sizeof s, vv) != NULL) {
+ if (s[strlen(s) - 1] == '\n')
+ s[strlen(s) - 1] = 0;
+ if (!s[0])
+ strcpy(s, " ");
+ help_subst(s, dcc[idx].nick, &fr, 0, botnetnick);
+ if (s[0])
+ dprintf(idx, "%s\n", s);
+ }
+ /* fgets == NULL means error or empty file, so check for error */
+ if (ferror(vv)) {
+ putlog(LOG_MISC, "*", "Error reading banner");
}
fclose(vv);
}
Oops, something went wrong.

0 comments on commit 9bd9d1b

Please sign in to comment.