Solved I get a bus error during fscanf()

EugenOS

New Member


Messages: 8

Hello,

I have a stable working program which works on Linux (on Thecus NAS). So we plan to port it to a PC with 16 data (and one OS) HDD with FreeBSD 9.3 64bit on board. So first of all I created a VMWare image with FreeBSD, GNOME and Netbeans and made some correction to the code (use a 64-bit target, some other changes to use big 3 GB storages and so on). So it has worked well on the VM. When I try it on the PC with FreeBSD (using GCC 4.8 like in the VM, I also installed only MC, gdb, sudo and fusefs-ntfs. All that was installed on the VM except GNOME and its requirements). But on the PC the program returns an error while configuration reading (when using a debug version it says Bus Error). When I see the ktrace dump I can see that the error occurs during fscanf. But I do not understand what is wrong? The file is present and its content is similar to the one that worked (even the line ends in 0x0A like in the one that worked).

So the last lines of the ktrace dump are:
Code:
   985 hashsrv.fbsd.dbg.ve CALL  open(0x40b288,0<O_RDONLY>,<unused>0x1b6)
   985 hashsrv.fbsd.dbg.ve NAMI  "/usr/local/hashsrv/config"
   985 hashsrv.fbsd.dbg.ve RET   open 4
   985 hashsrv.fbsd.dbg.ve CALL  fstat(0x4,0x7fffffffdc10)
   985 hashsrv.fbsd.dbg.ve RET   fstat 0
   985 hashsrv.fbsd.dbg.ve CALL  read(0x4,0x801865000,0x8000)
   985 hashsrv.fbsd.dbg.ve RET   read 515/0x203
   985 hashsrv.fbsd.dbg.ve NAMI  "hashsrv.fbsd.dbg.ve.core"
The function where the error occurs:
Code:
int loadcfg( char* fname )
{
    char s1[32],s2[5], s3[512],vname[16];
    FILE* f = fopen( fname, "r" );
    if( f == NULL) return -1;
    maxDBFileNameLen = 0;
    dbcount = 8;
    while(EOF != fscanf(f, "%s", s1))   // <---- as i understand the error occurs there
    {      
        if( EOF == fscanf(f, "%s", s2) ){ fclose(f); return -2;}
        if( EOF == fscanf(f, "%s", s3) ){ fclose(f); return -2;}
// bla bla bla....
    }
    fclose(f);
    return 0;
}
On a working system the penultimate line of the ktrace dump has a different value:
Code:
1276 hashsrv.fbsd.dbg.ve RET  read 129/0x81
 

obsigna

Aspiring Daemon

Reaction score: 536
Messages: 925

Most probably, fscanf() crashes when it encounters a character sequence which is longer than 31 chars.

In any case it would be more safe to explicitly pass the buffer width in calls to fscanf(), in your case:
Code:
fscanf(f, "%31s", s1)
fscanf(f, "%4s", s2)
fscanf(f, "%511s", s3)
NOTE, you need to reserve 1 byte for '\0'.
 
OP
OP
E

EugenOS

New Member


Messages: 8

Thank you. Yes, this was a reason of trouble. On the NAS and the VM, the configuration file was created by hand, but on this PC I made a shell script to create it from the content of an attached database HDD.

So I forgot to put spaces between "name", "equation" and "value", so s2 captures not a "=" but something longer.

(It follows because the NAS does not allow to install some additional library, only source code you wrote, so after unsuccessfully trying some "configuration file", libraries created that simple and "stupid-unstable" code because all that is only for internal use.)
 
Last edited by a moderator:
Top