From 18b986156fe3a88df242104553511f450bdb2e00 Mon Sep 17 00:00:00 2001 From: hooper114 Date: Thu, 18 Jun 2009 10:20:23 +0000 Subject: [PATCH] Correct some memory leaks found by MSVC --- backend/aztec.c | 12 ++++++------ backend/aztec.h | 2 +- backend/code.c | 8 ++++++-- backend/common.c | 15 ++++++++++++++- backend/common.h | 1 + backend/library.c | 3 ++- backend/png.c | 16 +++++++++------- backend/ps.c | 16 +++++++++------- backend/svg.c | 16 +++++++++------- win32/test.bat | 6 +++--- 10 files changed, 60 insertions(+), 35 deletions(-) diff --git a/backend/aztec.c b/backend/aztec.c index 73261efb..03d46b17 100644 --- a/backend/aztec.c +++ b/backend/aztec.c @@ -23,7 +23,7 @@ #include #include #ifdef _MSC_VER -#include +#include #endif #include "common.h" #include "aztec.h" @@ -55,14 +55,14 @@ int aztec_text_process(unsigned char source[], char binary_string[], int gs1) int i, j, k, bytes; int curtable, newtable, lasttable, chartype, maplength, blocks, debug; #ifndef _MSC_VER - int charmap[ustrlen(source)], typemap[ustrlen(source)]; + int charmap[ustrlen(source) * 2], typemap[ustrlen(source) * 2]; int blockmap[2][ustrlen(source)]; #else - int* charmap = (int*)_alloca(ustrlen(source) * sizeof(int)); - int* typemap = (int*)_alloca(ustrlen(source) * sizeof(int)); + int* charmap = (int*)_alloca((ustrlen(source) * 2) * sizeof(int)); + int* typemap = (int*)_alloca((ustrlen(source) * 2) * sizeof(int)); int* blockmap[2]; - blockmap[0] = (int*)_alloca(ustrlen(source) * sizeof(int)); - blockmap[1] = (int*)_alloca(ustrlen(source) * sizeof(int)); + blockmap[0] = (int*)_alloca(ustrlen(source) * sizeof(int)); + blockmap[1] = (int*)_alloca(ustrlen(source) * sizeof(int)); #endif /* Lookup input string in encoding table */ maplength = 0; diff --git a/backend/aztec.h b/backend/aztec.h index f34814e8..93e0e278 100644 --- a/backend/aztec.h +++ b/backend/aztec.h @@ -241,7 +241,7 @@ static char *hexbit[32] = {"00000", "00001", "00010", "00011", "00100", "00101", "10110", "10111", "11000", "11001", "11010", "11011", "11100", "11101", "11110", "11111" }; -static char *pentbit[32] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", +static char *pentbit[16] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; diff --git a/backend/code.c b/backend/code.c index 63fccc5e..b3067d75 100644 --- a/backend/code.c +++ b/backend/code.c @@ -169,10 +169,12 @@ int c39(struct zint_symbol *symbol, unsigned char source[]) char check_digit; int h, error_number; char dest[1000]; + char localstr[3]; error_number = 0; counter = 0; strcpy(dest, ""); + strcpy(localstr, ""); if((symbol->option_2 < 0) || (symbol->option_2 > 1)) { symbol->option_2 = 0; @@ -226,8 +228,8 @@ int c39(struct zint_symbol *symbol, unsigned char source[]) } h = ustrlen(source); - source[h] = check_digit; - source[h + 1] = '\0'; + localstr[0] = check_digit; + localstr[1] = '\0'; } /* Stop character */ @@ -247,9 +249,11 @@ int c39(struct zint_symbol *symbol, unsigned char source[]) if(symbol->symbology == BARCODE_CODE39) { ustrcpy(symbol->text, (unsigned char*)"*"); uconcat(symbol->text, source); + uconcat(symbol->text, (unsigned char*)localstr); uconcat(symbol->text, (unsigned char*)"*"); } else { ustrcpy(symbol->text, source); + uconcat(symbol->text, (unsigned char*)localstr); } return error_number; } diff --git a/backend/common.c b/backend/common.c index c0c8170a..14cd9fbb 100644 --- a/backend/common.c +++ b/backend/common.c @@ -92,7 +92,7 @@ int is_sane(char test_string[], unsigned char source[]) { /* Verifies that a string only uses valid characters */ unsigned int i, j, latch; - for(i = 0; i < ustrlen(source); i++) { + for(i = 0; i < ustrlen(source) - 1; i++) { latch = FALSE; for(j = 0; j < strlen(test_string); j++) { if (source[i] == test_string[j]) { latch = TRUE; } } @@ -231,6 +231,19 @@ int is_stackable(int symbology) { return 0; } +int is_extendable(int symbology) { + /* Indicates which symbols can have addon */ + if(symbology == BARCODE_EANX) { return 1; } + if(symbology == BARCODE_UPCA) { return 1; } + if(symbology == BARCODE_UPCE) { return 1; } + if(symbology == BARCODE_ISBNX) { return 1; } + if(symbology == BARCODE_UPCA_CC) { return 1; } + if(symbology == BARCODE_UPCE_CC) { return 1; } + if(symbology == BARCODE_EANX_CC) { return 1; } + + return 0; +} + int roundup(float input) { float remainder; diff --git a/backend/common.h b/backend/common.h index 4ae05ed6..c439683e 100644 --- a/backend/common.h +++ b/backend/common.h @@ -53,6 +53,7 @@ extern void lookup(char set_string[], char *table[], char data, char dest[]); extern int posn(char set_string[], char data); extern void expand(struct zint_symbol *symbol, char data[]); extern int is_stackable(int symbology); +extern int is_extendable(int symbology); extern int roundup(float input); extern int module_is_set(struct zint_symbol *symbol, int y_coord, int x_coord); extern void set_module(struct zint_symbol *symbol, int y_coord, int x_coord); diff --git a/backend/library.c b/backend/library.c index 0e08c547..b9539f22 100644 --- a/backend/library.c +++ b/backend/library.c @@ -389,6 +389,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source) int input_length; input_length = ustrlen(source); + #ifndef _MSC_VER unsigned char preprocessed[input_length]; #else @@ -595,7 +596,7 @@ int ZBarcode_Print(struct zint_symbol *symbol) /* int i, j; for(i = 0; i < symbol->rows; i++) { - for(j = 0; j < symbol->width / 7; j++) { + for(j = 0; j <= symbol->width / 7; j++) { printf("%2.2X ", symbol->encoded_data[i][j]); } printf("\n"); diff --git a/backend/png.c b/backend/png.c index c9bbc8d7..294fa3b9 100644 --- a/backend/png.c +++ b/backend/png.c @@ -555,13 +555,15 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle) latch = 0; r = 0; /* Isolate add-on text */ - for(i = 0; i < ustrlen(local_text); i++) { - if (latch == 1) { - addon[r] = local_text[i]; - r++; - } - if (symbol->text[i] == '+') { - latch = 1; + if(is_extendable(symbol->symbology)) { + for(i = 0; i < ustrlen(local_text); i++) { + if (latch == 1) { + addon[r] = local_text[i]; + r++; + } + if (symbol->text[i] == '+') { + latch = 1; + } } } addon[r] = '\0'; diff --git a/backend/ps.c b/backend/ps.c index 90613de3..6e9db17c 100644 --- a/backend/ps.c +++ b/backend/ps.c @@ -154,13 +154,15 @@ int ps_plot(struct zint_symbol *symbol) latch = 0; r = 0; /* Isolate add-on text */ - for(i = 0; i < ustrlen(symbol->text); i++) { - if (latch == 1) { - addon[r] = symbol->text[i]; - r++; - } - if (symbol->text[i] == '+') { - latch = 1; + if(is_extendable(symbol->symbology)) { + for(i = 0; i < ustrlen(symbol->text); i++) { + if (latch == 1) { + addon[r] = symbol->text[i]; + r++; + } + if (symbol->text[i] == '+') { + latch = 1; + } } } addon[r] = '\0'; diff --git a/backend/svg.c b/backend/svg.c index 1ce3830c..b2b2ddbe 100644 --- a/backend/svg.c +++ b/backend/svg.c @@ -150,13 +150,15 @@ int svg_plot(struct zint_symbol *symbol) latch = 0; r = 0; /* Isolate add-on text */ - for(i = 0; i < ustrlen(symbol->text); i++) { - if (latch == 1) { - addon[r] = symbol->text[i]; - r++; - } - if (symbol->text[i] == '+') { - latch = 1; + if(is_extendable(symbol->symbology)) { + for(i = 0; i < ustrlen(symbol->text); i++) { + if (latch == 1) { + addon[r] = symbol->text[i]; + r++; + } + if (symbol->text[i] == '+') { + latch = 1; + } } } addon[r] = '\0'; diff --git a/win32/test.bat b/win32/test.bat index 2dba0ac8..38a8ab5b 100755 --- a/win32/test.bat +++ b/win32/test.bat @@ -613,11 +613,11 @@ zint -o bar92.eps -b 92 --border=10 -d "Demonstration Aztec Code symbol generate zint -o bar92.svg -b 92 --border=10 -d "Demonstration Aztec Code symbol generated by libzint" -echo zint -o bar92a.png -b 92 --gs1 --border=10 -d "[01]98898765432106[02]13012345678909[10]1234567ABCDEFG[3202]012345[15]991231" +zint -o bar92a.png -b 92 --gs1 --border=10 -d "[01]98898765432106[02]13012345678909[10]1234567ABCDEFG[3202]012345[15]991231" -echo zint -o bar92a.eps -b 92 --gs1 --border=10 -d "[01]98898765432106[02]13012345678909[10]1234567ABCDEFG[3202]012345[15]991231" +zint -o bar92a.eps -b 92 --gs1 --border=10 -d "[01]98898765432106[02]13012345678909[10]1234567ABCDEFG[3202]012345[15]991231" -echo zint -o bar92a.svg -b 92 --gs1 --border=10 -d "[01]98898765432106[02]13012345678909[10]1234567ABCDEFG[3202]012345[15]991231" +zint -o bar92a.svg -b 92 --gs1 --border=10 -d "[01]98898765432106[02]13012345678909[10]1234567ABCDEFG[3202]012345[15]991231" echo testing DAFT Code