From:             php at codewhore dot org
Operating system: Linux 2.4
PHP version:      4.3.1
PHP Bug Type:     Filesystem function related
Bug description:  Certain combinations of fseek() and fwrite() produce unexpected 
results

In PHP:

<?php
  $fp = fopen('./foo', 'w+');
  fwrite($fp, 'quxbar', 6);
  fseek($fp, 3, SEEK_SET);
  fread($fp, 1);
  fseek($fp, 4, SEEK_SET);
  fwrite($fp, '!', 1);
  fseek($fp, 0, SEEK_SET);
  $buf = fread($fp, 4095);
  echo "$buf\n";
?>

In C:

#include <stdio.h>

int main()
{
  static char buf[4096];
  FILE *f = fopen("./foo", "w+");
  fwrite("quxbar", 6, 1, f);
  fseek(f, 3, SEEK_SET);
  fread(&buf, 1, 1, f);
  fseek(f, 4, SEEK_SET);
  fwrite("!", 1, 1, f);
  fseek(f, 0, SEEK_SET);
  fread(&buf, 4095, 1, f);
  printf("%s\n", buf);
  return 0;
}

The PHP version outputs 'quxbar!.
The C version outputs 'quxb!r'.

In fact, for any PHP code of the structure:
<?php
  fseek($fp, $x, SEEK_SET);
  fread($fp, $n);
  fseek($fp, $y, SEEK_SET);
  fwrite($fp, '!', 1);
?>

the data from the fwrite always gets written to the end of the stream when
$x < $y and $n is exactly 1. I can reproduce this with different whence
parameters, but it seems to only happen with a seek followed by a
single-byte read.
-- 
Edit bug report at http://bugs.php.net/?id=22362&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=22362&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=22362&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=22362&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=22362&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=22362&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=22362&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=22362&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=22362&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=22362&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=22362&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=22362&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=22362&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=22362&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=22362&r=gnused

Reply via email to