diff options
author | Eric Wong <normalperson@yhbt.net> | 2013-06-15 11:22:11 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-06-15 11:22:11 +0000 |
commit | 7f3f65bda2bce8d328cd90c4679fb242f0344e02 (patch) | |
tree | e41839bd395ecf80a5501a916a98e0f32fa88bdf | |
parent | 526b74a42392715e72634faf51a5e92314c01c6a (diff) | |
download | cmogstored-7f3f65bda2bce8d328cd90c4679fb242f0344e02.tar.gz |
Both hash_initialize and hash_insert may return NULL to indicate allocation errors. So implement a mog_oom_if_null helper function to destroy the process instead of attempting to continue and dereferencing NULL pointers. This may affect configurations with limited memory and lacking overcommit; but is unlikely to trigger given the small memory footprint of cmogstored.
-rw-r--r-- | alloc.c | 5 | ||||
-rw-r--r-- | cfg.c | 3 | ||||
-rw-r--r-- | cmogstored.h | 1 | ||||
-rw-r--r-- | fsck_queue.c | 1 | ||||
-rw-r--r-- | inherit.c | 3 | ||||
-rw-r--r-- | ioutil.c | 3 | ||||
-rw-r--r-- | mnt.c | 4 | ||||
-rw-r--r-- | process.c | 6 | ||||
-rw-r--r-- | svc.c | 4 | ||||
-rw-r--r-- | svc_dev.c | 5 |
10 files changed, 19 insertions, 16 deletions
@@ -58,6 +58,11 @@ _Noreturn void mog_oom(void) abort(); } +void mog_oom_if_null(const void *ptr) +{ + if (!ptr) + mog_oom(); +} /* * Cache alignment is important for sub-pagesized allocations @@ -53,8 +53,7 @@ static void cfg_atexit(void) __attribute__((constructor)) static void cfg_init(void) { all_cfg = hash_initialize(7, NULL, cfg_hash, cfg_cmp, cfg_free); - if (!all_cfg) - mog_oom(); + mog_oom_if_null(all_cfg); atexit(cfg_atexit); } diff --git a/cmogstored.h b/cmogstored.h index d9e74fc..0b5a7bf 100644 --- a/cmogstored.h +++ b/cmogstored.h @@ -310,6 +310,7 @@ void mog_rbuf_free(struct mog_rbuf *); void mog_rbuf_free_and_null(struct mog_rbuf **); void *mog_fsbuf_get(size_t *size); void mog_alloc_quit(void); +void mog_oom_if_null(const void *); #define die_errno(...) do { \ error(EXIT_FAILURE, errno, __VA_ARGS__); \ diff --git a/fsck_queue.c b/fsck_queue.c index 22f3d38..c99a9ed 100644 --- a/fsck_queue.c +++ b/fsck_queue.c @@ -37,6 +37,7 @@ static void fsck_queue_atexit(void) MOG_NOINLINE static void fsck_queue_once(void) { fsck_queues = hash_initialize(7, NULL, fq_hash, fq_cmp, free); + mog_oom_if_null(fsck_queues); atexit(fsck_queue_atexit); } @@ -131,8 +131,7 @@ void mog_inherit_init(void) return; listeners = hash_initialize(3, NULL, listener_hash, listener_cmp, free); - if (!listeners) - die("failed to initialize inherited listeners hash"); + mog_oom_if_null(listeners); atexit(listeners_cleanup); fds = xstrdup(orig); @@ -39,8 +39,7 @@ __attribute__((destructor)) static void iou_destructor(void) __attribute__((constructor)) static void iou_constructor(void) { dev_iou = hash_initialize(7, NULL, iou_hash, iou_cmp, free); - if (!dev_iou) - mog_oom(); + mog_oom_if_null(dev_iou); } static bool cleanup_begin_i(void *ent, void *unused) @@ -62,8 +62,8 @@ static Hash_table * mnt_new(size_t n) { Hash_table *rv = hash_initialize(n, NULL, me_hash, me_cmp, me_free); - if (!rv) - mog_oom(); + mog_oom_if_null(rv); + return rv; } @@ -38,8 +38,7 @@ void mog_process_init(size_t nr) if (nr < 3) nr = 3; processes = hash_initialize(nr, NULL, process_hash, process_cmp, free); - if (processes == NULL) - mog_oom(); + mog_oom_if_null(processes); } void mog_process_reset(void) @@ -123,8 +122,7 @@ void mog_process_register(pid_t pid, unsigned id) p->pid = pid; p->id = id; - if (hash_insert(processes, p) == NULL) - mog_oom(); + mog_oom_if_null(hash_insert(processes, p)); } /* @@ -50,8 +50,7 @@ static void svc_atexit(void) /* called atexit */ static void svc_once(void) { by_docroot = hash_initialize(7, NULL, svc_hash, svc_cmp, svc_free); - if (!by_docroot) - mog_oom(); + mog_oom_if_null(by_docroot); mog_umask = umask(0); umask(mog_umask); @@ -98,6 +97,7 @@ struct mog_svc * mog_svc_new(const char *docroot) CHECK(int, 0, pthread_mutex_init(&svc->by_mog_devid_lock, NULL)); svc->by_mog_devid = hash_initialize(7, NULL, mog_dev_hash, mog_dev_cmp, free); + mog_oom_if_null(svc->by_mog_devid); switch (hash_insert_if_absent(by_docroot, svc, NULL)) { case 0: @@ -52,6 +52,8 @@ static struct mog_devlist * mog_devlist_new(dev_t st_dev) */ NULL); + mog_oom_if_null(devlist->by_mogdevid); + return devlist; } @@ -90,8 +92,7 @@ static void svc_init_dev_hash(struct mog_svc *svc) svc->by_st_dev = hash_initialize(7, NULL, devlist_hash, devlist_cmp, devlist_free); - if (!svc->by_st_dev) - mog_oom(); + mog_oom_if_null(svc->by_st_dev); } static int svc_scandev(struct mog_svc *svc, size_t *nr, mog_scandev_cb cb) |