Skip to content

Commit 7b54c31

Browse files
committed
fix and expand sqlcipher_mem_realloc implementation
1 parent 5db049f commit 7b54c31

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/crypto_impl.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,24 @@ static void sqlcipher_mem_free(void *p) {
9696
}
9797
static void *sqlcipher_mem_realloc(void *p, int n) {
9898
void *new = NULL;
99+
int orig_sz = 0;
99100
if(mem_security_on) {
100-
new = sqlcipher_mem_malloc(n);
101-
if(new == NULL) {
101+
orig_sz = sqlcipher_mem_size(p);
102+
if (n==0) {
103+
sqlcipher_mem_free(p);
102104
return NULL;
105+
} else if (!p) {
106+
return sqlcipher_mem_malloc(n);
107+
} else if(n <= orig_sz) {
108+
return p;
109+
} else {
110+
new = sqlcipher_mem_malloc(n);
111+
if(new) {
112+
memcpy(new, p, orig_sz);
113+
sqlcipher_mem_free(p);
114+
}
115+
return new;
103116
}
104-
memcpy(new, p, n);
105-
sqlcipher_mem_free(p);
106-
return new;
107117
} else {
108118
return default_mem_methods.xRealloc(p, n);
109119
}

0 commit comments

Comments
 (0)