Segmentation error on server but not local machine

As indicated in the title, the program runs on my local computer (ubuntu 9.10), but not on the server (linux). This is a grid of godaddy hosting packages.

Please, help..

Here is the code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    long offset;
    FILE *io;
    unsigned char found;
    unsigned long loc;

    if (argc != 2)
    {
        printf("syntax: find 0000000\n");
        return 255;
    }

    offset = atol(argv[1]) * (sizeof(unsigned char)+sizeof(unsigned long));

    io = fopen("index.dat","rb");
    fseek(io,offset,SEEK_SET);
    fread(&found,sizeof(unsigned char),1,io);
    fread(&loc,sizeof(unsigned long),1,io);

    if (found == 1)
        printf("%d\n",loc);
    else
        printf("-1\n");

    fclose(io);

    return 0;
}

EDIT: This is not my program. I would like to know enough C to fix this, but I'm in the deadline. This program is designed to find the first occurrence of a 7-digit number in a PI sequence, index.dat contains a huge array of number => position.

http://jclement.ca/fun/pi/search.cgi

EDIT 2: I used updated code with test for null pointer, still getting the same results. The program works fine on my local computer, this error only occurs on the server.

+3
11

, fopen - , , . ( segfault, NULL .)

+5

, unsigned long .

?

#include <stdio.h>

int main(void)
{
    printf("%zu\n", sizeof(unsigned long));
    return 0;
}

gcc -std=c99 file.c. , unsigned long uint32_t #include <inttypes.h> :

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdint.h>

int main(int argc, char **argv)

{
    long offset;
    FILE *io;
    unsigned char found;
    uint32_t loc;

    if (argc != 2)
    {
        printf("syntax: find 0000000\n");
        return 255;
    }
    /* sizeof(unsigned char) is 1, and I am assuming you wanted
       sizeof(unsigned long) to be 4.  But see below. */
    offset = strtol(argv[1], NULL, 0) * (1+4);

    if ((io = fopen("index.dat", "rb")) == NULL) {
        fprintf(stderr, "Cannot open file\n");
        return EXIT_FAILURE;
    }
    if (fseek(io, offset, SEEK_SET) == -1) {
        fprintf(stderr, "Error seeking\n");
        perror(NULL);
        return EXIT_FAILURE;
    }
    if (fread(&found, 1, 1, io) != 1) {
        fprintf(stderr, "Error in first fread\n");
        return EXIT_FAILURE;
    }
    /* using sizeof loc makes sure that the correct size if always used,
       irrespective of the type of loc */
    if (fread(&loc, sizeof loc, 1, io) != 1) {
        fprintf(stderr, "Error in second fread\n");
        return EXIT_FAILURE;
    }
    if (found == 1)
        printf("%" PRIu32 "\n", loc);
    else
        printf("-1\n");
    fclose(io);

    return 0;
}

, " " 4 unsigned long. , 4 unsigned long .

, : , ..

+2

godaddy , FTP- ASCII, . .bin .

+2

, , , , io NULL.

: - , ( ) , .

+1

, sizeof(long) 8 (- , 64- ), 4 ( 32- ). , 2 . , - uint32_t ..

+1

-, , .

-, , .

0

, "index.dat" , . "rb" io fopen, .

0

gdb ? , ( -g gcc) gdb. , . ( < > ):

# gdb <program>

(gdb) set args <arg>
(gdb) run

gdb segfault , .

0

, . , , .

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    long offset;
    FILE *io;
    unsigned char found;
    unsigned long loc;

    if (argc != 2)
    {
        printf("syntax: find 0000000\n");
        return 255;
    }

    offset = atol(argv[1]) * (sizeof(unsigned char)+sizeof(unsigned long));

    io = fopen("index.dat","rb");
if (io==NULL) {fputs ("File error",stdout); exit (1);}
fseek (io , 0 , SEEK_END);
long fileSize = ftell (io);
long offsetEnd = offset+sizeof(unsigned char)+sizeof(unsigned long);
printf("file size: %d\nseek: %d\nseekEnd: %d\n",fileSize,offset,offsetEnd);

if (offsetEnd> fileSize) {fputs ("Reading outside of file...",stdout); exit (1);}

    fseek(io,offset,SEEK_SET);
    fread(&found,sizeof(unsigned char),1,io);
    fread(&loc,sizeof(unsigned long),1,io);

    if (found == 1)
        printf("%d\n",loc);
    else
        printf("-1\n");

    fclose(io);

    return 0;
}
0

:

fseek(io,offset,SEEK_SET);
printf("fseek worked\n");
fread(&found,sizeof(unsigned char),1,io);
printf("fread 1 worked\n");
fread(&loc,sizeof(unsigned long),1,io);
printf("fread 2 worked\n");

, . , .

Edit: When I say that the function call "worked", I mean "did not call segfault". Ideally, you would like to check every call on fseekand freadto make sure that they did not encounter any errors, but you mentioned that you have a deadline, so this is just an incorrect error tracing, track segfault.

-1
source

Maybe you should try to allocate some memory for the found and loc variables?

-3
source

Source: https://habr.com/ru/post/1736727/


All Articles