From e550d65c8570aeeab1de5294dd9273adf73ddb6c Mon Sep 17 00:00:00 2001 From: Garance A Drosehn Date: Mon, 20 Mar 2017 22:36:28 +0000 Subject: [PATCH] Fixes to chkprintcap: - Check the return from a call to malloc() in skim_printcap(), and return a NULL if that fails. - Fix a small memory leak in main() that happens if skim_printcap() returns an error, including the new error-return of NULL. Submitted by: Tom Rix Reviewed by: pfg, ngie MFC after: 4 weeks Sponsored by: Dell EMC Isilon, Juniper Differential Revision: D9954, D9982 --- usr.sbin/lpr/chkprintcap/chkprintcap.c | 12 ++++++++++-- usr.sbin/lpr/chkprintcap/skimprintcap.c | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/usr.sbin/lpr/chkprintcap/chkprintcap.c b/usr.sbin/lpr/chkprintcap/chkprintcap.c index 96fe4896fb2e..fab2239adbd2 100644 --- a/usr.sbin/lpr/chkprintcap/chkprintcap.c +++ b/usr.sbin/lpr/chkprintcap/chkprintcap.c @@ -113,8 +113,13 @@ main(int argc, char **argv) * the printcap file. */ skres = skim_printcap(pcap_fname, verbosity); - if (skres->fatalerr) - return (skres->fatalerr); + if (skres == NULL) { + problems = 1; + goto main_ret; + } else if (skres->fatalerr) { + problems = skres->fatalerr; + goto main_ret; + } /* * Now use the standard capability-db routines to check the values @@ -156,6 +161,9 @@ main(int argc, char **argv) warnx("WARNING: but only found %d queues to process!", queuecnt); } + +main_ret: + free(pcap_fname); return (problems); } diff --git a/usr.sbin/lpr/chkprintcap/skimprintcap.c b/usr.sbin/lpr/chkprintcap/skimprintcap.c index da2828290b8b..34e61ceb8fb2 100644 --- a/usr.sbin/lpr/chkprintcap/skimprintcap.c +++ b/usr.sbin/lpr/chkprintcap/skimprintcap.c @@ -82,6 +82,8 @@ skim_printcap(const char *pcap_fname, int verbosity) enum {CMNT_LINE, ENTRY_LINE, TAB_LINE, TABERR_LINE} is_type, had_type; skinf = malloc(sizeof(struct skiminfo)); + if (skinf == NULL) + return (NULL); memset(skinf, 0, sizeof(struct skiminfo)); pc_file = fopen(pcap_fname, "r");