[erlang-bugs] prim_file bug

Martin Bjorklund <>
Fri Oct 20 12:43:08 CEST 2006


Reading 0 bytes from a raw file returns eof, instead of {ok, []} or
{ok, <<>>}.

(non-raw file works, and the man page says that it should return {ok,
...}.)


Eshell V5.5.1  (abort with ^G)
1> {ok, F} = file:open("README", [read,raw]).
{ok,{file_descriptor,prim_file,{#Port<0.101>,5}}}
2> file:read(F, 0).
eof
3> {ok, G} = file:open("README", [read]).    
{ok,<0.38.0>}
4> file:read(G, 0).                     
{ok,[]}



Tested in R10B-10 and R11B-1.


Here's a patch, but I'm not 100% sure it's correct.



Index: prim_file.erl
===================================================================
--- prim_file.erl       (revision 6046)
+++ prim_file.erl       (working copy)
@@ -296,7 +296,7 @@
 read(#file_descriptor{module = ?MODULE, data = {Port, _}}, Size)
   when is_integer(Size), 0 =< Size, Size < ?LARGEFILESIZE ->
     case drv_command(Port, <<?FILE_READ, Size:64>>) of
-       {ok, {0, _Data}} ->
+       {ok, {0, _Data}} when Size =/= 0 ->
            eof;
        {ok, 0} ->
            eof;



Index: efile_drv.c
===================================================================
--- efile_drv.c (revision 6046)
+++ efile_drv.c (working copy)
@@ -2170,11 +2170,13 @@
            reply_posix_error(desc, EBADF);
            goto done;
        }
+/*
        if (size == 0) {
            reply_Uint(desc, size);
            goto done;
        }
-       if (desc->read_size >= size) {
+*/
+       if (desc->read_size > 0 && desc->read_size >= size) {
            /* We already have all data */
            TRACE_C('D');
            reply_data(desc, desc->read_binp, desc->read_offset, size);



/martin



More information about the erlang-bugs mailing list