Skip to content

Commit f0f12f9

Browse files
committed
assertions and compiler warnings
1 parent 6dd9a71 commit f0f12f9

24 files changed

Lines changed: 124 additions & 1159 deletions

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
2019-01-01 Jim Zubov <jz@vesvault.com>
22
* * Usage of RAND_bytes() and OPENSSL_cleanup()
33
credits: https://reddit.com/u/skeeto
4+
2019-01-03 Jim Zubov <jz@vesvault.com>
5+
* * Assertions for malloc() returning NULL
6+
Compiler warnings cleaned up
7+
credits: https://reddit.com/u/hillbull

aclocal.m4

Lines changed: 0 additions & 986 deletions
This file was deleted.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
AC_PREREQ([2.69])
6-
AC_INIT([VES], [0.901b], [bugs@vesvault.com])
6+
AC_INIT([VES], [0.902b], [bugs@vesvault.com])
77
AC_CONFIG_SRCDIR([src/ves-util.c])
88
AC_CONFIG_HEADERS([src/config.h])
99

lib/jVar.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <stdlib.h>
3434
#include <string.h>
3535
#include <stdio.h>
36+
#include <assert.h>
3637
#include "jVar.h"
3738

3839

@@ -41,6 +42,7 @@ void jVar_chkMem(jVar *val, size_t len) {
4142
if (val->memSize < 256) val->memSize = 256;
4243
while (val->memSize < len) val->memSize <<= 1;
4344
val->vBuf = val->vBuf ? realloc(val->vBuf, val->memSize) : malloc(val->memSize);
45+
assert(val->vBuf);
4446
}
4547
}
4648

