Fossil SCM

plug possible memory leak in compress/decompress SQL functions.

jan.nijtmans 2015-04-13 13:35 trunk
Commit abef6cf7683f4a0397e60f4a75315e422080b726
1 file changed +9 -2
+9 -2
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -66,21 +66,27 @@
6666
){
6767
const unsigned char *pIn;
6868
unsigned char *pOut;
6969
unsigned int nIn;
7070
unsigned long int nOut;
71
+ int rc;
7172
7273
pIn = sqlite3_value_blob(argv[0]);
7374
nIn = sqlite3_value_bytes(argv[0]);
7475
nOut = 13 + nIn + (nIn+999)/1000;
7576
pOut = sqlite3_malloc( nOut+4 );
7677
pOut[0] = nIn>>24 & 0xff;
7778
pOut[1] = nIn>>16 & 0xff;
7879
pOut[2] = nIn>>8 & 0xff;
7980
pOut[3] = nIn & 0xff;
80
- compress(&pOut[4], &nOut, pIn, nIn);
81
- sqlite3_result_blob(context, pOut, nOut+4, sqlite3_free);
81
+ rc = compress(&pOut[4], &nOut, pIn, nIn);
82
+ if( rc==Z_OK ){
83
+ sqlite3_result_blob(context, pOut, nOut+4, sqlite3_free);
84
+ }else{
85
+ sqlite3_free(pOut);
86
+ sqlite3_result_error(context, "input cannot be zlib compressed", -1);
87
+ }
8288
}
8389
8490
/*
8591
** Implementation of the "decompress(X)" SQL function. The argument X
8692
** is a blob which was obtained from compress(Y). The output will be
@@ -103,10 +109,11 @@
103109
pOut = sqlite3_malloc( nOut+1 );
104110
rc = uncompress(pOut, &nOut, &pIn[4], nIn-4);
105111
if( rc==Z_OK ){
106112
sqlite3_result_blob(context, pOut, nOut, sqlite3_free);
107113
}else{
114
+ sqlite3_free(pOut);
108115
sqlite3_result_error(context, "input is not zlib compressed", -1);
109116
}
110117
}
111118
112119
/*
113120
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -66,21 +66,27 @@
66 ){
67 const unsigned char *pIn;
68 unsigned char *pOut;
69 unsigned int nIn;
70 unsigned long int nOut;
 
71
72 pIn = sqlite3_value_blob(argv[0]);
73 nIn = sqlite3_value_bytes(argv[0]);
74 nOut = 13 + nIn + (nIn+999)/1000;
75 pOut = sqlite3_malloc( nOut+4 );
76 pOut[0] = nIn>>24 & 0xff;
77 pOut[1] = nIn>>16 & 0xff;
78 pOut[2] = nIn>>8 & 0xff;
79 pOut[3] = nIn & 0xff;
80 compress(&pOut[4], &nOut, pIn, nIn);
81 sqlite3_result_blob(context, pOut, nOut+4, sqlite3_free);
 
 
 
 
 
82 }
83
84 /*
85 ** Implementation of the "decompress(X)" SQL function. The argument X
86 ** is a blob which was obtained from compress(Y). The output will be
@@ -103,10 +109,11 @@
103 pOut = sqlite3_malloc( nOut+1 );
104 rc = uncompress(pOut, &nOut, &pIn[4], nIn-4);
105 if( rc==Z_OK ){
106 sqlite3_result_blob(context, pOut, nOut, sqlite3_free);
107 }else{
 
108 sqlite3_result_error(context, "input is not zlib compressed", -1);
109 }
110 }
111
112 /*
113
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -66,21 +66,27 @@
66 ){
67 const unsigned char *pIn;
68 unsigned char *pOut;
69 unsigned int nIn;
70 unsigned long int nOut;
71 int rc;
72
73 pIn = sqlite3_value_blob(argv[0]);
74 nIn = sqlite3_value_bytes(argv[0]);
75 nOut = 13 + nIn + (nIn+999)/1000;
76 pOut = sqlite3_malloc( nOut+4 );
77 pOut[0] = nIn>>24 & 0xff;
78 pOut[1] = nIn>>16 & 0xff;
79 pOut[2] = nIn>>8 & 0xff;
80 pOut[3] = nIn & 0xff;
81 rc = compress(&pOut[4], &nOut, pIn, nIn);
82 if( rc==Z_OK ){
83 sqlite3_result_blob(context, pOut, nOut+4, sqlite3_free);
84 }else{
85 sqlite3_free(pOut);
86 sqlite3_result_error(context, "input cannot be zlib compressed", -1);
87 }
88 }
89
90 /*
91 ** Implementation of the "decompress(X)" SQL function. The argument X
92 ** is a blob which was obtained from compress(Y). The output will be
@@ -103,10 +109,11 @@
109 pOut = sqlite3_malloc( nOut+1 );
110 rc = uncompress(pOut, &nOut, &pIn[4], nIn-4);
111 if( rc==Z_OK ){
112 sqlite3_result_blob(context, pOut, nOut, sqlite3_free);
113 }else{
114 sqlite3_free(pOut);
115 sqlite3_result_error(context, "input is not zlib compressed", -1);
116 }
117 }
118
119 /*
120

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button