Fossil SCM
plug possible memory leak in compress/decompress SQL functions.
Commit
abef6cf7683f4a0397e60f4a75315e422080b726
Parent
5c40f8565e9a3f9…
1 file changed
+9
-2
+9
-2
| --- src/sqlcmd.c | ||
| +++ src/sqlcmd.c | ||
| @@ -66,21 +66,27 @@ | ||
| 66 | 66 | ){ |
| 67 | 67 | const unsigned char *pIn; |
| 68 | 68 | unsigned char *pOut; |
| 69 | 69 | unsigned int nIn; |
| 70 | 70 | unsigned long int nOut; |
| 71 | + int rc; | |
| 71 | 72 | |
| 72 | 73 | pIn = sqlite3_value_blob(argv[0]); |
| 73 | 74 | nIn = sqlite3_value_bytes(argv[0]); |
| 74 | 75 | nOut = 13 + nIn + (nIn+999)/1000; |
| 75 | 76 | pOut = sqlite3_malloc( nOut+4 ); |
| 76 | 77 | pOut[0] = nIn>>24 & 0xff; |
| 77 | 78 | pOut[1] = nIn>>16 & 0xff; |
| 78 | 79 | pOut[2] = nIn>>8 & 0xff; |
| 79 | 80 | 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 | + } | |
| 82 | 88 | } |
| 83 | 89 | |
| 84 | 90 | /* |
| 85 | 91 | ** Implementation of the "decompress(X)" SQL function. The argument X |
| 86 | 92 | ** is a blob which was obtained from compress(Y). The output will be |
| @@ -103,10 +109,11 @@ | ||
| 103 | 109 | pOut = sqlite3_malloc( nOut+1 ); |
| 104 | 110 | rc = uncompress(pOut, &nOut, &pIn[4], nIn-4); |
| 105 | 111 | if( rc==Z_OK ){ |
| 106 | 112 | sqlite3_result_blob(context, pOut, nOut, sqlite3_free); |
| 107 | 113 | }else{ |
| 114 | + sqlite3_free(pOut); | |
| 108 | 115 | sqlite3_result_error(context, "input is not zlib compressed", -1); |
| 109 | 116 | } |
| 110 | 117 | } |
| 111 | 118 | |
| 112 | 119 | /* |
| 113 | 120 |
| --- 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 |