Fread on Lion does not read when length > 2G - c

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

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.

Works fine for me on MacOS X Lion (10.7.2) with XCode 4.2. The executable is a 64-bit program. You should ensure yours is too.
$ make 2gb
/usr/bin/gcc -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
2gb.c:5: warning: unused parameter ‘argc’
$ file 2gb
2gb: Mach-O 64-bit executable x86_64
$ dd if=/dev/zero of=input bs=1m count=3072
./2g3072+0 records in
3072+0 records out
3221225472 bytes transferred in 42.940363 secs (75016261 bytes/sec)
$ ls -l input
./2gb -rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:48 input
$ ./2gb input output
Input file length : 3221225472
$ openssl sha input
SHA(input)= c93bf6713a90e34554311f0a9e43cfd1f153475a
$ openssl sha output
SHA(output)= c93bf6713a90e34554311f0a9e43cfd1f153475a
$ ls -l input output
-rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:48 input
-rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:49 output
$ rm input output
$ /usr/bin/gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
And, when forced into 32-bit compilation:
$ rm 2gb
$ make CC='/usr/bin/gcc -m32' 2gb
/usr/bin/gcc -m32 -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
2gb.c:5: warning: unused parameter ‘argc’
$ dd if=/dev/zero of=input bs=1m count=3072
3072+0 records in
3072+0 records out
3221225472 bytes transferred in 38.326753 secs (84046397 bytes/sec)
$ ./2gb input output
Input file length : 0
$ ls -l input
-rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:57 input
$

printf("%d\n", sizeof(int));
gcc -Wall Typ.c -o Typ
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long
--> 4
Thank you Jonathan.
i am on 10.7.2 and xcode 4.2
gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
i do gcc TestFile.c -o TestFile.o
and even exactly as you did :
/usr/bin/gcc -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
and i add -m64 after
and i have again 2 differents fingerprint.
xcode 3.6.2 was removed using sudo /Library/uninstall-devtools --mode=all
and xcode 4.2 install after.
i run the pgm on another user newly created (cause of $path maybe) and its the same.
i don't understand.
Apple team answer after reporting this bug
Hello Stéphane,
This is a follow up to Bug ID# 10376104. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 6434977. The original bug number being used to track this duplicate issue can be found in the Related Problem section of your bug report's Problem Detail view.
Thank you for submitting this bug report. We truly appreciate your assistance in helping us discover and isolate bugs.
Best Regards,
Developer Bug Reporting Team
Apple Worldwide Developer Relations

Related

fopen not defaulting to current directory

I'm using a new mac with macOS Monterey and the M1 Max chip. I'd like fopen to default to the current directory when no path is given (this is what I'm used to and I think is the norm), but it's defaulting to my user folder. So, for example, when I compile the below code, the resultant app, regardless of where it is, will only open file.txt if that file is in my user folder (I get a segmentation fault if it's anywhere else). Similarly, if I have the code make a new file with FILE* fp = fopen("new file.txt", "w");, the new file will appear in my user folder.
I'm compiling with gcc (that I had downloaded via downloading the newest version of Eclipse) from Terminal with the command:
gcc -std=c99 main.c -o yo
When I build using Eclipse or leave out -std=c99 I have the same issue.
Also, for gcc --version I get:
InstalledDir:
nathanschmidt#nathans-MacBook-Pro ~ % gcc --version Configured with:
--prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.27.3) Target:
arm64-apple-darwin21.3.0 Thread model: posix InstalledDir:
/Library/Developer/CommandLineTools/usr/bin
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE* fp = fopen("file.txt", "r");
printf("%d\n", fp);
int x;
fscanf(fp, "%d", &x);
fclose(fp);
printf("%d\n", x);
return EXIT_SUCCESS;
}

scan-build not working reporting "Removing directory xxx because it contains no report."

