Fix memory leak in PostScript

Also match ZBarcode_Encode prototype with definition
Fixes thanks to Alex Haley <ahaley42@sf>, Ref ticket #33
This commit is contained in:
Robin Stuart 2016-09-13 21:30:19 +01:00
parent 343b3b873e
commit b49f3f0255
2 changed files with 37 additions and 18 deletions

View File

@ -786,17 +786,17 @@ static int reduced_charset(struct zint_symbol *symbol, const unsigned char *sour
return error_number; return error_number;
} }
int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int in_length) { int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int length) {
int error_number, error_buffer, i; int error_number, error_buffer, i;
#ifdef _MSC_VER #ifdef _MSC_VER
unsigned char* local_source; unsigned char* local_source;
#endif #endif
error_number = 0; error_number = 0;
if (in_length == 0) { if (length == 0) {
in_length = (int) ustrlen(source); length = (int) ustrlen(source);
} }
if (in_length == 0) { if (length == 0) {
strcpy(symbol->errtxt, "No input data"); strcpy(symbol->errtxt, "No input data");
error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA); error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA);
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_DATA;
@ -810,9 +810,9 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int in_le
#endif #endif
} }
#ifndef _MSC_VER #ifndef _MSC_VER
unsigned char local_source[in_length + 1]; unsigned char local_source[length + 1];
#else #else
local_source = (unsigned char*) _alloca(in_length + 1); local_source = (unsigned char*) _alloca(length + 1);
#endif #endif
/* First check the symbology field */ /* First check the symbology field */
@ -965,25 +965,25 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int in_le
} }
if (symbol->input_mode == GS1_MODE) { if (symbol->input_mode == GS1_MODE) {
for (i = 0; i < in_length; i++) { for (i = 0; i < length; i++) {
if (source[i] == '\0') { if (source[i] == '\0') {
strcpy(symbol->errtxt, "NULL characters not permitted in GS1 mode"); strcpy(symbol->errtxt, "NULL characters not permitted in GS1 mode");
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_DATA;
} }
} }
if (gs1_compliant(symbol->symbology) == 1) { if (gs1_compliant(symbol->symbology) == 1) {
error_number = ugs1_verify(symbol, source, in_length, local_source); error_number = ugs1_verify(symbol, source, length, local_source);
if (error_number != 0) { if (error_number != 0) {
return error_number; return error_number;
} }
in_length = ustrlen(local_source); length = ustrlen(local_source);
} else { } else {
strcpy(symbol->errtxt, "Selected symbology does not support GS1 mode"); strcpy(symbol->errtxt, "Selected symbology does not support GS1 mode");
return ZINT_ERROR_INVALID_OPTION; return ZINT_ERROR_INVALID_OPTION;
} }
} else { } else {
memcpy(local_source, source, in_length); memcpy(local_source, source, length);
local_source[in_length] = '\0'; local_source[length] = '\0';
} }
switch (symbol->symbology) { switch (symbol->symbology) {
@ -991,17 +991,17 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int in_le
case BARCODE_MICROQR: case BARCODE_MICROQR:
case BARCODE_GRIDMATRIX: case BARCODE_GRIDMATRIX:
case BARCODE_HANXIN: case BARCODE_HANXIN:
error_number = extended_charset(symbol, local_source, in_length); error_number = extended_charset(symbol, local_source, length);
break; break;
default: default:
error_number = reduced_charset(symbol, local_source, in_length); error_number = reduced_charset(symbol, local_source, length);
break; break;
} }
if ((error_number == ZINT_ERROR_INVALID_DATA) && (supports_eci(symbol->symbology) if ((error_number == ZINT_ERROR_INVALID_DATA) && (supports_eci(symbol->symbology)
&& (symbol->input_mode == UNICODE_MODE))) { && (symbol->input_mode == UNICODE_MODE))) {
/* Try another ECI mode */ /* Try another ECI mode */
symbol->eci = get_best_eci(local_source, in_length); symbol->eci = get_best_eci(local_source, length);
if (symbol->eci >= 3) { if (symbol->eci >= 3) {
@ -1012,18 +1012,18 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source, int in_le
case BARCODE_MICROQR: case BARCODE_MICROQR:
case BARCODE_GRIDMATRIX: case BARCODE_GRIDMATRIX:
case BARCODE_HANXIN: case BARCODE_HANXIN:
error_number = utf_to_eci(symbol->eci, source, local_source, &in_length); error_number = utf_to_eci(symbol->eci, source, local_source, &length);
error_number = extended_charset(symbol, local_source, in_length); error_number = extended_charset(symbol, local_source, length);
break; break;
default: default:
error_number = reduced_charset(symbol, local_source, in_length); error_number = reduced_charset(symbol, local_source, length);
break; break;
} }
} }
} }
if ((symbol->symbology == BARCODE_CODE128) || (symbol->symbology == BARCODE_CODE128B)) { if ((symbol->symbology == BARCODE_CODE128) || (symbol->symbology == BARCODE_CODE128B)) {
for (i = 0; i < in_length; i++) { for (i = 0; i < length; i++) {
if (local_source[i] == '\0') { if (local_source[i] == '\0') {
symbol->text[i] = ' '; symbol->text[i] = ' ';
} else { } else {

View File

@ -106,6 +106,9 @@ int ps_plot(struct zint_symbol *symbol) {
} }
if (feps == NULL) { if (feps == NULL) {
strcpy(symbol->errtxt, "Could not open output file"); strcpy(symbol->errtxt, "Could not open output file");
#ifdef _MSC_VER
free(local_text);
#endif
return ZINT_ERROR_FILE_ACCESS; return ZINT_ERROR_FILE_ACCESS;
} }
@ -116,23 +119,35 @@ int ps_plot(struct zint_symbol *symbol) {
if (strlen(symbol->fgcolour) != 6) { if (strlen(symbol->fgcolour) != 6) {
strcpy(symbol->errtxt, "Malformed foreground colour target"); strcpy(symbol->errtxt, "Malformed foreground colour target");
fclose(feps); fclose(feps);
#ifdef _MSC_VER
free(local_text);
#endif
return ZINT_ERROR_INVALID_OPTION; return ZINT_ERROR_INVALID_OPTION;
} }
if (strlen(symbol->bgcolour) != 6) { if (strlen(symbol->bgcolour) != 6) {
strcpy(symbol->errtxt, "Malformed background colour target"); strcpy(symbol->errtxt, "Malformed background colour target");
fclose(feps); fclose(feps);
#ifdef _MSC_VER
free(local_text);
#endif
return ZINT_ERROR_INVALID_OPTION; return ZINT_ERROR_INVALID_OPTION;
} }
error_number = is_sane(SSET, (unsigned char*) symbol->fgcolour, strlen(symbol->fgcolour)); error_number = is_sane(SSET, (unsigned char*) symbol->fgcolour, strlen(symbol->fgcolour));
if (error_number == ZINT_ERROR_INVALID_DATA) { if (error_number == ZINT_ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Malformed foreground colour target"); strcpy(symbol->errtxt, "Malformed foreground colour target");
fclose(feps); fclose(feps);
#ifdef _MSC_VER
free(local_text);
#endif
return ZINT_ERROR_INVALID_OPTION; return ZINT_ERROR_INVALID_OPTION;
} }
error_number = is_sane(SSET, (unsigned char*) symbol->bgcolour, strlen(symbol->bgcolour)); error_number = is_sane(SSET, (unsigned char*) symbol->bgcolour, strlen(symbol->bgcolour));
if (error_number == ZINT_ERROR_INVALID_DATA) { if (error_number == ZINT_ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Malformed background colour target"); strcpy(symbol->errtxt, "Malformed background colour target");
fclose(feps); fclose(feps);
#ifdef _MSC_VER
free(local_text);
#endif
return ZINT_ERROR_INVALID_OPTION; return ZINT_ERROR_INVALID_OPTION;
} }
locale = setlocale(LC_ALL, "C"); locale = setlocale(LC_ALL, "C");
@ -951,5 +966,9 @@ int ps_plot(struct zint_symbol *symbol) {
if (locale) if (locale)
setlocale(LC_ALL, locale); setlocale(LC_ALL, locale);
#ifdef _MSC_VER
free(local_text);
#endif
return error_number; return error_number;
} }