Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
update: Consolidate out-of-memory error paths. Minor code cleanups.
Browse files Browse the repository at this point in the history
There are couple of places in update_db() where we can get
memory allocation failures. Consolidate such error handling
in single place and make code more readable.

Check for oom from call to g_strdup_printf() in update_db().

Allocate database directory and update file paths at the
beginning of the update procedure, so oom conditions
catched before we attempt to establish lock or create
update dot file.

Move code checking for valid file descriptor from update_end()
up to the caller.

Signed-off-by: Sergey Popovich <[email protected]>
  • Loading branch information
Sergey Popovich authored and Serhii Popovych committed Jan 5, 2016
1 parent fcbef7f commit 705a2be
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ static inline int update_begin(const char *update_fname)

static inline void update_end(int fd, const char *update_fname, bool ok)
{
if (fd != -1) {
close(fd);
if (ok)
unlink(update_fname);
}
close(fd);
if (ok)
unlink(update_fname);
}

int update_required(const gchar *db_file)
Expand All @@ -147,18 +145,20 @@ bool update_db(bool quiet, const gchar *db_file)
int year;
bool ret = false;
bool db_exist = false;
bool db_locked;
bool db_locked = false;

u_fname = get_db_dot_fname(db_file, UPDATE_DB_FNAME_SUFFIX);
if (!u_fname) {
fputs("update_db(): Out of memory\n", stderr);
goto end_no_lock;
}
if (!u_fname)
goto oom;

db_dir = g_path_get_dirname(db_file);
if (!db_dir)
goto oom;

db_locked = cve_db_write_lock(LOCK_WAIT_SECS);
if (!db_locked) {
fputs("Exiting...\n", stderr);
goto end_no_lock;
goto end;
}

/* Lock aquired, check if database is still needs update */
Expand All @@ -167,12 +167,6 @@ bool update_db(bool quiet, const gchar *db_file)
goto end;
}

db_dir = g_path_get_dirname(db_file);
if (!db_dir) {
fprintf(stderr, "update_db(): Out of memory\n");
goto end;
}

u_handle = update_begin(u_fname);
if (u_handle == -1) {
fprintf(stderr, "Can't create timestamp file %s\n", u_fname);
Expand Down Expand Up @@ -206,7 +200,12 @@ bool update_db(bool quiet, const gchar *db_file)
}

uri = g_strdup_printf("%s/%s", nvd_uri, nvdcve);
if (!uri)
goto oom;

target = g_build_path(G_DIR_SEPARATOR_S, db_dir, nvdcve, NULL);
if (!target)
goto oom;

st = fetch_uri(uri, target, !quiet);
switch (st) {
Expand Down Expand Up @@ -248,10 +247,14 @@ bool update_db(bool quiet, const gchar *db_file)

ret = true;
end:
update_end(u_handle, u_fname, ret);
cve_db_unlock();
end_no_lock:
if (u_handle != -1)
update_end(u_handle, u_fname, ret);
if (db_locked)
cve_db_unlock();
return ret;
oom:
fputs("main(): Out of memory\n", stderr);
goto end;
}

/*
Expand Down

0 comments on commit 705a2be

Please sign in to comment.