Problems of 64-bit code in real programs: FreeBSD

Evgeniy Ryzhkov
Articles: 106


While telling people about problems and difficulties of developing 64-bit applications I often hear in response: "You just should write programs well and there will be no problems". What only have we done trying to persuade people that this view is absurd! We wrote articles, gave examples but there still were those who would stick to their opinion.

Surfing recently through the net searching for discussions of problems relating to 64-bit, at some forum I came across a description of strange behavior of a program for reading files. This program couldn't read a file of more than 2 GB although the code was correct:

size_t size;
size = fread(...);

Investigation showed that the error was... inside fread function of FreeBSD!

It is difficult to believe in this that's why I will prove my words. You can see the code of fread function in the repository of FreeBSD source code http://svnweb.freebsd.org/:

size_t
fread(void * __restrict buf, size_t size,
        size_t count, FILE * __restrict fp)
{
  int ret;
  FLOCKFILE(fp);
  ret = __fread(buf, size, count, fp);
  FUNLOCKFILE(fp);
  return (ret);
}

Oh my God, how could one define ret variable as int? Of course it's absolutely incorrect. This error was corrected... only in December 2008:

size_t
fread(void * __restrict buf, size_t size,
        size_t count, FILE * __restrict fp)
{
  size_t ret;
  FLOCKFILE(fp);
  ret = __fread(buf, size, count, fp);
  FUNLOCKFILE(fp);
  return (ret);
}

It is astonishing that an error in FreeBSD was corrected only in the end of 2008. Perhaps 64-bit systems are not called-for yet?



Use PVS-Studio to search for bugs in C, C++ and C# code

We offer you to check your project code with PVS-Studio. Just one bug found in the project will show you the benefits of the static code analysis methodology better than a dozen of the articles.

goto PVS-Studio;

Evgeniy Ryzhkov
Articles: 106


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++ and C#

goto PVS-Studio;