I have a function which reads headers from mp3 files, and extracts the title, artist, album etc. The code looks like this:
Code:
typedef struct
{
char *title;
char *artist;
char *album;
} media_t;
static int extract_mp3(FILE *fp, media_t *media)
{
char tag[4];
tag[0] = '\0';
fseek(fp, -128, SEEK_END);
fread (tag, 1, 3, fp);
tag[3] = '\0';
if(strcmp((const char *)tag, "TAG") == 0)
{
media->title = NULL;
media->artist = NULL;
media->album = NULL;
media->title = malloc(31);
media->artist = malloc(31);
media->album = malloc(31);
fread(media->title, 1, 30, fp);
fread(media->artist, 1, 30, fp);
fread(media->album, 1, 30, fp);
media->title = trim(media->title,' ');
media->artist = trim(media->artist,' ');
media->album = trim(media->album,' ');
}
return 0;
}
I didn't include the part with the fopen. So here is my problem: If I put a breakpoint after the mallocs, in the debugger everything looks fine, all three variables (title,artist,album) look initialized, containing \0. But if I put a breakpoint after the first fread (fread(media->title, 1, 30, fp) the last variable (album) changes it's value, and the debugger tells me that it's address is out of bounds, and before the last fread the execution breaks with segfault.
Can somebody tell me, what am I doing wrong, and how can the last member change it's value when the execution didn't even get to it.
Thank you.