simple buggy code file
$ python --version
Python 3.8.5
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang --version
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ make --version
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
hello.c
int main() {
int x = 7 / 0; // bug here
return 0;
}
run command & result with gcc
$ scan-build -v gcc -o hello hello.c
scan-build: INFO: Report directory created: /tmp/scan-build-2021-06-29-03-50-40-733039-n821xtkx
hello.c: In function ‘main’:
hello.c:2:13: warning: division by zero [-Wdiv-by-zero]
2 | int x = 7 / 0; // Memory leak
| ^
scan-build: WARNING: Removing directory '/tmp/scan-build-2021-06-29-03-50-40-733039-n821xtkx' because it contains no report.
with clang
$ scan-build -v clang -o hello hello.c
scan-build: INFO: Report directory created: /tmp/scan-build-2021-06-29-03-51-08-738812-1059tk4t
hello.c:2:13: warning: division by zero is undefined [-Wdivision-by-zero]
int x = 7 / 0; // Memory leak
^ ~
1 warning generated.
scan-build: WARNING: Removing directory '/tmp/scan-build-2021-06-29-03-51-08-738812-1059tk4t' because it contains no report.
I can't understand that the warnning is genereted (maybe by the compiler?), but still why the scan-build is not working?
**** UPDATED 06/30/2021
I follow this tutorial, and finnaly get the "python" version of scan-build which is such a misunderstanding, the answer perfectly solved my problem, now is working now.
https://github.com/rizsotto/scan-build
works now!
Somehow you are invoking scan-build-py instead of scan-build.
In my Ubuntu 20.04, after installation (sudo apt install clang-tools):
/usr/bin/scan-build -> scan-build-10*
/usr/bin/scan-build-10 -> ../share/clang/scan-build-10/bin/scan-build*
/usr/bin/scan-build-py-10 -> ../share/clang/scan-build-py-10/bin/scan-build*
If I use /usr/bin/scan-build-py-10, I get the same output as you.
If I use /usr/bin/scan-build-10 I get the expected output when analyzing your C program.
$ scan-build-10 -v gcc -o hello hello.c
scan-build: Using '/usr/lib/llvm-10/bin/clang' for static analysis
scan-build: Emitting reports for this run to '/tmp/scan-build-2021-06-29-065953-647-1'.
hello.c: In function ‘main’:
hello.c:2:15: warning: division by zero [-Wdiv-by-zero]
2 | int x = 7 / 0; // bug here
| ^
hello.c:2:9: warning: Value stored to 'x' during its initialization is never read
int x = 7 / 0; // bug here
^ ~~~~~
hello.c:2:15: warning: Division by zero
int x = 7 / 0; // bug here
~~^~~
2 warnings generated.
scan-build: 2 bugs found.
scan-build: Run 'scan-view /tmp/scan-build-2021-06-29-065953-647-1' to examine bug reports.
$ scan-view /tmp/scan-build-2021-06-29-065953-647-1

Link Main error with library

