FS#13497: '0' is sometimes a valid track number

Most notably for CD rips that use a track number of 0 for
the leadin.

Therefore change our "invalid track number" canary to -1 instead
of 0.  Additionally don't try to parse an empty string.

In the process, get rid of redudant 'discnum = 0' as well.

NOTE: While not strictly necessary, we recommend rebuilding the
      database to ensure files without track numbers are
      updated with the new canary.

Change-Id: I543f98ca49cec7b5eeffa7c14c1eca57171f345a
This commit is contained in:
Solomon Peachy 2025-11-19 21:03:34 -05:00
parent 481cc70fe0
commit e94a96cdcf
15 changed files with 104 additions and 89 deletions

View file

@ -263,7 +263,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
case SKIN_TOKEN_METADATA_TRACK_NUMBER:
if (id3->track_string)
return id3->track_string;
if (id3->tracknum) {
if (id3->tracknum >= 0) {
itoa_buf(buf, buf_size, id3->tracknum);
return buf;
}

View file

@ -173,7 +173,7 @@ void finalize_id3(struct mp3entry *id3)
id3->codectype = mul_id3.codectype;
id3->vbr = mul_id3.vbr;
id3->discnum = 0;
id3->tracknum = 0;
id3->tracknum = -1;
id3->track_level = 0;
id3->album_level = 0;
}

View file

@ -571,7 +571,7 @@ static const char * id3_get_or_speak_info(int selected_item, void* data,
if(say_it)
say_number_and_spell(val, true);
}
else if (id3->tracknum)
else if (id3->tracknum >= 0)
{
itoa_buf(buffer, buffer_len, id3->tracknum);
val = buffer;

View file

@ -2322,7 +2322,7 @@ static void NO_INLINE add_tagcache(char *path, unsigned long mtime)
logf("-> %s", path);
if (id3.tracknum <= 0) /* Track number missing? */
if (id3.tracknum < 0) /* Track number missing? */
{
id3.tracknum = -1;
}

View file

@ -70,8 +70,6 @@ bool get_aac_metadata(int fd, struct mp3entry *entry)
unsigned char buf[5];
entry->title = NULL;
entry->tracknum = 0;
entry->discnum = 0;
entry->id3v1len = 0;
entry->id3v2len = getid3v2len(fd);
entry->first_frame_offset = entry->id3v2len;

View file

@ -36,8 +36,6 @@
static void read_id3_tags(int fd, struct mp3entry* id3)
{
id3->tracknum = 0;
id3->discnum = 0;
setid3v2title(fd, id3);
}

View file

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>
#include <errno.h>
#include "platform.h"
#include "metadata.h"
@ -473,7 +474,12 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
if (type == 0) {
id3->track_string = id3buf;
asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
id3->tracknum = atoi(id3->track_string);
if (strlen(id3->track_string)) {
char *p = NULL;
int tracknum = strtol(id3->track_string, &p, 0);
if (!(tracknum == 0 && (errno || *p)))
id3->tracknum = tracknum;
}
} else if ((type >=2) && (type <= 5)) {
id3->tracknum = asf_intdecode(fd, type, length);
} else {

View file

@ -243,7 +243,12 @@ static int skip_unsynched(int fd, int len)
/* parse numeric value from string */
static int parsetracknum( struct mp3entry* entry, char* tag, int bufferpos )
{
entry->tracknum = atoi( tag );
if (strlen(tag)) {
char *p = NULL;
int tracknum = strtol(tag, &p, 0);
if (!(tracknum == 0 && (errno || *p)))
entry->tracknum = tracknum;
}
return bufferpos;
}
@ -826,7 +831,7 @@ void setid3v2title(int fd, struct mp3entry *entry)
return;
}
entry->id3version = version;
entry->tracknum = entry->year = entry->discnum = 0;
entry->year = entry->discnum = 0;
entry->title = entry->artist = entry->album = NULL; /* FIXME incomplete */
global_flags = header[5];

View file

@ -452,6 +452,8 @@ bool get_metadata_ex(struct mp3entry* id3, int fd, const char* trackname, int fl
id3->has_embedded_cuesheet = false;
id3->embedded_cuesheet.pos = 0;
id3->tracknum = -1;
entry = &audio_formats[id3->codectype];
/* Load codec specific track tag information and confirm the codec type. */

View file

@ -23,6 +23,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>
#include <errno.h>
#include "platform.h"
#include "metadata.h"
@ -315,7 +316,12 @@ long parse_tag(const char* name, char* value, struct mp3entry* id3,
if (((item == eTRACK && (type == TAGTYPE_APE)))
|| (item == eTRACKNUMBER && (type == TAGTYPE_VORBIS)))
{
id3->tracknum = atoi(value);
if (strlen(value)) {
char *p = NULL;
int tracknum = strtol(value, &p, 0);
if (!(tracknum == 0 && (errno || *p)))
id3->tracknum = tracknum;
}
p = &(id3->track_string);
}
else if (item == eDISCNUMBER || item == eDISC)

View file

@ -166,8 +166,6 @@ bool get_mp3_metadata(int fd, struct mp3entry *entry)
entry->filesize = filesize(fd);
entry->id3v1len = getid3v1len(fd);
entry->id3v2len = getid3v2len(fd);
entry->tracknum = 0;
entry->discnum = 0;
if (entry->id3v2len)
setid3v2title(fd, entry);

View file

@ -481,10 +481,14 @@ static bool read_mp4_tags(int fd, struct mp3entry* id3,
read_mp4_tag_i_from_n(fd, &id3->discnum, &id3->disc_string, size, &buffer_left, &buffer);
break;
case MP4_trkn:
read_mp4_tag_i_from_n(fd, &id3->tracknum, &id3->track_string, size, &buffer_left, &buffer);
case MP4_trkn: {
char *p = NULL;
int tracknum = 0;
read_mp4_tag_i_from_n(fd, &tracknum, &id3->track_string, size, &buffer_left, &buffer);
if (!(tracknum == 0 && (errno || *p)))
id3->tracknum = tracknum;
break;
}
#ifdef HAVE_ALBUMART
case MP4_covr:
{

View file

@ -56,8 +56,6 @@ static void read_id3_tags(int fd, struct mp3entry* id3)
id3->title = NULL;
id3->filesize = filesize(fd);
id3->id3v2len = getid3v2len(fd);
id3->tracknum = 0;
id3->discnum = 0;
id3->vbr = false; /* All TTA files are CBR */
/* first get id3v2 tags. if no id3v2 tags ware found, get id3v1 tags */

View file

@ -732,7 +732,7 @@ static void print_mp3entry(const struct mp3entry *id3, FILE *f)
if (id3->album) fprintf(f, "Album: %s\n", id3->album);
if (id3->genre_string) fprintf(f, "Genre: %s\n", id3->genre_string);
if (id3->disc_string || id3->discnum) fprintf(f, "Disc: %s (%d)\n", id3->disc_string, id3->discnum);
if (id3->track_string || id3->tracknum) fprintf(f, "Track: %s (%d)\n", id3->track_string, id3->tracknum);
if (id3->track_string || id3->tracknum >= 0) fprintf(f, "Track: %s (%d)\n", id3->track_string, id3->tracknum);
if (id3->year_string || id3->year) fprintf(f, "Year: %s (%d)\n", id3->year_string, id3->year);
if (id3->composer) fprintf(f, "Composer: %s\n", id3->composer);
if (id3->comment) fprintf(f, "Comment: %s\n", id3->comment);