Skip to content

Commit

Permalink
cron: handle ENOMEM without asserting
Browse files Browse the repository at this point in the history
Problem: cronodate_create() calls xzmalloc() which asserts on
ENOMEM, but cronodate is used in a broker module, so asserting is
inappropriate.

Have cronodate_create() call cmalloc() and return NULL on failure.
Ensure the return value is checked all along the call path from the
cron broker module:

cron_entry_create()
 -> cron_datetime_create()
  -> datetime_entry_from_json()
   -> datetime_entry_create()
    -> cronodate_create()
  • Loading branch information
garlick committed Oct 23, 2023
1 parent e90547f commit 14e94c2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/common/libutil/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ test_cronodate_t_LDADD = \
$(builddir)/cronodate.lo \
$(top_builddir)/src/common/libidset/libidset.la \
$(builddir)/veb.lo \
$(builddir)/xzmalloc.lo \
$(top_builddir)/src/common/libtap/libtap.la

test_wallclock_t_SOURCES = test/wallclock.c
Expand Down
7 changes: 4 additions & 3 deletions src/common/libutil/cronodate.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "src/common/libczmqcontainers/czmq_containers.h"
#include "src/common/libidset/idset.h"
#include "src/common/libutil/xzmalloc.h"
#include "ccan/str/str.h"

#include "cronodate.h"
Expand Down Expand Up @@ -178,9 +177,11 @@ void cronodate_destroy (cronodate_t *d)
cronodate_t * cronodate_create ()
{
int i;
cronodate_t *d = xzmalloc (sizeof (*d));
cronodate_t *d;

if (!(d = calloc (1, sizeof (*d))))
return NULL;

memset (d, 0, sizeof (*d));
for (i = 0; i < TM_MAX_ITEM; i++) {
struct idset *n = idset_create (tm_unit_max (i) + 1,
IDSET_FLAG_AUTOGROW);
Expand Down
9 changes: 8 additions & 1 deletion src/modules/cron/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <flux/core.h>

#include "src/common/libutil/cronodate.h"
#include "src/common/libutil/errno_safe.h"

#include "entry.h"

Expand All @@ -38,7 +39,10 @@ struct datetime_entry * datetime_entry_create ()
{
struct datetime_entry *dt = calloc (1, sizeof (*dt));
if (dt) {
dt->d = cronodate_create ();
if (!(dt->d = cronodate_create ())) {
ERRNO_SAFE_WRAP (free, dt);
return NULL;
}
/* Fill cronodate set initially. The cronodate object will
* be refined when json arguments from user are processed
*/
Expand All @@ -52,6 +56,9 @@ static struct datetime_entry * datetime_entry_from_json (json_t *o)
int i, rc = 0;
struct datetime_entry *dt = datetime_entry_create ();

if (!dt)
return NULL;

for (i = 0; i < TM_MAX_ITEM; i++) {
json_t *val;
/* Time unit members of the json arguments are optional.
Expand Down

0 comments on commit 14e94c2

Please sign in to comment.