Skip to content

Commit

Permalink
Simplify codec_new() with memcpy() & memset()
Browse files Browse the repository at this point in the history
Prevents issues like #26 in the future. Hopefully.
  • Loading branch information
resilar committed Sep 19, 2019
1 parent 4a6f062 commit 36bb594
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions sqleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,35 @@ typedef struct codec {
SQLEET_HAS_SKIP = 0x10,
SQLEET_HAS_KDF = 0x20
} flags;
int error;

const void *zKey;
int nKey;
void *pagebuf;
int pagesize;
int skip;
enum {
SQLEET_KDF_NONE = 0,
SQLEET_KDF_PBKDF2_HMAC_SHA256
} kdf;

Btree *btree;
int error;
const void *zKey;
int nKey;
} Codec;

Codec *codec_new(const void *zKey, int nKey, Codec *from)
{
Codec *codec;
if ((codec = sqlite3_malloc(sizeof(Codec)))) {
if (from) {
memcpy(codec->key, from->key, sizeof(codec->key));
memcpy(codec->salt, from->salt, sizeof(codec->salt));
memcpy(codec->header, from->header, sizeof(codec->header));
codec->reader = (from->reader == from) ? codec : from->reader;
codec->writer = (from->writer == from) ? codec : from->writer;
codec->flags = from->flags;
codec->pagesize = from->pagesize;
codec->skip = from->skip;
codec->kdf = from->kdf;
memcpy(codec, from, sizeof(Codec));
if (codec->reader == from)
codec->reader = codec;
if (codec->writer == from)
codec->writer = codec;
codec->pagebuf = NULL;
codec->btree = NULL;
} else {
memset(codec, 0, sizeof(Codec));
codec->reader = codec->writer = codec;
memset(codec->key, 0, sizeof(codec->key));
memset(codec->salt, 0, sizeof(codec->salt));
codec->flags = 0;
codec->pagesize = 0;
}
codec->pagebuf = NULL;
codec->zKey = zKey;
codec->nKey = nKey;
}
Expand Down Expand Up @@ -420,7 +412,7 @@ static int verify_page1(Pager *pager)

/*
* A hack to control the page size of attached vacuum database.
* Otherwise the resulting database inherits page size from the source database.
* Otherwise the database inherits page size from the source database.
*/
static void size_hook(void *pcodec, int new_pagesize, int reserved)
{
Expand Down

0 comments on commit 36bb594

Please sign in to comment.