glibc 检测到错误 - id3Tag
所以,我是 C 新手,我一直在尝试编写这个工具来从命令行编辑音乐文件上的 id3 标签。我一直收到这个错误:
* glibc 检测到双重释放或损坏(顶部):0x0000000000502010 **
从我读到的内容来看,我知道它与释放内存。不过,我只是不太确定从这里该去哪里。无论如何,我的逻辑是,如果存在标签,我会读取该标签,然后根据命令行中指定的字段进行所需的任何更改。这是一直给我带来麻烦的块。感谢您提前提供任何见解!
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码中没有
free
调用,所以我想您很高兴没有双重释放任何东西?因此,我认为你这里有一些堆损坏。也就是说,您向标签写入的数据超出了空间。首先,为了解决@H2CO3 的观点,我不认为这就是问题所在。他提到的问题可能确实存在 - 在这种情况下你会损坏你的数据文件 - 但看起来这并不是程序失败的原因。
我认为您真正的问题是您从未将任何内容分配给
in_file
!我认为您需要:一些其他注释:
strcmp
返回零(false),当字符串匹配时,返回非零(true)...所以所有您的比较似乎已损坏 - 这不应该导致腐败。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: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.