annotate status.cpp @ 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 58b88ab69424
children 62a0515f59be
rev   line source
mas01cr@395 1 #include "audioDB.h"
mas01cr@395 2 extern "C" {
mas01cr@395 3 #include "audioDB_API.h"
mas01cr@402 4 #include "audioDB-internals.h"
mas01cr@395 5 }
mas01cr@395 6
mas01cr@395 7 int audiodb_status(adb_t *adb, adb_status_t *status) {
mas01cr@395 8 /* FIXME: it would be nice to be able to test for "is this database
mas01cr@395 9 pointer valid", but at the moment we punt that to memory
mas01cr@395 10 discipline. */
mas01cr@395 11
mas01cr@395 12 unsigned dudCount = 0;
mas01cr@395 13 unsigned nullCount = 0;
mas01cr@395 14
mas01cr@399 15 size_t trackTableLength = ALIGN_PAGE_UP(adb->header->numFiles * O2_TRACKTABLE_ENTRY_SIZE);
mas01cr@395 16 unsigned *trackTable = 0;
mas01cr@395 17 void *tmp = 0;
mas01cr@395 18 if (adb->header->length > 0) {
mas01cr@395 19 tmp = mmap(0, trackTableLength, PROT_READ, MAP_SHARED, adb->fd, adb->header->trackTableOffset);
mas01cr@395 20 if (tmp == (void *) -1) {
mas01cr@395 21 return 1;
mas01cr@395 22 }
mas01cr@395 23 trackTable = (unsigned *) tmp;
mas01cr@395 24 }
mas01cr@395 25
mas01cr@395 26 for(unsigned k = 0; k < adb->header->numFiles; k++) {
mas01cr@395 27 /* FIXME: this bare "16" here reveals a problem (or maybe two).
mas01cr@395 28 * 16 here means the default value of the sequenceLength parameter
mas01cr@395 29 * initializer (both in C++ and corresponding to the "-l" or
mas01cr@395 30 * "--sequencelength" command-line argument).
mas01cr@395 31 *
mas01cr@395 32 * The problem is that the API as currently designed provides no
mas01cr@395 33 * way to pass that information in to this routine; there's no
mas01cr@395 34 * input parameter; nor is there in the SOAP version of this
mas01cr@395 35 * query. However, there /is/ a way to pass that information on
mas01cr@395 36 * the command-line -- though that codepath is completely
mas01cr@395 37 * untested. I can see that it might be useful to provide this
mas01cr@395 38 * information, but at present it's probably completely unused, so
mas01cr@395 39 * the compromise for now is to hardwire the 16.
mas01cr@395 40 */
mas01cr@395 41 if(trackTable[k] < 16) {
mas01cr@395 42 dudCount++;
mas01cr@395 43 if(!trackTable[k]) {
mas01cr@395 44 nullCount++;
mas01cr@395 45 }
mas01cr@395 46 }
mas01cr@395 47 }
mas01cr@395 48
mas01cr@395 49 if(adb->header->length > 0) {
mas01cr@395 50 if(munmap(trackTable, trackTableLength)) {
mas01cr@395 51 return 1;
mas01cr@395 52 }
mas01cr@395 53 }
mas01cr@395 54
mas01cr@395 55 status->numFiles = adb->header->numFiles;
mas01cr@395 56 status->dim = adb->header->dim;
mas01cr@395 57 status->length = adb->header->length;
mas01cr@395 58 status->dudCount = dudCount;
mas01cr@395 59 status->nullCount = nullCount;
mas01cr@395 60 status->flags = adb->header->flags;
mas01cr@395 61 status->data_region_size = adb->header->timesTableOffset - adb->header->dataOffset;
mas01cr@395 62
mas01cr@395 63 return 0;
mas01cr@395 64 }
mas01cr@395 65