bugGNU Octave - Bugs: bug #63968, fwrite writes incorrect number of...

 
 

bug #63968: fwrite writes incorrect number of bytes

Submitter:  None
Submitted:  Mon 27 Mar 2023 02:05:19 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Duplicate Assigned to:  None
Originator Name:  Michael Swamp Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 8.1.0
Operating System:  * Any Fixed Release:  8.2.0
Planned Release:  8.2.0
* Mandatory Fields

Add a New Comment Rich Markup
   

Sat 08 Apr 2023 10:40:52 AM UTC, comment #4: 

With the recent fixes to bug #63931, this too looks fixed. Closing as fixed. If you have problems, comment here and it can be reopened. It'll be in Octave 8.2.0 to be released next week if all goes well.

Arun Giridhar <arungiridhar>
Group Member
Mon 27 Mar 2023 02:54:29 PM UTC, comment #3: 

On second thought, looking at the file modes in the other two bugs and in this one, there's something subtly different. The test code given in this bug report writes numbers as uint8 but in text mode, not in binary mode, so there needs to be an additional criterion to skip the text encoding to/from UTF-8.

Reopening as in progress.

All bugs related to the encoding problem:


The first two seem to have been fixed by https://hg.savannah.gnu.org/hgweb/octave/rev/fcd97a68e5f7 but that applies only to binary mode.

The last two are in progress.

For anyone new coming across this report, it's the same workaround for all bugs: set your encoding mode to UTF-8. In the GUI, it's under Edit / Preferences / Editor tab / "Text encoding used for loading and saving". You can also run the mfile_encoding command in comment #2 below.

Arun Giridhar <arungiridhar>
Group Member
Mon 27 Mar 2023 02:30:42 PM UTC, comment #2: 

I ran your code and this is the output I got with both the unpatched and patched versions, so this looks like good news?


octave:1> test_63968
Writing File
Wrote 16386810 bytes

File Pointer 16386810

Write took 95.057924 seconds

Reading File
Read took 0.087963 seconds

Read 16386810 bytes

File Pointer 16386810

Reading File
Read took 141.914758 seconds

Read 16386810 bytes


Then to double-check, I set the encoding to latin1, and sure enough I got the error:


octave:1> __mfile_encoding__ ("latin1")
octave:2> __mfile_encoding__
ans = latin1
octave:3> test_63968
Writing File
Wrote 16386810 bytes

File Pointer 16277485

Write took 99.510554 seconds

Reading File
Read took 0.088034 seconds

Read 16277511 bytes

File Pointer 16277511

Reading File
Index 254 Index 76 Index 251 Read Failure

File Pointer 16277511

error: Invalid Read
error: called from
    test_63968 at line 49 column 9


This is identical to the other two bugs, so I'll leave this closed as a duplicate but if you want a workaround before 8.2.0 is released, please set your encoding to UTF-8. You can do that in the Octave GUI, or from the command prompt you can run


__mfile_encoding__ ("utf-8")


Arun Giridhar <arungiridhar>
Group Member
Mon 27 Mar 2023 02:12:01 PM UTC, comment #1: 

A fix for this problem has already been applied to the stable branch and if there are no problems reported with it, it will show up in Octave 8.2.0.

See these bug reports:


The fix:


Closing as duplicate, but if you think your bug is different, comment here and it can be reopened.

Arun Giridhar <arungiridhar>
Group Member
Mon 27 Mar 2023 02:05:19 PM UTC, original submission:  

Seems to be a few bugs with fwrite.  Not only is it writing incorrect data when you do binary writes with uint8, but it also seems to be writing an incorrect number of bytes and not reporting a write error.

Run the following script.  It does not report a write error, but when you read the data back it has a different amount of bytes.

display("Writing File");
ID = tic();
fid = fopen("TestWrite","w");
for i = 1:255
  for j = 1:254
    for k = 1:253
      CNT = fwrite(fid,uint8(k),"uint8");
      if (CNT == 0)
        display(sprintf("Index %d Index %d Index %d Write Failure\n",i,j,k));
        error("Write Error");
      endif
    endfor
  endfor
endfor
WRITE_FP = ftell(fid);
display(sprintf("Wrote %d bytes\n",i*j*k));
display(sprintf("File Pointer %d\n",WRITE_FP));
fclose(fid);
elapsed_time = toc(ID);
display(sprintf("Write took %f seconds\n",elapsed_time));
##
## Read back Data
##
display("Reading File");
ID = tic();
fid = fopen("TestWrite","r");
DATA = uint8(fread(fid,"uint8"));
READ_FP = ftell(fid);
fclose(fid);
elapsed_time = toc(ID);
display(sprintf("Read took %f seconds\n",elapsed_time));
display(sprintf("Read %d bytes\n",length(DATA)));
display(sprintf("File Pointer %d\n",READ_FP));
##
## Read Back Data - Method 2
display("Reading File");
ID = tic();
fid = fopen("TestWrite","r");
DATA2=uint8([]);
for i = 1:255
  for j = 1:254
    for k = 1:253
      [VAL, CNT] = fread(fid,1,"uint8");
      if (CNT == 0)
        READ2_FP = ftell(fid);
        display(sprintf("Index %d Index %d Index %d Read Failure\n",i,j,k));
        display(sprintf("File Pointer %d\n",READ2_FP));
        error("Invalid Read");
      endif
      DATA2(i*j+k) = uint8(VAL);
    endfor
  endfor
endfor
fclose(fid);
elapsed_time = toc(ID);
display(sprintf("Read took %f seconds\n",elapsed_time));
display(sprintf("Read %d bytes\n",length(DATA)));

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #54532:  TestWriteFile.m added by None (2KiB - application/octet-stream)

 

Digest:
   bug dependencies.

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Updated the item)
  • -email is unavailable- added by arungiridhar (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 12 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-04-08 mmuetzel StatusFixed Duplicate
        Dependencies- Depends on bugs #63931
    2023-04-08 arungiridhar StatusIn Progress Fixed
        Open/ClosedOpen Closed
        Fixed ReleaseNone 8.2.0
    2023-03-27 arungiridhar StatusDuplicate In Progress
        Open/ClosedClosed Open
        Operating SystemMicrosoft Windows Any
        Planned ReleaseNone 8.2.0
    2023-03-27 arungiridhar StatusNone Duplicate
        Open/ClosedOpen Closed
    2023-03-27 None Attached File- Added TestWriteFile.m, #54532

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code