coverity/13765,13766,13767,13768: Fix memory leaks in hardcopy.c

The function mch_print_begin() returns early in case of an
error, but without freeing allocated memory.
To fix this, use stack allocation instead of heap allocation
for the variables res_prolog, res_encoding, res_cidfont and
res_cmap.
This commit is contained in:
oni-link 2014-11-16 11:22:58 +01:00
parent 7465fc6ee9
commit e175751364

View File

@ -2434,20 +2434,15 @@ int mch_print_begin(prt_settings_T *psettings)
double right; double right;
double top; double top;
double bottom; double bottom;
struct prt_ps_resource_S *res_prolog; struct prt_ps_resource_S res_prolog;
struct prt_ps_resource_S *res_encoding; struct prt_ps_resource_S res_encoding;
char buffer[256]; char buffer[256];
char_u *p_encoding; char_u *p_encoding;
char_u *p; char_u *p;
struct prt_ps_resource_S *res_cidfont; struct prt_ps_resource_S res_cidfont;
struct prt_ps_resource_S *res_cmap; struct prt_ps_resource_S res_cmap;
int retval = FALSE; int retval = FALSE;
res_prolog = xmalloc(sizeof(struct prt_ps_resource_S));
res_encoding = xmalloc(sizeof(struct prt_ps_resource_S));
res_cidfont = xmalloc(sizeof(struct prt_ps_resource_S));
res_cmap = xmalloc(sizeof(struct prt_ps_resource_S));
/* /*
* PS DSC Header comments - no PS code! * PS DSC Header comments - no PS code!
*/ */
@ -2515,23 +2510,23 @@ int mch_print_begin(prt_settings_T *psettings)
} }
/* Search for external resources VIM supplies */ /* Search for external resources VIM supplies */
if (!prt_find_resource("prolog", res_prolog)) { if (!prt_find_resource("prolog", &res_prolog)) {
EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\"")); EMSG(_("E456: Can't find PostScript resource file \"prolog.ps\""));
return FALSE; return FALSE;
} }
if (!prt_open_resource(res_prolog)) if (!prt_open_resource(&res_prolog))
return FALSE; return FALSE;
if (!prt_check_resource(res_prolog, PRT_PROLOG_VERSION)) if (!prt_check_resource(&res_prolog, PRT_PROLOG_VERSION))
return FALSE; return FALSE;
if (prt_out_mbyte) { if (prt_out_mbyte) {
/* Look for required version of multi-byte printing procset */ /* Look for required version of multi-byte printing procset */
if (!prt_find_resource("cidfont", res_cidfont)) { if (!prt_find_resource("cidfont", &res_cidfont)) {
EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\"")); EMSG(_("E456: Can't find PostScript resource file \"cidfont.ps\""));
return FALSE; return FALSE;
} }
if (!prt_open_resource(res_cidfont)) if (!prt_open_resource(&res_cidfont))
return FALSE; return FALSE;
if (!prt_check_resource(res_cidfont, PRT_CID_PROLOG_VERSION)) if (!prt_check_resource(&res_cidfont, PRT_CID_PROLOG_VERSION))
return FALSE; return FALSE;
} }
@ -2543,25 +2538,25 @@ int mch_print_begin(prt_settings_T *psettings)
if (!prt_out_mbyte) { if (!prt_out_mbyte) {
p_encoding = enc_skip(p_penc); p_encoding = enc_skip(p_penc);
if (*p_encoding == NUL if (*p_encoding == NUL
|| !prt_find_resource((char *)p_encoding, res_encoding)) { || !prt_find_resource((char *)p_encoding, &res_encoding)) {
/* 'printencoding' not set or not supported - find alternate */ /* 'printencoding' not set or not supported - find alternate */
int props; int props;
p_encoding = enc_skip(p_enc); p_encoding = enc_skip(p_enc);
props = enc_canon_props(p_encoding); props = enc_canon_props(p_encoding);
if (!(props & ENC_8BIT) if (!(props & ENC_8BIT)
|| !prt_find_resource((char *)p_encoding, res_encoding)) { || !prt_find_resource((char *)p_encoding, &res_encoding)) {
/* 8-bit 'encoding' is not supported */ /* 8-bit 'encoding' is not supported */
/* Use latin1 as default printing encoding */ /* Use latin1 as default printing encoding */
p_encoding = (char_u *)"latin1"; p_encoding = (char_u *)"latin1";
if (!prt_find_resource((char *)p_encoding, res_encoding)) { if (!prt_find_resource((char *)p_encoding, &res_encoding)) {
EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""), EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
p_encoding); p_encoding);
return FALSE; return FALSE;
} }
} }
} }
if (!prt_open_resource(res_encoding)) if (!prt_open_resource(&res_encoding))
return FALSE; return FALSE;
/* For the moment there are no checks on encoding resource files to /* For the moment there are no checks on encoding resource files to
* perform */ * perform */
@ -2571,12 +2566,12 @@ int mch_print_begin(prt_settings_T *psettings)
p_encoding = enc_skip(p_enc); p_encoding = enc_skip(p_enc);
if (prt_use_courier) { if (prt_use_courier) {
/* Include ASCII range encoding vector */ /* Include ASCII range encoding vector */
if (!prt_find_resource(prt_ascii_encoding, res_encoding)) { if (!prt_find_resource(prt_ascii_encoding, &res_encoding)) {
EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""), EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
prt_ascii_encoding); prt_ascii_encoding);
return FALSE; return FALSE;
} }
if (!prt_open_resource(res_encoding)) if (!prt_open_resource(&res_encoding))
return FALSE; return FALSE;
/* For the moment there are no checks on encoding resource files to /* For the moment there are no checks on encoding resource files to
* perform */ * perform */
@ -2597,37 +2592,37 @@ int mch_print_begin(prt_settings_T *psettings)
if (prt_out_mbyte && prt_custom_cmap) { if (prt_out_mbyte && prt_custom_cmap) {
/* Find user supplied CMap */ /* Find user supplied CMap */
if (!prt_find_resource(prt_cmap, res_cmap)) { if (!prt_find_resource(prt_cmap, &res_cmap)) {
EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""), EMSG2(_("E456: Can't find PostScript resource file \"%s.ps\""),
prt_cmap); prt_cmap);
return FALSE; return FALSE;
} }
if (!prt_open_resource(res_cmap)) if (!prt_open_resource(&res_cmap))
return FALSE; return FALSE;
} }
/* List resources supplied */ /* List resources supplied */
STRCPY(buffer, res_prolog->title); STRCPY(buffer, res_prolog.title);
STRCAT(buffer, " "); STRCAT(buffer, " ");
STRCAT(buffer, res_prolog->version); STRCAT(buffer, res_prolog.version);
prt_dsc_resources("DocumentSuppliedResources", "procset", buffer); prt_dsc_resources("DocumentSuppliedResources", "procset", buffer);
if (prt_out_mbyte) { if (prt_out_mbyte) {
STRCPY(buffer, res_cidfont->title); STRCPY(buffer, res_cidfont.title);
STRCAT(buffer, " "); STRCAT(buffer, " ");
STRCAT(buffer, res_cidfont->version); STRCAT(buffer, res_cidfont.version);
prt_dsc_resources(NULL, "procset", buffer); prt_dsc_resources(NULL, "procset", buffer);
if (prt_custom_cmap) { if (prt_custom_cmap) {
STRCPY(buffer, res_cmap->title); STRCPY(buffer, res_cmap.title);
STRCAT(buffer, " "); STRCAT(buffer, " ");
STRCAT(buffer, res_cmap->version); STRCAT(buffer, res_cmap.version);
prt_dsc_resources(NULL, "cmap", buffer); prt_dsc_resources(NULL, "cmap", buffer);
} }
} }
if (!prt_out_mbyte || prt_use_courier) { if (!prt_out_mbyte || prt_use_courier) {
STRCPY(buffer, res_encoding->title); STRCPY(buffer, res_encoding.title);
STRCAT(buffer, " "); STRCAT(buffer, " ");
STRCAT(buffer, res_encoding->version); STRCAT(buffer, res_encoding.version);
prt_dsc_resources(NULL, "encoding", buffer); prt_dsc_resources(NULL, "encoding", buffer);
} }
prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate, prt_dsc_requirements(prt_duplex, prt_tumble, prt_collate,
@ -2661,20 +2656,20 @@ int mch_print_begin(prt_settings_T *psettings)
prt_dsc_noarg("BeginProlog"); prt_dsc_noarg("BeginProlog");
/* Add required procsets - NOTE: order is important! */ /* Add required procsets - NOTE: order is important! */
if (!prt_add_resource(res_prolog)) if (!prt_add_resource(&res_prolog))
return FALSE; return FALSE;
if (prt_out_mbyte) { if (prt_out_mbyte) {
/* Add CID font procset, and any user supplied CMap */ /* Add CID font procset, and any user supplied CMap */
if (!prt_add_resource(res_cidfont)) if (!prt_add_resource(&res_cidfont))
return FALSE; return FALSE;
if (prt_custom_cmap && !prt_add_resource(res_cmap)) if (prt_custom_cmap && !prt_add_resource(&res_cmap))
return FALSE; return FALSE;
} }
if (!prt_out_mbyte || prt_use_courier) if (!prt_out_mbyte || prt_use_courier)
/* There will be only one Roman font encoding to be included in the PS /* There will be only one Roman font encoding to be included in the PS
* file. */ * file. */
if (!prt_add_resource(res_encoding)) if (!prt_add_resource(&res_encoding))
return FALSE; return FALSE;
prt_dsc_noarg("EndProlog"); prt_dsc_noarg("EndProlog");
@ -2780,11 +2775,6 @@ int mch_print_begin(prt_settings_T *psettings)
/* Fail if any problems writing out to the PS file */ /* Fail if any problems writing out to the PS file */
retval = !prt_file_error; retval = !prt_file_error;
free(res_prolog);
free(res_encoding);
free(res_cidfont);
free(res_cmap);
return retval; return retval;
} }