LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2019, 04:18 AM   #1
anon033
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
warning: array index 1028888 is past the end of the array (which contains 1028888


Hello everyone! After a lot of research and testing I have opted to write my own id3 tag editor in C, I have wanted to write this for awhile actually; last night just gave me an excuse to do so. I have actually been up for hours debugging and could use some help. I have fixed a few bugs (mainly pointer stuff), but these I am just stumped on I have done some ddging and stack overflow pages; but honestly feel lost. Right now I am trying to do one thing, print out the id3 tag. I only want to do this one thing and after it works I am moving onto the next step and so on. I am trying my best to code from the heap and keep things dynamic (which I am not doing so well and could use some pointers on). I currently have this code:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        /* name of file */
        char song_file[1028888] = "\0";
        song_file[1028888] = strlcpy(song_file, argv[1], sizeof(song_file));

        typedef struct {
                char TAG[3];
                char title[30];

                char artist[30];
                char album[30];

                char comment[30];
                char date[30];

                char track_number[30];
                char genre[30];

                char album_artist[30];
                char lyrics[30];

                char cover[30];
        }id3;

        id3 *tag = calloc(1, sizeof(id3));

        FILE *song;
        song = fopen(&song_file[30], "r+b");

        /* set postion of mp3 file and read id3 tag */
        fseek(song, -128, SEEK_END);
        fread(&tag->TAG, 1, sizeof(&id3.TAG), song);

        fread(&tag->title, 1, sizeof(&id3.title), song);
        fread(&tag->artist, 1, sizeof(&id3.artist), song);

        fread(&tag->album, 1, sizeof(&id3.album), song);
        fread(&tag->comment, 1, sizeof(&id3.comment), song);

        fread(&tag->date, 1, sizeof(&id3.date), song);
        fread(&tag->track_number, 1, sizeof(&id3.track_number), song);

        fread(&tag->genre, 1, sizeof(&id3.genre), song);
        fread(&tag->album_artist, 1, sizeof(&id3.album_artist), song);

        fread(&tag->lyrics, 1, sizeof(&id3.lyrics), song);
        fread(&tag->cover, 1, sizeof(&id3.cover), song);

}
and when I compile this I get errors which are as follows.

Code:
read.c:9:2: warning: array index 1028888 is past the end of the array (which contains 1028888 elements) [-Warray-bounds]
        song_file[1028888] = strlcpy(song_file, argv[1], sizeof(song_file));
        ^         ~~~~~~~
read.c:8:2: note: array 'song_file' declared here
        char song_file[1028888] = "\0";
        ^
1 warning and 11 errors generated.
I need some help. For

Code:
read.c:9:2: warning: array index 1028888 is past the end of the array (which contains 1028888 elements) [-Warray-bounds]
        song_file[1028888] = strlcpy(song_file, argv[1], sizeof(song_file));
        ^         ~~~~~~~
read.c:8:2: note: array 'song_file' declared here
        char song_file[1028888] = "\0";
        ^
no matter how big I make the array I keep getting this error. I thought initially it was due to all the 30s I I just made it huge for testing, but no luck. I have two questions here, how do I fix this error (why am I getting it) and how would I make this dynamic? All the arrays (especially for the title[] and such) I want to be dynamic IE no cap as those things can get really big in my experience.

solution:

song = fopen(argv[1], "r+b");

Last edited by anon033; 06-20-2019 at 10:03 AM.
 
Old 06-20-2019, 04:55 AM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by FOSSilized_Daemon View Post
no matter how big I make the array I keep getting this error.
Arrays in C are zero based, so an array, declared as containing (i.e.) 100 elements will use member indexes in the range 0 .. 99, you never can have an element with index equal to the array size, the highest element always is 1 less (as the first is the [0] one)
 
2 members found this post helpful.
Old 06-20-2019, 05:38 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
And you are trying to place information at an index which is beyond the boundaries of that array.

Start at location [0].

Further, line 9 is just incorrect, you don't assign the result of a string operation to a single byte in an array.

Where that array is declared, it will use a lot of stack.

Better to allocate memory, or not use that as a function scope variable.
 