@@ -98,6 +100,7 @@ jVar *jVar_detach(jVar *val) {
98100
case JVAR_STRING:
99101
case JVAR_JSON: {
100102
jVar *res = malloc(sizeof(jVar));
103+
if (!res) return NULL;
101104
memcpy(res, val, sizeof(jVar));
102105
val->vBuf = NULL;
103106
val->len = 0;
@@ -220,6 +223,7 @@ char *jVar_getString(jVar *val) {
220223
int len = jVar_cpString(val, NULL, -1);
221224
if (len < 0) return NULL;
222225
char *str = malloc(len + 1);
226+
if (!str) return NULL;
223227
str[jVar_cpString(val, str, len)] = 0;
224228
return str;
225229
}
@@ -325,40 +329,45 @@ void jVar_setString0(jVar *val, char *str) {
325329

326330
jVar *jVar_null() {
327331
jVar *val = malloc(offsetof(jVar,vInt));
328-
val->type = JVAR_NULL;
332+
if (val) val->type = JVAR_NULL;
329333
return val;
330334
}
331335

332336
jVar *jVar_bool(jVar_TBool v) {
333337
jVar *val = malloc(offsetof(jVar,vBool) + sizeof(val->vBool));
338+
if (!val) return NULL;
334339
val->type = JVAR_BOOL;
335340
val->vBool = v != 0;
336341
return val;
337342
}
338343

339344
jVar *jVar_int(jVar_TInt v) {
340345
jVar *val = malloc(offsetof(jVar,vInt) + sizeof(val->vInt));
346+
if (!val) return NULL;
341347
val->type = JVAR_INT;
342348
val->vInt = v;
343349
return val;
344350
}
345351

346352
jVar *jVar_float(jVar_TFloat v) {
347353
jVar *val = malloc(offsetof(jVar,vFloat) + sizeof(val->vFloat));
354+
if (!val) return NULL;
348355
val->type = JVAR_FLOAT;
349356
val->vFloat = v;
350357
return val;
351358
}
352359

353360
jVar *jVar_stringl(const char *v, size_t len) {
354361
char *buf = malloc(len + 16);
362+
if (!buf) return NULL;
355363
if (v) memcpy(buf, v, len);
356364
return jVar_stringl0(buf, len, 16);
357365
}
358366

359367
jVar *jVar_stringl0(char *v, size_t len, size_t extra) {
360368
if (!v) return NULL;
361369
jVar *val = malloc(sizeof(jVar));
370+
if (!val) return NULL;
362371
val->type = JVAR_STRING;
363372
val->len = len;
364373
val->vString = v;
@@ -378,6 +387,7 @@ jVar *jVar_string0(char *v) {
378387

379388
jVar *jVar_JSON(const char *json) {
380389
jVar *val = jVar_string(json);
390+
if (!val) return NULL;
381391
val->type = JVAR_JSON;
382392
jVar_chkMem(val, val->len + 1);
383393
val->vString[val->len] = 0;
@@ -386,6 +396,7 @@ jVar *jVar_JSON(const char *json) {
386396

387397
jVar *jVar_array() {
388398
jVar *val = malloc(sizeof(jVar));
399+
if (!val) return NULL;
389400
val->type = JVAR_ARRAY;
390401
val->len = 0;
391402
val->memSize = 0;
@@ -395,6 +406,7 @@ jVar *jVar_array() {
395406

396407
jVar *jVar_object() {
397408
jVar *val = malloc(sizeof(jVar));
409+
if (!val) return NULL;
398410
val->type = JVAR_OBJECT;
399411
val->len = 0;
400412
val->memSize = 0;
@@ -530,6 +542,7 @@ char *jVar_toJSON(jVar *val) {
530542

531543
jVarParser *jVarParser_new(jVarParser *parent) {
532544
jVarParser *p = malloc(sizeof(jVarParser));
545+
if (!p) return NULL;
533546
p->state = JVAR_PARSE_INITIAL;
534547
p->result = NULL;
535548
p->key = NULL;
@@ -645,7 +658,6 @@ jVarParser *jVarParser_proceed(jVarParser *p,jVarParser *child) {
645658
const char *h = p->head;
646659
const char *t = p->tail;
647660
char c;
648-
int expf = 0;
649661
if (p->state == JVAR_PARSE_INITIAL) p->state = JVAR_PARSE_INCOMPLETE;
650662
while (++h < t) {
651663
switch (c = *h) {

lib/libVES.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
libVES_List_STATIC0(libVES_unlockedKeys, &libVES_VaultKey_ListCtlU);
4949

50-
const char *libVES_errorMsgs[11] = {
50+
const char *libVES_errorMsgs[12] = {
5151
NULL,
5252
"Bad parameters",
5353
"Communication with the API server failed",
@@ -59,6 +59,7 @@ const char *libVES_errorMsgs[11] = {
5959
"API server error",
6060
"Unsupported algorithm",
6161
"Incorrect operation",
62+
"Internal assertion failed",
6263
};
6364

6465
const char *libVES_appName = "(unspecified app)";
@@ -77,6 +78,7 @@ libVES *libVES_new(const char *vaultURI) {
7778
libVES *libVES_fromRef(libVES_Ref *ref) {
7879
libVES_init(NULL);
7980
libVES *ves = malloc(sizeof(libVES));
81+
if (!ves) return NULL;
8082
ves->external = ref;
8183
ves->apiUrl = LIBVES_API_URL;
8284
ves->appName = libVES_appName;

lib/libVES.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
* libVES.h libVES: Main header
2929
*
3030
***************************************************************************/
31-
#define LIBVES_VERSION_NUMBER 0x00090001L
32-
#define LIBVES_VERSION_CODE "0.901b"
31+
#define LIBVES_VERSION_NUMBER 0x00090002L
32+
#define LIBVES_VERSION_CODE "0.902b"
3333
#define LIBVES_VERSION_STR "libVES.c " LIBVES_VERSION_CODE " (c) 2018 VESvault Corp"
3434
#define LIBVES_VERSION_SHORT "libVES/" LIBVES_VERSION_CODE
3535

@@ -69,6 +69,7 @@ typedef struct libVES {
6969
#define LIBVES_E_SERVER 8
7070
#define LIBVES_E_UNSUPPORTED 9
7171
#define LIBVES_E_INCORRECT 10
72+
#define LIBVES_E_ASSERT 11
7273

7374
#define LIBVES_O_FILE 0x01
7475
#define LIBVES_O_VKEY 0x02

lib/libVES/CiAlgo_AES.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <openssl/pem.h>
3939
#include <openssl/engine.h>
4040
#include <openssl/crypto.h>
41+
#include <assert.h>
4142
#include "Cipher.h"
4243
#include "CiAlgo_AES.h"
4344
#include "Util.h"
@@ -47,6 +48,7 @@
4748
#define libVES_CiAlgo_AESSETKEY(alg, algname) \
4849
if (key && keylen < sizeof(ci->alg.key)) libVES_throw(ves, LIBVES_E_PARAM, algname " cipher key is too short", NULL); \
4950
ci = malloc(offsetof(libVES_Cipher, alg.end)); \
51+
assert(ci); \
5052
if (key) { \
5153
memcpy(ci->alg.key, key, sizeof(ci->alg.key)); \
5254
memcpy(ci->alg.seed, key + (keylen <= sizeof(ci->alg.key) + sizeof(ci->alg.seed) ? keylen - sizeof(ci->alg.seed) : sizeof(ci->alg.key)), sizeof(ci->alg.seed)); \

lib/libVES/Cipher.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int libVES_Cipher_proceed(libVES_Cipher *ci, int final, const char *srctext, siz
9797
int len = func(ci, final, srctext, srclen, NULL);
9898
if (len < 0) libVES_throw(ci->ves, LIBVES_E_CRYPTO, "Cannot determine the cipher buffer size", -1);
9999
if (!dsttext) return len;
100-
*dsttext = malloc(len);
100+
libVES_assert(ci->ves, (*dsttext = malloc(len)), -1);
101101
}
102102
int res = func(ci, final, srctext, srclen, *dsttext);
103103
if (res < 0) libVES_throw(ci->ves, LIBVES_E_CRYPTO, "Cipher error", -1);
@@ -115,6 +115,7 @@ int libVES_Cipher_encrypt(libVES_Cipher *ci, int final, const char *plaintext, s
115115
libVES_Seek *libVES_Cipher_seek(libVES_Cipher *ci, libVES_Seek *sk) {
116116
if (!sk) {
117117
sk = malloc(sizeof(*sk));
118+
if (!sk) return NULL;
118119
sk->plainPos = sk->cipherPos = sk->cipherFbPos = -1;
119120
sk->cipherFbLen = 0;
120121
sk->cipherFb = NULL;

lib/libVES/File.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
libVES_File *libVES_File_new(libVES_Ref *ref) {
4343
libVES_File *file = malloc(sizeof(libVES_File));
44+
if (!file) return NULL;
4445
file->id = 0;
4546
file->name = file->path = file->mime = NULL;
4647
file->external = ref;
@@ -51,6 +52,7 @@ libVES_File *libVES_File_new(libVES_Ref *ref) {
5152
libVES_File *libVES_File_fromJVar(jVar *data) {
5253
if (!data) return NULL;
5354
libVES_File *file = malloc(sizeof(libVES_File));
55+
if (!file) return NULL;
5456
file->id = jVar_getInt(jVar_get(data, "id"));
5557
file->name = jVar_getString0(jVar_get(data, "name"));
5658
file->path = jVar_getString0(jVar_get(data, "path"));

lib/libVES/KeyAlgo_EVP.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ char *libVES_KeyAlgo_EVP_pub2str(libVES_VaultKey *vkey, void *pkey) {
7777
BIO *mem = BIO_new(BIO_s_mem());
7878
if (PEM_write_bio_PUBKEY(mem, (EVP_PKEY *) pkey) > 0) {
7979
len = BIO_get_mem_data(mem, &buf);
80-
str = malloc(len + 1);
81-
memcpy(str, buf, len);
82-
str[len] = 0;
80+
if ((str = malloc(len + 1))) {
81+
memcpy(str, buf, len);
82+
str[len] = 0;
83+
}
8384
} else str = NULL;
8485
BIO_free(mem);
8586
return str;
@@ -138,9 +139,10 @@ char *libVES_KeyAlgo_EVP_toPEM(libVES_veskey *veskey, struct evp_pkey_st *pkey)
138139
if (PEM_write_bio_PKCS8PrivateKey(mem, pkey, (veskey ? EVP_aes_256_cbc() : NULL), (veskey ? veskey->veskey : NULL), (veskey ? veskey->keylen : 0), NULL, NULL) > 0) {
139140
char *buf;
140141
int len = BIO_get_mem_data(mem, &buf);
141-
res = malloc(len + 1);
142-
memcpy(res, buf, len);
143-
res[len] = 0;
142+
if ((res = malloc(len + 1))) {
143+
memcpy(res, buf, len);
144+
res[len] = 0;
145+
}
144146
} else res = NULL;
145147
BIO_free(mem);
146148
return res;
@@ -165,6 +167,7 @@ libVES_VaultKey *libVES_KeyAlgo_RSA_new(const libVES_KeyAlgo *algo, void *pkey,
165167
return NULL;
166168
}
167169
libVES_VaultKey *vkey = malloc(sizeof(libVES_VaultKey));
170+
if (!vkey) return NULL;
168171
vkey->algo = algo;
169172
vkey->pPriv = pkey;
170173
vkey->pPub = NULL;
@@ -179,6 +182,7 @@ int libVES_KeyAlgo_RSA_decrypt(libVES_VaultKey *vkey, const char *ciphertext, si
179182
if (*ctlen > len) {
180183
*ctlen = len;
181184
d = keybuf = malloc(len);
185+
libVES_assert(vkey->ves, d, -1);
182186
} else {
183187
if (!plaintext) return len;
184188
d = (unsigned char *) plaintext;
@@ -257,6 +261,7 @@ libVES_VaultKey *libVES_KeyAlgo_ECDH_new(const libVES_KeyAlgo *algo, void *pkey,
257261
return NULL;
258262
}
259263
libVES_VaultKey *vkey = malloc(sizeof(libVES_VaultKey));
264+
if (!vkey) return NULL;
260265
vkey->algo = algo;
261266
vkey->pPriv = pkey;
262267
vkey->pPub = NULL;

0 commit comments

Comments
 (0)