From ea96823f2626e2a4178eec54f8a78a666d376ac1 Mon Sep 17 00:00:00 2001 From: kpfleming Date: Sat, 1 Nov 2008 18:22:39 +0000 Subject: fix a bunch of potential problems found by gcc 4.3.x, primarily bare strings being passed to printf()-like functions and ignored results from read()/write() and friends git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@153337 f38db490-d61c-443f-a65b-d21fe96a405b --- main/asterisk.c | 112 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 42 deletions(-) (limited to 'main/asterisk.c') diff --git a/main/asterisk.c b/main/asterisk.c index 0a978a949..941789b53 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -1157,8 +1157,11 @@ static void hup_handler(int num) if (restartnow) execvp(_argv[0], _argv); sig_flags.need_reload = 1; - if (sig_alert_pipe[1] != -1) - write(sig_alert_pipe[1], &a, sizeof(a)); + if (sig_alert_pipe[1] != -1) { + if (write(sig_alert_pipe[1], &a, sizeof(a)) < 0) { + fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno)); + } + } signal(num, hup_handler); } @@ -1316,7 +1319,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart) close(ast_consock); if (!ast_opt_remote) unlink(ast_config_AST_PID); - printf(term_quit()); + printf("%s", term_quit()); if (restart) { if (option_verbose || ast_opt_console) ast_verbose("Preparing for Asterisk restart...\n"); @@ -1351,8 +1354,11 @@ static void __quit_handler(int num) { int a = 0; sig_flags.need_quit = 1; - if (sig_alert_pipe[1] != -1) - write(sig_alert_pipe[1], &a, sizeof(a)); + if (sig_alert_pipe[1] != -1) { + if (write(sig_alert_pipe[1], &a, sizeof(a)) < 0) { + fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno)); + } + } /* There is no need to restore the signal handler here, since the app * is going to exit */ } @@ -1411,7 +1417,7 @@ static int ast_all_zeros(char *s) static void consolehandler(char *s) { - printf(term_end()); + printf("%s", term_end()); fflush(stdout); /* Called when readline data is available */ @@ -1621,8 +1627,8 @@ static int show_warranty(int fd, int argc, char *argv[]) { int x; - for (x = 0; x < sizeof(warranty_lines) / sizeof(warranty_lines[0]); x++) - ast_cli(fd, (char *) warranty_lines[x]); + for (x = 0; x < ARRAY_LEN(warranty_lines); x++) + ast_cli(fd, "%s", (char *) warranty_lines[x]); return RESULT_SUCCESS; } @@ -1650,8 +1656,8 @@ static int show_license(int fd, int argc, char *argv[]) { int x; - for (x = 0; x < sizeof(license_lines) / sizeof(license_lines[0]); x++) - ast_cli(fd, (char *) license_lines[x]); + for (x = 0; x < ARRAY_LEN(license_lines); x++) + ast_cli(fd, "%s", (char *) license_lines[x]); return RESULT_SUCCESS; } @@ -1794,7 +1800,7 @@ static int ast_el_read_char(EditLine *el, char *cp) for (tries=0; tries < 30 * reconnects_per_second; tries++) { if (ast_tryconnect()) { fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries); - printf(term_quit()); + printf("%s", term_quit()); WELCOME_MESSAGE; if (!ast_opt_mute) fdsend(ast_consock, "logger mute silent"); @@ -1823,9 +1829,12 @@ static int ast_el_read_char(EditLine *el, char *cp) } /* Write over the CLI prompt */ - if (!ast_opt_exec && !lastpos) - write(STDOUT_FILENO, "\r", 1); - write(STDOUT_FILENO, buf, res); + if (!ast_opt_exec && !lastpos) { + if (write(STDOUT_FILENO, "\r", 1) < 0) { + } + } + if (write(STDOUT_FILENO, buf, res) < 0) { + } if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (buf[res-2] == '\n'))) { *cp = CC_REFRESH; return(1); @@ -1912,8 +1921,13 @@ static char *cli_prompt(EditLine *el) if ((LOADAVG = fopen("/proc/loadavg", "r"))) { float avg1, avg2, avg3; int actproc, totproc, npid, which; - fscanf(LOADAVG, "%f %f %f %d/%d %d", - &avg1, &avg2, &avg3, &actproc, &totproc, &npid); + + if (fscanf(LOADAVG, "%f %f %f %d/%d %d", + &avg1, &avg2, &avg3, &actproc, &totproc, &npid) != 6) { + ast_log(LOG_WARNING, "parsing /proc/loadavg failed\n"); + fclose(LOADAVG); + break; + } if (sscanf(t, "%d", &which) == 1) { switch (which) { case 1: @@ -1933,6 +1947,7 @@ static char *cli_prompt(EditLine *el) break; } } + fclose(LOADAVG); } break; #endif @@ -2261,7 +2276,9 @@ static int ast_el_read_history(char *filename) return ret; while (!feof(f)) { - fgets(buf, sizeof(buf), f); + if (!fgets(buf, sizeof(buf), f)) { + continue; + } if (!strcmp(buf, "_HiStOrY_V2_\n")) continue; if (ast_all_zeros(buf)) @@ -2274,7 +2291,7 @@ static int ast_el_read_history(char *filename) return ret; } -static void ast_remotecontrol(char * data) +static void ast_remotecontrol(char *data) { char buf[80]; int res; @@ -2289,9 +2306,15 @@ static void ast_remotecontrol(char * data) char *ebuf; int num = 0; - read(ast_consock, buf, sizeof(buf)); - if (data) - write(ast_consock, data, strlen(data) + 1); + if (read(ast_consock, buf, sizeof(buf)) < 0) { + ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno)); + return; + } + if (data) { + if (write(ast_consock, data, strlen(data) + 1) < 0) { + ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno)); + } + } stringp = buf; hostname = strsep(&stringp, "/"); cpid = strsep(&stringp, "/"); @@ -2349,7 +2372,9 @@ static void ast_remotecontrol(char * data) /* Skip verbose lines */ if (*curline != 127) { not_written = 0; - write(STDOUT_FILENO, curline, nextline - curline); + if (write(STDOUT_FILENO, curline, nextline - curline) < 0) { + ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno)); + } } curline = nextline; } while (!ast_strlen_zero(curline)); @@ -2619,7 +2644,8 @@ static void *monitor_sig_flags(void *unused) sig_flags.need_quit = 0; quit_handler(0, 0, 1, 0); } - read(sig_alert_pipe[0], &a, sizeof(a)); + if (read(sig_alert_pipe[0], &a, sizeof(a)) != sizeof(a)) { + } } return NULL; @@ -2884,7 +2910,7 @@ int main(int argc, char *argv[]) #endif ast_term_init(); - printf(term_end()); + printf("%s", term_end()); fflush(stdout); if (ast_opt_console && !option_verbose) @@ -2909,18 +2935,18 @@ int main(int argc, char *argv[]) quit_handler(0, 0, 0, 0); exit(0); } - printf(term_quit()); + printf("%s", term_quit()); ast_remotecontrol(NULL); quit_handler(0, 0, 0, 0); exit(0); } else { ast_log(LOG_ERROR, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", ast_config_AST_SOCKET); - printf(term_quit()); + printf("%s", term_quit()); exit(1); } } else if (ast_opt_remote || ast_opt_exec) { ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET); - printf(term_quit()); + printf("%s", term_quit()); exit(1); } /* Blindly write pid file since we couldn't connect */ @@ -2935,7 +2961,9 @@ int main(int argc, char *argv[]) #if HAVE_WORKING_FORK if (ast_opt_always_fork || !ast_opt_no_fork) { #ifndef HAVE_SBIN_LAUNCHD - daemon(1, 0); + if (daemon(1, 0) < 0) { + ast_log(LOG_ERROR, "daemon() failed: %s\n", strerror(errno)); + } ast_mainpid = getpid(); /* Blindly re-write pid file since we are forking */ unlink(ast_config_AST_PID); @@ -2977,7 +3005,7 @@ int main(int argc, char *argv[]) initstate((unsigned int) getpid() * 65536 + (unsigned int) time(NULL), randompool, sizeof(randompool)); if (init_logger()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } @@ -3048,12 +3076,12 @@ int main(int argc, char *argv[]) ast_autoservice_init(); if (load_modules(1)) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (dnsmgr_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } @@ -3062,12 +3090,12 @@ int main(int argc, char *argv[]) ast_channels_init(); if (ast_cdr_engine_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (ast_device_state_engine_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } @@ -3076,37 +3104,37 @@ int main(int argc, char *argv[]) ast_udptl_init(); if (ast_image_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (ast_file_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (load_pbx()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (init_framer()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (astdb_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (ast_enum_init()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } if (load_modules(0)) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } @@ -3115,7 +3143,7 @@ int main(int argc, char *argv[]) * registering manager actions. This will cause reversed locking * order between the module list and manager actions list. */ if (init_manager()) { - printf(term_quit()); + printf("%s", term_quit()); exit(1); } @@ -3126,7 +3154,7 @@ int main(int argc, char *argv[]) if (ast_opt_console && !option_verbose) ast_verbose(" ]\n"); if (option_verbose || ast_opt_console) - ast_verbose(term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp))); + ast_verbose("%s", term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp))); if (ast_opt_no_fork) consolethread = pthread_self(); -- cgit v1.2.3