annotate audioDB-internals.h @ 411:ad2206c24986 api-inversion

Fix a memory corruption bug. When allocating the adb_t in audiodb_open(), zero the memory; then we're not going to try to free() or delete some arbitrary uninitialized thing if the thing that we're opening turns out not to be an audiodb database.
author mas01cr
date Thu, 11 Dec 2008 08:54:06 +0000
parents d7e590d58c85
children 6e6f4c1cc14d
rev   line source
mas01cr@408 1 typedef struct adb_datum_internal {
mas01cr@408 2 uint32_t nvectors;
mas01cr@408 3 uint32_t dim;
mas01cr@408 4 const char *key;
mas01cr@408 5 void *data;
mas01cr@408 6 void *times;
mas01cr@408 7 void *power;
mas01cr@408 8 } adb_datum_internal_t;
mas01cr@408 9
mas01cr@402 10 struct adb {
mas01cr@402 11 char *path;
mas01cr@402 12 int fd;
mas01cr@402 13 int flags;
mas01cr@402 14 adb_header_t *header;
mas01cr@402 15 std::set<std::string> *keys;
mas01cr@402 16 };
mas01cr@402 17
mas01cr@401 18 /* We could go gcc-specific here and use typeof() instead of passing
mas01cr@401 19 * in an explicit type. Answers on a postcard as to whether that's a
mas01cr@401 20 * good plan or not. */
mas01cr@401 21 #define mmap_or_goto_error(type, var, start, length) \
mas01cr@401 22 { void *tmp = mmap(0, length, PROT_READ, MAP_SHARED, adb->fd, (start)); \
mas01cr@401 23 if(tmp == (void *) -1) { \
mas01cr@401 24 goto error; \
mas01cr@401 25 } \
mas01cr@401 26 var = (type) tmp; \
mas01cr@401 27 }
mas01cr@401 28
mas01cr@401 29 #define maybe_munmap(table, length) \
mas01cr@401 30 { if(table) { \
mas01cr@401 31 munmap(table, length); \
mas01cr@401 32 } \
mas01cr@401 33 }
mas01cr@401 34
mas01cr@410 35 #define write_or_goto_error(fd, buffer, size) \
mas01cr@410 36 { ssize_t tmp = size; \
mas01cr@410 37 if(write(fd, buffer, size) != tmp) { \
mas01cr@410 38 goto error; \
mas01cr@410 39 } \
mas01cr@410 40 }
mas01cr@410 41
mas01cr@410 42 #define read_or_goto_error(fd, buffer, size) \
mas01cr@410 43 { ssize_t tmp = size; \
mas01cr@410 44 if(read(fd, buffer, size) != tmp) { \
mas01cr@410 45 goto error; \
mas01cr@410 46 } \
mas01cr@410 47 }
mas01cr@410 48
mas01cr@401 49 static inline int audiodb_sync_header(adb_t *adb) {
mas01cr@401 50 off_t pos;
mas01cr@401 51 pos = lseek(adb->fd, (off_t) 0, SEEK_CUR);
mas01cr@401 52 if(pos == (off_t) -1) {
mas01cr@401 53 goto error;
mas01cr@401 54 }
mas01cr@401 55 if(lseek(adb->fd, (off_t) 0, SEEK_SET) == (off_t) -1) {
mas01cr@401 56 goto error;
mas01cr@401 57 }
mas01cr@401 58 if(write(adb->fd, adb->header, O2_HEADERSIZE) != O2_HEADERSIZE) {
mas01cr@401 59 goto error;
mas01cr@401 60 }
mas01cr@401 61
mas01cr@401 62 /* can be fsync() if fdatasync() is racily exciting and new */
mas01cr@401 63 fdatasync(adb->fd);
mas01cr@401 64 if(lseek(adb->fd, pos, SEEK_SET) == (off_t) -1) {
mas01cr@401 65 goto error;
mas01cr@401 66 }
mas01cr@401 67 return 0;
mas01cr@401 68
mas01cr@401 69 error:
mas01cr@401 70 return 1;
mas01cr@401 71 }