Hi!
I am writing a program which uses libcdio for accessing audio tracks on a cd. After making some changes to the code, the program started to misbehave. It did not crash but exhibited some strange (and unwanted) behaviour. It took me half of the day to figure out what was going on.
It turned out libcdio is compiled with large file support, which causes the off_t type to be 8 bytes. In my program (compiled without -D_FILE_OFFSET_BITS=64) an off_t is only 4 bytes. At one point in the program I call a libcdio function taking an off_t and an int as arguments. However because of the mismatch in off_t sizes, my program would call the function with 8 bytes of arguments (4 for off_t, 4 for int). The function would take all 8 bytes and interpret them as a single off_t, and the int would have some random(?) garbage value. Here is a minimal example showing this problem:
So I know I can fix this by compiling my own program with large file support, or by defining off_t to some 64 bit int in my sources. But then the program would break in a similar fashion if it were ever linked against a libcdio built without large file support.Code:[~] $ cat function.h #ifndef FUNCTION_H_ #define FUNCTION_H_ #include <stdio.h> extern void function(off_t a, int b); #endif [~] $ cat function.c #include "function.h" void function(off_t a, int b) { if (a == 1 && b == 2) printf("Succes!\n"); else printf("Fail...\na: %lld\nb: %d\n", a, b); } [~] $ cat main.cc extern "C" { #include "function.h" } int main() { off_t a = 1; int b = 2; function(a, b); return 0; } [~] $ gcc -Wall -c -D_FILE_OFFSET_BITS=64 function.c [~] $ ar srv libtest.a function.o ar: creating libtest.a a - function.o [~] $ g++ -Wall main.cc -L. -ltest [~] $ ./a.out Fail... a: 8589934593 b: 0 [~] $
So the question is, is there any way to find out (preferably at compile time) the size of an off_t in the external library? Just having to guess, compile (always works without even a warning), run and then see if the program behaves properly is obviously not an elegant solution.
thanks,
bepaald


Reply With Quote