Recently, I tried to create a library and to do that I created two files: file-parser.c and array.c with their corresponded .h file. So, in the file-parser I am using a function from the array that calls a length and when I compile (without linking it) parser-file.c the commands
gcc -c -o file-parser.o file-parser.c
gcc -c -o array.o array.c
ar r libutils.a array.o file-parser.o
don't tell me an error.
The problem is that when I compile the main.c file
gcc -o prova main.c -L. -lutils
gcc returns:
array/libutils.a(file-parser.o): in function "AnalizeRow":
file-parser.c:(.text+0x109): undefined reference to "length"
array/libutils.a(file-parser.o): in the function "GetWord":
file-parser.c:(.text+0x16a): undefined reference to "length"
collect2: error: ld returned 1 exit status
I'm not English, so I think the error message took to me by ld.
Can anyone explain where was my error?
Thanks.
A similar error may take place in anywhere in a development process!!!
Your question does not give a complete picture about current development process.
I see, after read your question, you has two files with source code - array.c and
file-parser.c. Also, you mentioned the static library libutils.a, but you did not indicate - how are you created it and what contains it for now.
I try to recreate your current position:
The file file-parser.c
#include <stdlib.h>
int
AnalizeRow(char row[])
{
size_t len = length(row);
return 0;
}
char *
GetWord(char row[])
{
size_t len = length(row);
return NULL;
}
The file file-parser.h
int AnalizeRow(char row[]);
char* GetWord(char row[]);
The file array.c
#include <string.h>
size_t
length(char row[])
{
return strlen(row);
}
The file array.h
size_t length(char row[]);
Create the empty static library libutils.a
$ ar cr libutils.a
$ ar t libutils.a (it empty)
Your code
$ gcc -c -o file-parser.o file-parser.c
$ gcc -c -o array.o array.c
$ ar r libutils.a array.o file-parser.o
Content the libutils.a
$ ar t libutils.a
array.o
file-parser.o
Build and run
$ gcc -o prova main.c -L. -lutils && ./prova
I have not errors at all..
Preliminary conclusions:
I have wrong development process
Your static library libutils.a is cause the problem
Your source code in files is cause the problem
Testing environment
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie
$ uname -a
$ apt-cache policy binutils
binutils:
Installed: 2.25-5+deb8u1
Candidate: 2.25-5+deb8u1
Version table:
*** 2.25-5+deb8u1 0
500 http://ftp.ru.debian.org/debian/ jessie-proposed-updates/main amd64 Packages
100 /var/lib/dpkg/status
2.25-5 0
500 http://ftp.ru.debian.org/debian/ jessie/main amd64 Packages
500 http://httpredir.debian.org/debian/ jessie/main amd64 Packages

C - Print value stored at symbol location

I have a simple command line application (custom dir binary) which I would like to instrument. The debug symbols are enabled, and I can see the global string pointer I'm interested in, the_full_path_name in the output of objdump and nm -D.
Is it possible, in c, to somehow lookup that symbol name/location, and print the contents of the memory which it points at using code injection (ie: LD_PRELOAD library with a custom gcc attribute((constructor)) and additional functions)? I need to accomplish this without having to attach gdb to the process.
Thank you.
I am not really sure if i understood your question but does following help you anyways?
File containing global pointer
$ cat global.c
char mylongstring[] = "myname is nulled pointer";
$ gcc -fPIC -shared global.c -o libglobal.so
Original library
$ cat get_orig.c
#include <stdio.h>
extern char * mylongstring;
char *get()
{
mylongstring = "get from orig";
return mylongstring;
}
$ gcc -fPIC -shared get_orig.c -o libget_orig.so -L. -lglobal
Fake Library
$ cat get_dup.c
#include <stdio.h>
extern char * mylongstring;
char *get()
{
mylongstring = "get from dup";
return mylongstring;
}
$ gcc -fPIC -shared get_dup.c -o libget_dup.so -L. -lglobal
Actual consumer of global variable:
$ cat printglobal.c
#include <stdio.h>
char *get();
int main(void)
{
printf("global value=%s\n",get());
return 0;
}
$ gcc printglobal.c -L. -lglobal -lget_orig -o a.out
otool output
$ otool -L a.out
a.out:
libglobal.so (compatibility version 0.0.0, current version 0.0.0)
libget_orig.so (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
Running a.out
$ DYLD_LIBRARY_PATH=./ ./a.out
global value=get from orig
Replace library
$ cp /tmp/libget_dup.so libget_orig.so
$ DYLD_LIBRARY_PATH=./ ./a.out
global value=get from dup
BTW, i tested this on MAC so .so is really a misnomer for .dylib

Read binary file,cannot execute binary file: Exec format error

My code
#include <stdio.h>
#define FILENAME "m_6"
int main() {
float f;
FILE * file;
// read values
if ( ! (file = fopen(FILENAME,"r")))
return 1;
while ( fread( &f, sizeof(float), 1, file ))
printf("%e\n", f);
fclose( file );
}
When I compile with gcc gcc -c n1.c -o n1
and try to run it I got
bash: ./n1: cannot execute binary file: Exec format error
File m_6 and n1.c were executed on the same machine.
3.13.0-76-generic #120-Ubuntu SMP Mon Jan 18 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
How to solve this?
gcc -c compiles source files without linking.
Cancel -c from from command.

Resources