PDA

View Full Version : [solved] file_get_contents BUG


geleont
02-01-2011, 12:25 PM
file_get_contents in quercus 4.0.11 has bug.

Minimal code to reproduce:
<?php
file_put_contents("123", "Hello",LOCK_EX);
?>
Cause
java.lang.NullPointerException
com.caucho.quercus.lib.file.FileOutput.unlock(File Output.java:197)
com.caucho.quercus.lib.file.FileModule.flock(FileM odule.java:1087)
com.caucho.quercus.lib.file.FileModule.file_put_co ntents(FileModule.java:1045)
I reviewed source code of quercus FileModule module and I am sure it happens because we close os, which is linked on the same object as s(BinaryOutput os = (BinaryOutput) s;) and after that we trying to make
flock(env, (LockableStream) s, LOCK_UN, null);
on that stream.
public static Value file_put_contents(Env env,
StringValue filename,
Value data,
@Optional int flags,
@Optional Value context)
{
if (filename.length() == 0) {
env.warning(L.l("file name must not be null"));
return BooleanValue.FALSE;
}

// php/1634

BinaryStream s = null;

try {
boolean useIncludePath = (flags &; FILE_USE_INCLUDE_PATH) != 0;
String mode = (flags &; FILE_APPEND) != 0 ? "a" : "w";

s = fopen(env, filename, mode, useIncludePath, context);

if (! (s instanceof BinaryOutput))
return BooleanValue.FALSE;

if ((flags &; LOCK_EX) != 0) {
if (s instanceof LockableStream) {
if (! flock(env, (LockableStream) s, LOCK_EX, null))
return BooleanValue.FALSE;
} else {
return BooleanValue.FALSE;
}
}

BinaryOutput os = (BinaryOutput) s;

try {
long dataWritten = 0;

if (data instanceof ArrayValue) {
for (Value item : ((ArrayValue) data).values()) {
InputStream is = item.toInputStream();

dataWritten += os.write(is, Integer.MAX_VALUE);

is.close();
}
}
else {
InputStream is = data.toInputStream();

dataWritten += os.write(is, Integer.MAX_VALUE);

is.close();
}

return LongValue.create(dataWritten);
} finally {
os.close();
}
} catch (IOException e) {
throw new QuercusModuleException(e);
} finally {
if (s != null &;&; (s instanceof LockableStream)
&;&; ((flags &; LOCK_EX) != 0))
flock(env, (LockableStream) s, LOCK_UN, null);
}
}

domdorn
02-02-2011, 10:12 AM
thanks for adding it to the issue tracker.

http://bugs.caucho.com/view.php?id=4370

domdorn
02-02-2011, 11:17 AM
bug is fixed in current trunk.