error handling in key areas #102
Costa Tsaousis (ktsaou) committed
Mar 31, 2016 at 20:30 UTC
7e086a7ecced30619a975669ebc006b4afecd7d2
4 files changed
+21
-5
src/avl.c
+8
-2
@@ -17,6 +17,7 @@
17
#include <config.h>
18
#endif
19
#include "avl.h"
20
+#include "log.h"
21
22
/* Private methods */
23
int _avl_removeroot(avl_tree* t);
@@ -390,9 +391,14 @@ int avl_search(avl_tree* t, avl* a, int (*iter)(avl* a), avl** ret) {
391
void avl_init(avl_tree* t, int (*compar)(void* a, void* b)) {
392
t->root = NULL;
393
t->compar = compar;
394
+
395
+ int lock;
396
#ifdef AVL_LOCK_WITH_MUTEX
394
- pthread_mutex_init(&t->mutex, NULL);
397
+ lock = pthread_mutex_init(&t->mutex, NULL);
398
#else
396
- pthread_rwlock_init(&t->rwlock, NULL);
399
+ lock = pthread_rwlock_init(&t->rwlock, NULL);
400
#endif
401
+
402
+ if(lock != 0)
403
+ fatal("Failed to initialize AVL mutex/rwlock, error: %d", lock);
404
}
src/main.c
+2
@@ -460,6 +460,8 @@ int main(int argc, char **argv)
460
461
if(st->enabled) {
462
st->thread = malloc(sizeof(pthread_t));
463
+ if(!st->thread)
464
+ fatal("Cannot allocate pthread_t memory");
465
466
info("Starting thread %s.", st->name);
467
src/plugin_tc.c
+6
-2
@@ -346,11 +346,15 @@ static struct tc_device *tc_device_create(char *id)
346
347
d->classes_index.root = NULL;
348
d->classes_index.compar = tc_class_compare;
349
+
350
+ int lock;
351
#ifdef AVL_LOCK_WITH_MUTEX
350
- pthread_mutex_init(&d->classes_index.mutex, NULL);
352
+ lock = pthread_mutex_init(&d->classes_index.mutex, NULL);
353
#else
352
- pthread_rwlock_init(&d->classes_index.rwlock, NULL);
354
+ lock = pthread_rwlock_init(&d->classes_index.rwlock, NULL);
355
#endif
356
+ if(lock != 0)
357
+ fatal("Failed to initialize plugin_tc mutex/rwlock, return code %d.", lock);
358
359
tc_device_index_add(d);
360
src/url.c
+5
-1
@@ -31,6 +31,9 @@ char *url_encode(char *str) {
31
*buf = malloc(strlen(str) * 3 + 1),
32
*pbuf = buf;
33
34
+ if(!buf)
35
+ fatal("Cannot allocate memory.");
36
+
37
while (*pstr) {
38
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
39
*pbuf++ = *pstr;
@@ -56,7 +59,8 @@ char *url_decode(char *str) {
59
*buf = malloc(strlen(str) + 1),
60
*pbuf = buf;
61
59
- if(!buf) fatal("Cannot allocate memory.");
62
+ if(!buf)
63
+ fatal("Cannot allocate memory.");
64
65
while (*pstr) {
66
if (*pstr == '%') {