1 members found this post helpful.
Old 06-20-2019, 09:36 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
In looking at this on a screen versus on my phone. A far greater shortcut here is to not try to store argv[1] anywhere, but instead to inspect argv[1] to verify that it is a proper path+filename, and then just use argv[1] as the argument for your fopen() call.

How do you *know* that you need to go 30 characters into that string to find what you want? Is that you just truncating away the fully qualify path from it all? You can use the full path, I haven't played with it this much, but I believe you can even use a path like "../../subdir/filename.ext" But I'd have to write some test code.

Either case:[list=1]You selected 1028888. Why? Where'd you get that number, and for a passing argument which is supposed to be a path and name of a file on your system. 128 or 256 characters would seem to be plenty, but then again, my contention is that you do not need the array at all, you can just directly use argv[1]. If the reason you've selected an offset of 30 is because your argv contains, "/path/path/(30 characters worth)/filename, then I suggest just trying to use the path "as is".

And how are you debugging? printf()? GDB? Both?
 
1 members found this post helpful.
Old 06-20-2019, 09:48 AM   #5
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
In looking at this on a screen versus on my phone. A far greater shortcut here is to not try to store argv[1] anywhere, but instead to inspect argv[1] to verify that it is a proper path+filename, and then just use argv[1] as the argument for your fopen() call.

How do you *know* that you need to go 30 characters into that string to find what you want? Is that you just truncating away the fully qualify path from it all? You can use the full path, I haven't played with it this much, but I believe you can even use a path like "../../subdir/filename.ext" But I'd have to write some test code.

Either case:[list=1]You selected 1028888. Why? Where'd you get that number, and for a passing argument which is supposed to be a path and name of a file on your system. 128 or 256 characters would seem to be plenty, but then again, my contention is that you do not need the array at all, you can just directly use argv[1]. If the reason you've selected an offset of 30 is because your argv contains, "/path/path/(30 characters worth)/filename, then I suggest just trying to use the path "as is".

And how are you debugging? printf()? GDB? Both?
The number was just because I didn't know what was causing the issue and because I never called anything using 0 or even 1 I figured I must be passed the array, but your recommendation to remove it all together worked. Thank you so much

Last edited by anon033; 06-20-2019 at 09:54 AM.
 
Old 06-20-2019, 09:58 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You have an off-by-one error. Increase the size of song_file by 1.

Last edited by dugan; 06-20-2019 at 09:59 AM.
 
Old 06-20-2019, 09:59 AM   #7
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by rtmistler View Post
In looking at this on a screen versus on my phone. A far greater shortcut here is to not try to store argv[1] anywhere, but instead to inspect argv[1] to verify that it is a proper path+filename, and then just use argv[1] as the argument for your fopen() call.

How do you *know* that you need to go 30 characters into that string to find what you want? Is that you just truncating away the fully qualify path from it all? You can use the full path, I haven't played with it this much, but I believe you can even use a path like "../../subdir/filename.ext" But I'd have to write some test code.

Either case:[list=1]You selected 1028888. Why? Where'd you get that number, and for a passing argument which is supposed to be a path and name of a file on your system. 128 or 256 characters would seem to be plenty, but then again, my contention is that you do not need the array at all, you can just directly use argv[1]. If the reason you've selected an offset of 30 is because your argv contains, "/path/path/(30 characters worth)/filename, then I suggest just trying to use the path "as is".

And how are you debugging? printf()? GDB? Both?
Yes it works! onless the file has spaces, I'll fix that haha. Thank you again, gotta fix the printing and argv having spaces and then read.c should work!
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line execution error: unexpected EOF while looking for matching `"' bash: -c: line 25: syntax error: unexpected end of file maheshreddy690 Linux - Newbie 1 12-25-2018 01:13 PM
[SOLVED] warning: empty declaration| |error: parameter ‘mount_opts’ is initialized| error: array index in non-array initializer|... BW-userx Programming 5 08-24-2017 04:26 PM
Parse error: syntax error, unexpected $end in /home/a7358914/public_html/index.php on nothing07 Programming 5 06-03-2012 02:11 PM
[SOLVED] gcc Warning: Array index has type 'char' traene Programming 4 04-20-2008 08:43 PM
Multimedia: ID3 "Supper Tagging" for Linux? app to retrieve missing ID3 info? CoolAJ86 Linux - Software 3 10-10-2005 05:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration