Erwin's global board snippet error

Posted by Frobozz on Sun 02 Jan 2005 03:25 PM — 8 posts, 25,465 views.

#0
Alright!

I have had this code up and running for a while now, and I noticed something... And of course I do not know how to fix it!

Under Erwin's Board system there appears to be a point where no new notes can be -saved- on any of the boards over a reboot and Im not sure why this happens. But I did look through my log file and found these two entries:


Sat Jan  1 17:41:38 2005 :: Loading Global Boards
Sat Jan  1 17:41:38 2005 :: [*****] BUG: fread_string: string too long
Sat Jan  1 17:41:38 2005 :: [*****] BUG: Load_notes: bad key word.


I grepped for these phrases and found:

from db.c




        if ( ln >= (MAX_STRING_LENGTH - 1) )
        {
             bug( "fread_string: string too long" );
             *plast = '\0';
             return STRALLOC( buf );
        }


and from board.c in the function load_board



void load_board( GLOBAL_BOARD_DATA *board )
{
    FILE *fp, *fp_archive;
    NOTE_DATA *last_note;
    char filename[200];

    sprintf (filename, "%s%s", NOTE_DIR, board->short_name);

    fp = fopen (filename, "r");

    /* Silently return */
    if (!fp)
        return;

    /* Start note fetching. copy of db.c: load_notes() */

    last_note = NULL;

    for ( ; ; )
    {
        NOTE_DATA *pnote;
        char letter;

        do
        {
            letter = getc( fp );
            if ( feof(fp) )
            {
                fclose( fp );
                return;
            }
        }
        while ( isspace(letter) );
        ungetc( letter, fp );

        CREATE( pnote, NOTE_DATA, sizeof( *pnote ) );

        if ( str_cmp( fread_word( fp ), "sender" ) )
            break;
        pnote->sender     = fread_string( fp );

        if ( str_cmp( fread_word( fp ), "date" ) )
            break;
        pnote->date       = fread_string( fp );

        if ( str_cmp( fread_word( fp ), "stamp" ) )
            break;
        pnote->date_stamp = fread_number( fp );
        if ( str_cmp( fread_word( fp ), "expire" ) )
            break;
        pnote->expire = fread_number( fp );

        if ( str_cmp( fread_word( fp ), "to" ) )
            break;
        pnote->to_list    = fread_string( fp );

        if ( str_cmp( fread_word( fp ), "subject" ) )
            break;
        pnote->subject    = fread_string( fp );

        if ( str_cmp( fread_word( fp ), "text" ) )
            break;
        pnote->text       = fread_string( fp );

        pnote->next = NULL; /* jic */

        /* Should this note be archived right now ? */

        if (pnote->expire < current_time)
        {
            char archive_name[200];

            sprintf (archive_name, "%s%s.old", NOTE_DIR, board->short_name);
            fp_archive = fopen (archive_name, "a");
            if (!fp_archive)
                bug ("Could not open archive boards for writing",0);
            else
            {
                append_note (fp_archive, pnote);
                fclose (fp_archive); /* it might be more efficient to close this later */
            }

            free_global_note (pnote);
            board->changed = TRUE;
            continue;

        }


        if ( board->note_first == NULL )
            board->note_first = pnote;
        else
            last_note->next     = pnote;
       }
    bug( "Load_notes: bad key word.", 0 );
    return; /* just return */
  
}



Now being totally stumped on this, Im not sure why the board system will only load up 8 total posts but save all the rest into the ../notes directory.

Any ideas?
#1
Hmm.

I seemed to get around this problem, temporarily, by increasing MAX_STRING_LENGTH -1 to (MAX_..... *2)-1

But is there a better way to do this?
USA #2
You have the "notes" dir made in the smaug dir right?
#3
Definately do, yep!

That's the confusing part, all of the notes end up saved there but it looks like when they pass MAX_STRING_LENGTH those notes dont get loaded to the game, they stay alone in the ../smaug/notes directory.

For instance before I changed to MAX_STRING_LENGTH * 2 this was the output:

Name        Unread     Total    Description
========    ======     =====    ===========
General     [   0]    [   3]    General discussion
Ideas       [   0]    [   3]    Suggestion for improvement
Announce    [   0]    [   2]    Announcements from Immortals
Bugs        [   0]    [   0]    Typos, bugs, errors
Personal    [   0]    [   0]    Personal messages

But after the change:

Name        Unread     Total    Description
========    ======     =====    ===========
General     [   0]    [  11]    General discussion
Ideas       [   0]    [   7]    Suggestion for improvement
Announce    [   0]    [   4]    Announcements from Immortals
Bugs        [   0]    [   0]    Typos, bugs, errors
Personal    [   0]    [   0]    Personal messages



Thus my confusion!
USA #4
Quote:
MAX_STRING_LENGTH -1 to (MAX_..... *2)-1
You really don't want to do this... it's a safety feature to make sure that the string is of the right length, and if you read more than the buffer can hold you'll wreak havok on your memory.

What might be happening is that you have notes that are simply too long. You can either increase the max length of a note, but that's a pain, or you can more simply break the notes into multiple parts yourself. Is there any chance this is what's happening? Max string length is 2048 characters I believe.
#5
Hmm.

That's a good suggestion but it seems that the length of the note really doesn't matter. Posts with messages as simple as "test" or "testing" were being "unloaded" after a reboot.

Thus my confusion!
USA #6
I think that anything that comes after a bad note is ignored... the server might stop reading the file after it finds a bad keyword.
USA #7
Yep, you've more or less already figured it out. You have a note with a message that's too long. So fread_string gave up trying to look for more. Problem is, the file pointer isn't advanced to where the tilde at the end of that string should be, and when the note reader picks up where it last left off, it didn't find what it though was supposed to be there. So the file stops reading.

MAX_STRING_LENGTH is 4096 in unmodified Smaug, so if you have a message longer than 4KB, that would do it. Should be fairly easy to spot something that big in the notes.