Problems of 64-bit code in real programs: FreeBSD

02.02.2009 Evgeniy Ryzhkov

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://www.viva64.com/go.php?url=503:

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 http://www.viva64.com/go.php?url=504 :

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?