--- Chad Simmons <[EMAIL PROTECTED]> wrote:
>
> --- Saereth Lightsblade <[EMAIL PROTECTED]> wrote:
> >
> > Greetings list,
> > I'm having a problem with a function I wrote. What it
>
> [SNIP]
>
> >
> > void remove_from_roster(CHAR_DATA *ch)
> > {
> > char buf[1024];
> > FILE *fp;
> > char *string;
> > char MyArray[1024][1024];
> > int i;
> >
> > string = "End";
> > sprintf(buf, "%s%s.dat", DATA_DIR,FILENAME);
> >
> > if ( !( fp = fopen(buf, "w+" ) ) )
> > {
> > bug( "Could not open data file.", 0 );
> > return;
> > }
> > i = 0;
> > for ( ; ; )
> > {
> > if (feof(fp))
> > string = "End";
> > else
> > fgets(string,MSL,fp);
> >
> > if (string == NULL)
> > break;
> >
> > if (!str_cmp(string,"End"))
> > break;
> >
> > strcpy(MyArray[i],string);
>
> [SNIP]
>
>
> Lemme guess. The compiler was giving a warning about string might be used
> uninitialized in this function.. So to fix it you initialized string with the
>
> line:
>
> string = "End";
>
> The problems with this is that you now have the string pointer pointing to a
> const array 4 chars long. When you try to use this pointer in this statement:
>
> fgets(string,MSL,fp);
>
> You're saying that string is a pointer to an array with MSL number of
> characters.. When you try to read more into less space you get a buffer
> overrun, and Bad Things(tm) happen
Whoops.. I forgot to give you the solution.. ;p
Change:
char * string;
To:
char string[MSL];
This won't solve the problem of your file being more than 1024 lines long... Or
the problem of string being greater than 1024 bytes long. But it should stop
crashing right away.
~Kender
=====
-----BEGIN GEEK CODE BLOCK-----
Version 3.1
GCS/L/C/O d-(+) s++:+ a-- C+++$>++++ UBLS++++$
P+++(--)$ L++>+++ E--- W+>++$ N !o K? w(--) !O
M- !V PS+ PE(++) Y+ PGP->+ t- 5 X+() R(+) tv+@
b++(+++) !DI+++ D G(-) e>+++$ h---() r+++ y+++
------END GEEK CODE BLOCK------
__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com