# HG changeset patch # User Matti Hamalainen # Date 1578151649 -7200 # Node ID 82540f8191945573f4b97e88483a96a15983f0c1 # Parent 2ed665b74043d5a567c4ae7cd6be75cb12275f49 Clean up error handling. diff -r 2ed665b74043 -r 82540f819194 sidlib.c --- a/sidlib.c Sat Jan 04 14:34:09 2020 +0200 +++ b/sidlib.c Sat Jan 04 17:27:29 2020 +0200 @@ -14,7 +14,7 @@ if (tmp == NULL) { th_io_error(ctx, THERR_MALLOC, - "Could not allocate %" PRIu_SIZE_T " bytes for a string.\n", + "Could not allocate %" PRIu_SIZE_T " bytes for a string.", len); goto err; } @@ -22,7 +22,7 @@ if (!thfread_str(ctx, tmp, len)) { th_io_error(ctx, THERR_FREAD, - "Could not read %" PRIu_SIZE_T " bytes from file.\n", + "Could not read %" PRIu_SIZE_T " bytes from file.", len); goto err; } @@ -37,19 +37,20 @@ } -static BOOL sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid, +static int sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid, th_md5state_t *state, const BOOL newSLDB) { + int ret = THERR_OK; uint8_t *data = NULL; - BOOL ret = FALSE, first = TRUE; + BOOL first = TRUE; size_t read; if ((data = (uint8_t *) th_malloc(SIDLIB_BUFFER_SIZE)) == NULL) { - th_io_error(ctx, THERR_MALLOC, - "Error allocating temporary data buffer of %d bytes.\n", + ret = th_io_error(ctx, THERR_MALLOC, + "Error allocating temporary data buffer of %d bytes.", SIDLIB_BUFFER_SIZE); - goto error; + goto exit; } psid->dataSize = 0; @@ -63,9 +64,9 @@ { if (read < 4) { - th_io_error(ctx, THERR_FREAD, - "Error reading song data, unexpectedly small file.\n"); - goto error; + ret = th_io_error(ctx, THERR_FREAD, + "Error reading song data, unexpectedly small file."); + goto exit; } // Grab the load address @@ -87,18 +88,16 @@ } } while (read > 0 && !thfeof(ctx)); - ret = TRUE; - -error: +exit: th_free(data); return ret; } -BOOL sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, const BOOL newSLDB) +int sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, const BOOL newSLDB) { + int ret = THERR_OK; th_md5state_t state; - BOOL ret = FALSE; off_t hdrStart, hdrEnd; hdrStart = thftell(ctx); @@ -114,10 +113,10 @@ !thfread_be16(ctx, &psid->startSong) || !thfread_be32(ctx, &psid->speed)) { - th_io_error(ctx, ctx->status, - "Could not read PSID/RSID header from '%s': %s.\n", + ret = th_io_error(ctx, ctx->status, + "Could not read PSID/RSID header from '%s': %s.", ctx->filename, th_error_str(ctx->status)); - goto error; + goto exit; } psid->magic[SIDLIB_PSID_MAGIC_LEN] = 0; @@ -126,10 +125,10 @@ psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' || psid->version < 1 || psid->version > 4) { - th_io_error(ctx, THERR_NOT_SUPPORTED, - "Not a supported PSID or RSID file: %s\n", + ret = th_io_error(ctx, THERR_NOT_SUPPORTED, + "Not a supported PSID or RSID file: '%s'", ctx->filename); - goto error; + goto exit; } psid->isRSID = psid->magic[0] == 'R'; @@ -138,10 +137,10 @@ !sidlib_fread_str(ctx, &psid->sidAuthor, SIDLIB_PSID_STR_LEN) || !sidlib_fread_str(ctx, &psid->sidCopyright, SIDLIB_PSID_STR_LEN)) { - th_io_error(ctx, ctx->status, - "Error reading SID file header from '%s': %s.\n", + ret = th_io_error(ctx, ctx->status, + "Error reading SID file header from '%s': %s.", ctx->filename, th_error_str(ctx->status)); - goto error; + goto exit; } // Check if we need to load PSIDv2NG header ... @@ -154,10 +153,10 @@ !thfread_u8(ctx, &psid->sid2Addr) || !thfread_u8(ctx, &psid->sid3Addr)) { - th_io_error(ctx, ctx->status, - "Error reading PSID/RSID v2+ extra header data from '%s': %s.\n", + ret = th_io_error(ctx, ctx->status, + "Error reading PSID/RSID v2+ extra header data from '%s': %s.", ctx->filename, th_error_str(ctx->status)); - goto error; + goto exit; } } @@ -172,8 +171,8 @@ // We just hash the whole file, so seek back to beginning .. thfseek(ctx, hdrStart, SEEK_SET); - if (!sidlib_read_hash_data(ctx, psid, &state, FALSE)) - goto error; + if ((ret = sidlib_read_hash_data(ctx, psid, &state, FALSE)) != THERR_OK) + goto exit; psid->dataSize -= hdrEnd - hdrStart; } @@ -181,8 +180,8 @@ { // "Old" Songlengths.txt style MD5 hash calculation // We need to separately hash data etc. - if (!sidlib_read_hash_data(ctx, psid, &state, TRUE)) - goto error; + if ((ret = sidlib_read_hash_data(ctx, psid, &state, TRUE)) != THERR_OK) + goto exit; // Append header data to hash th_md5_append_le16(&state, psid->initAddress); @@ -217,22 +216,16 @@ // Calculate the hash th_md5_finish(&state, psid->hash); - ret = TRUE; -error: - // Free buffer +exit: return ret; } -BOOL sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, const BOOL newSLDB) +int sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, const BOOL newSLDB) { if ((*ppsid = th_malloc0(sizeof(SIDLibPSIDHeader))) == NULL) - { - th_io_error(ctx, THERR_MALLOC, - "Error allocating PSID context struct.\n"); - return FALSE; - } + return THERR_MALLOC; (*ppsid)->allocated = TRUE; @@ -338,20 +331,21 @@ // Parse one SLDB definition line, return SLDB node // -SIDLibSLDBNode *sidlib_sldb_parse_entry(th_ioctx *ctx, const char *line) +static int sidlib_sldb_parse_entry(th_ioctx *ctx, SIDLibSLDBNode **pnode, const char *line) { - SIDLibSLDBNode *node = NULL; + int ret = THERR_OK; + SIDLibSLDBNode *node; size_t pos, tmpLen, savePos; BOOL isOK; int i; // Allocate new node - node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode)); - if (node == NULL) + if ((*pnode = node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode))) == NULL) { - th_io_error(ctx, THERR_MALLOC, - "Error allocating new node.\n"); - return NULL; + ret = th_io_error(ctx, THERR_MALLOC, + "Error allocating new SLDB node for SongLengthDB file '%s' line #%d:\n%s", + ctx->filename, ctx->line, line); + goto exit; } // Get hash value @@ -366,9 +360,10 @@ th_findnext(line, &pos); if (line[pos] != '=') { - th_io_error(ctx, THERR_INVALID_DATA, - "'=' expected on column #%d.\n", pos); - goto error; + ret = th_io_error(ctx, THERR_INVALID_DATA, + "'=' expected on column %d of SongLengthDB file '%s' line #%d:\n%s", + pos, ctx->filename, ctx->line, line); + goto exit; } // First playtime is after '=' @@ -389,14 +384,17 @@ // Allocate memory for lengths if (node->nlengths == 0) - goto error; + { + ret = THERR_OK; + goto exit; + } - node->lengths = (int *) th_calloc(node->nlengths, sizeof(int)); - if (node->lengths == NULL) + if ((node->lengths = (int *) th_calloc(node->nlengths, sizeof(int))) == NULL) { - th_io_error(ctx, THERR_MALLOC, - "Could not allocate memory for node.\n"); - goto error; + ret = th_io_error(ctx, THERR_MALLOC, + "Error allocating SLDB length data for SongLengthDB file '%s' line #%d:\n%s", + ctx->filename, ctx->line, line); + goto exit; } // Read lengths in @@ -407,17 +405,18 @@ th_findnext(line, &pos); l = sidlib_sldb_gettime(line, &pos); + if (l >= 0) node->lengths[i] = l; else isOK = FALSE; } - return node; + return ret; -error: +exit: sidlib_sldb_node_free(node); - return NULL; + return ret; } @@ -435,13 +434,15 @@ // int sidlib_sldb_read(th_ioctx *ctx, SIDLibSLDB *dbh) { + int ret = THERR_OK; char *line = NULL; if ((line = th_malloc(SIDLIB_BUFFER_SIZE)) == NULL) { - return th_io_error(ctx, THERR_MALLOC, - "Error allocating temporary data buffer of %d bytes.\n", + ret = th_io_error(ctx, THERR_MALLOC, + "Error allocating temporary data buffer of %d bytes.", SIDLIB_BUFFER_SIZE); + goto exit; } while (thfgets(line, SIDLIB_BUFFER_SIZE, ctx) != NULL) @@ -461,36 +462,33 @@ if (hashLen != TH_MD5HASH_LENGTH_CH) { - th_io_error(ctx, THERR_INVALID_DATA, - "Invalid MD5-hash in SongLengthDB file '%s' line #%d:\n%s\n", + ret = th_io_error(ctx, THERR_INVALID_DATA, + "Invalid MD5-hash in SongLengthDB file '%s' line #%d:\n%s", ctx->filename, ctx->line, line); + goto exit; } else { // Parse and add node to db - if ((tmnode = sidlib_sldb_parse_entry(ctx, line)) != NULL) - { - th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) tmnode); - } - else - { - th_io_error(ctx, THERR_INVALID_DATA, - "Invalid entry in SongLengthDB file '%s' line #%d:\n%s\n", - ctx->filename, ctx->line, line); - } + if ((ret = sidlib_sldb_parse_entry(ctx, &tmnode, line)) != THERR_OK) + goto exit; + + th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) tmnode); } } else if (line[pos] != ';' && line[pos] != '[' && line[pos] != 0) { - th_io_error(ctx, THERR_INVALID_DATA, - "Invalid line in SongLengthDB file '%s' line #%d:\n%s\n", + ret = th_io_error(ctx, THERR_INVALID_DATA, + "Invalid line in SongLengthDB file '%s' line #%d:\n%s", ctx->filename, ctx->line, line); + goto exit; } } +exit: th_free(line); - return THERR_OK; + return ret; }