0

So, I'm new to C and i've been trying to write this tool to edit the id3 tags on music files from the command line. I've been getting this error:

* glibc detected double free or corruption (top): 0x0000000000502010 **

From what I've read, I know it has something to do with freeing up memory. I'm just not really sure where to go from here, though. Anyway, my logic was that if a tag existed I'd read that tag in, then make whatever changes needed to be made from the fields specified in the command line. Here's the block that's been giving me trouble. Thanks for any insight in advance!

    fopen(argv[1], "rb");
    fseek(in_file, -128, SEEK_END);
    fread(&tagTest, sizeof(struct iD3Tag), 1, in_file);
    fclose(in_file);

    for (x = 2; x < argc-1; x++)
    {            
        if (strcmp(argv[x], "-title"))
            strncpy(tagTest.title, argv[x+1], 30);
        if (strcmp(argv[x], "-artist"))
            strncpy(tagTest.artist, argv[x+1], 30);
        if (strcmp(argv[x], "-album"))
            strncpy(tagTest.album, argv[x+1], 30);
        if (strcmp(argv[x], "-year"))
            strncpy(tagTest.year, argv[x+1], 4);
        if (strcmp(argv[x], "-comment"))
            strncpy(tagTest.comment, argv[x+1], 28);
        if (strcmp(argv[x], "-track"))
            tagTest.track = atoi(argv[x+1]);
    }

    tagTest.seperator = 0;

    fopen(argv[1], "r+b");
    fseek(in_file, -128, SEEK_END);
    fwrite(&tagTest, sizeof(struct iD3Tag), 1, in_file);
    fclose(in_file);
soflaz
  • 5
  • 1
  • 1
    `fread(&tagTest, sizeof(struct iD3Tag), 1, in_file);` is a no-go. Structures have an inner padding between the structure members due to memory align constraints so your tag information will essentially be corrupted... –  Feb 05 '12 at 18:26
  • What's the definition of `struct iD3Tag`? – pmg Feb 05 '12 at 18:29

1 Answers1

1

There are no free calls in this code, so I presume you're happy you're not double-freeing anything? Therefore, I presume you have some heap corruption here. I.e. you're writing more data into the tag than there is space for.

First, to address the point from @H2CO3, I don't believe that's the problem here. It might be that the problem he mentions does exist - in which case you'll corrupt your datafile - but it doesn't look like that's the cause of the program failure.

I think your real problem is that you never assign anything to in_file! I think you need:

in_file = fopen (argv[1], "rb");

Some other comments:

  • strcmp returns zero (false) when a string matches and non-zero (true) when it does not match ... so all your compares appear broken - that shouldn't cause corruption.
  • The loop appears to check the argument values as well as switches - you should skip those.
  • There's no need to open the file twice - you can open it for both read and write at the start.
  • You need to put a lot more error checking in in case the file doesn't exist or isn't what you expect - but then I'm sure you knew that.
ams
  • 24,923
  • 4
  • 54
  • 75
  • Thanks so much! I didn't even realize that about `strcmp` or `fopen` (A testament to how inexperienced i am at C). Just another question, what do you mean exactly when you say that that the loop checks the argument values as well as switches? Also, I made the changes to the if statements but now I'm getting some segmentation fault whenever I run the program on the same file twice. And when I go to view the tag after I've edited it on a second program run, it's not printing the whole thing. – soflaz Feb 06 '12 at 00:20
  • Imagine you have arguments "-title foo -artist bar". Your code first checks to see if "-title" is an option; it is so it does the right thing (or it would if you had strcmp right). Then the next thing it does is check if "foo" is an option, but it really ought to have consumed that already. – ams Feb 06 '12 at 09:15
  • Oh. I understand now. Thank you again. I got everything working the way it should finally haha – soflaz Feb 07 '12 at 07:46