Mercurial > hg > audiodb
changeset 472:0f96ad351990 api-inversion
Clean up some compiler warnings.
Yes, we must check return values from read(2). Make
audiodb_track_id_datum() be more paranoid, and audiodb_free_datum()
defend against potential double-frees as well (even though we're not
exposing that issue at this point).
author | mas01cr |
---|---|
date | Tue, 06 Jan 2009 15:25:39 +0000 |
parents | c26c5b7ef0d2 |
children | b2fd8113d8bc |
files | insert.cpp query.cpp |
diffstat | 2 files changed, 14 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/insert.cpp Wed Dec 31 16:52:34 2008 +0000 +++ b/insert.cpp Tue Jan 06 15:25:39 2009 +0000 @@ -276,12 +276,15 @@ int audiodb_free_datum(adb_datum_t *datum) { if(datum->data) { free(datum->data); + datum->data = NULL; } if(datum->power) { free(datum->power); + datum->power = NULL; } if(datum->times) { free(datum->times); + datum->times = NULL; } return 0; }
--- a/query.cpp Wed Dec 31 16:52:34 2008 +0000 +++ b/query.cpp Tue Jan 06 15:25:39 2009 +0000 @@ -203,20 +203,19 @@ adb_reference_t reference = {0}; char features[MAXSTR], power[MAXSTR], times[MAXSTR]; lseek(adb->fd, adb->header->dataOffset + track_id * O2_FILETABLE_ENTRY_SIZE, SEEK_SET); - /* FIXME: learn not to worry and love the bomb^Wbuffer overflow */ - read(adb->fd, features, MAXSTR); + read_or_goto_error(adb->fd, features, MAXSTR); reference.features = features; if(adb->header->flags & O2_FLAG_POWER) { lseek(adb->fd, adb->header->powerTableOffset + track_id * O2_FILETABLE_ENTRY_SIZE, SEEK_SET); - read(adb->fd, power, MAXSTR); + read_or_goto_error(adb->fd, power, MAXSTR); reference.power = power; } if(adb->header->flags & O2_FLAG_TIMES) { lseek(adb->fd, adb->header->timesTableOffset + track_id * O2_FILETABLE_ENTRY_SIZE, SEEK_SET); - read(adb->fd, times, MAXSTR); + read_or_goto_error(adb->fd, times, MAXSTR); reference.times = times; } - audiodb_insert_create_datum(&reference, d); + return audiodb_insert_create_datum(&reference, d); } else { /* initialize from sources of data that we already have */ d->nvectors = (*adb->track_lengths)[track_id]; @@ -225,19 +224,22 @@ /* read out stuff from the database tables */ d->data = (double *) malloc(d->nvectors * d->dim * sizeof(double)); lseek(adb->fd, adb->header->dataOffset + track_offset, SEEK_SET); - read(adb->fd, d->data, d->nvectors * d->dim * sizeof(double)); + read_or_goto_error(adb->fd, d->data, d->nvectors * d->dim * sizeof(double)); if(adb->header->flags & O2_FLAG_POWER) { d->power = (double *) malloc(d->nvectors * sizeof(double)); lseek(adb->fd, adb->header->powerTableOffset + track_offset / d->dim, SEEK_SET); - read(adb->fd, d->power, d->nvectors * sizeof(double)); + read_or_goto_error(adb->fd, d->power, d->nvectors * sizeof(double)); } if(adb->header->flags & O2_FLAG_TIMES) { d->times = (double *) malloc(2 * d->nvectors * sizeof(double)); lseek(adb->fd, adb->header->timesTableOffset + track_offset / d->dim, SEEK_SET); - read(adb->fd, d->times, 2 * d->nvectors * sizeof(double)); + read_or_goto_error(adb->fd, d->times, 2 * d->nvectors * sizeof(double)); } + return 0; } - return 0; + error: + audiodb_free_datum(d); + return 1; } int audiodb_datum_qpointers(adb_datum_t *d, uint32_t sequence_length, double **vector_data, double **vector, adb_qpointers_internal_t *qpointers) {