Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fread on Lion does not read when length > 2G

Tags:

c

osx-lion

fread

Since Macosx Lion fread does not read file with length > 2G (int size, 2'147'483'648 bytes). It worked for years with macosx snow leopard.

I wrote a program to test it :

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

int main(int argc, char *argv[])
{
FILE   *fin = NULL, *fout = NULL;
char   *ptr = NULL;
size_t len;
fpos_t flen;

if (!(fin = fopen(argv[1], "rb")))
{
    printf("The input file: %s could not be opened\n", argv[1]);
    return -1;
}
if ((fout = fopen(argv[2], "rb")))
{
    printf("The output file %s already exist\n", argv[2]);
    fclose(fin);
    return -1;
}
if (!(fout = fopen(argv[2],"wb")))
{
    printf("Cannot write on output file %s\n", argv[2]);
    fclose(fin);
    return -1;
}

fseek(fin, 0, SEEK_END);
fgetpos(fin, &flen);
len = flen;
printf("Input file length : %zd\n", len);
fseek(fin, 0, SEEK_SET);

if (!(ptr = malloc(len))) 
{
    printf("Canot allocate %zd bytes\n", len);
    fclose(fin);
    fclose(fout);
    return -1;
}
if (fread(ptr, sizeof(char), len, fin) != len)
{
    printf("Cannot read file\n");
    fclose(fin);
    fclose(fout);
    free(ptr);
    return -1;
}
fclose(fin);
if (fwrite(ptr, sizeof(char), len, fout) != len) 
{
    printf("Cannot write file\n");
    fclose(fout);
    free(ptr);
    return -1;
}
free(ptr);
fclose(fout);

return 1;
}

just run :

  • ./pgm inputfile outputfile
  • openssl sha inputfile
  • openssl sha outputfile

There is no error. The length of the 2 files are the same. The two fingerprints are not the same. (The pointer is well allocated and write in the outputfile) Its only with fread, not fwrite.

i don't understand the problem.

I just see this program (i don't know if apple use this one on Lion) and r variable is defined as int. http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c

Thanks for answers

like image 524
Stephane Avatar asked Nov 28 '25 22:11

Stephane


1 Answers

Sounds like you're not compiling in 64 bit mode. Look for a command line argument or an option to whatever compiler you're using. To make sure you're compiling in the right mode, printf("%d\n", sizeof(int)); and see if it shows you what you expected.

like image 189
Joel Spolsky Avatar answered Dec 01 '25 13:12

Joel Spolsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!