bugGNU Octave - Bugs: bug #65390, scanf sets error message when...

 
 

bug #65390: scanf sets error message when input ends with newline

Submitter:  A.R. Burgers <arb>
Submitted:  Fri 01 Mar 2024 03:10:47 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Regression
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * stable Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Sat 02 Mar 2024 10:06:13 PM UTC, comment #13: 

I checked in jwe's changeset on stable here:

https://hg.savannah.gnu.org/hgweb/octave/rev/9c08824262af

On the development branch, I restored the BIST test that check that the error message is empty:

https://hg.savannah.gnu.org/hgweb/octave/rev/2ff2d201a344

This will now produce a failing test on dev.

Rik <rik5>
Group administrator
Sat 02 Mar 2024 09:45:17 PM UTC, comment #12: 

@Dmitri: I'm fine with checking in jwe's patch for now.  I will include some BIST tests and do that soon.

Rik <rik5>
Group administrator
Sat 02 Mar 2024 07:55:40 PM UTC, comment #11: 

I vote to apply jwe patch and deal with misleading error message post 9.1 release. At the moment a workaround is to specify "\n" explicitly in the format string.


octave:12> s = "1 2\n3 4\n"
s = 1 2
3 4

octave:13> [a,b,c,d] = sscanf (s, '%f %f\n', [2 inf])
a =

   1   3
   2   4

b = 4
c =
d = 9
octave:14>


Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Sat 02 Mar 2024 07:43:51 PM UTC, comment #10: 

I just spent an hour on this and am still no closer to a solution.  There are several levels of function calls and the code is manipulating the stream state bits to communicate information in an out-of-band manner up and down the stack.  It would probably be wiser to actually have R/W parameters to the various functions so that we could easily distinguish between a true error on the stream and an EOF which should really just break out of various looping structures.

Rik <rik5>
Group administrator
Sat 02 Mar 2024 06:50:31 PM UTC, comment #9: 

I hate this function, so many corner cases.

@jwe: The patch correctly restores reading two values instead of three.  However, the third output (error message) is wrong.  See below.


octave:1> s = "1 2\n"
s = 1 2

octave:2> [a,b,c,d] = sscanf (s, '%f %f')
a =

   1
   2

b = 2
c = sscanf: format failed to match
d = 5


The output 'c' should be empty because there is no error.


Separately, bug #63467 is definitely related and is also solved by jwe's patch.

Rik <rik5>
Group administrator
Sat 02 Mar 2024 01:10:30 AM UTC, comment #8: 
Hendrik K <koerhen>
Fri 01 Mar 2024 10:37:11 PM UTC, comment #7: 

Works for me.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Fri 01 Mar 2024 10:18:41 PM UTC, comment #6: 

Maybe the attached change?  It attempts to make the stream state handling for floating point conversions (performed in the octave_scan_conv<double> specialization) the same as what is used in the octave_scan_1 function that is used to process integer conversions.



(file #55762)

John W. Eaton <jwe>
Group administrator
Fri 01 Mar 2024 07:31:38 PM UTC, comment #5: 

Confirmed.  Changing release to stable as the bug is present there as well.

Rik <rik5>
Group administrator
Fri 01 Mar 2024 07:25:57 PM UTC, comment #4: 

Bisected to:

changeset:   31542:2f08a53e0a23
parent:      31540:ce5b4a00b022
user:        Rik <rik@octave.org>
date:        Fri Nov 25 08:30:30 2022 -0800
summary:     Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)


31540 is good, 31542 is bad.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Fri 01 Mar 2024 06:09:49 PM UTC, comment #3: 

The same with "sscanf":

octave:1> s1 = sprintf("1 2")
s1 = 1 2
octave:2> a1 = sscanf(s1, '%f  %f')
a1 =

   1
   2

octave:3> s2 = sprintf("1 2\n")
s2 = 1 2

octave:4> a2 = sscanf(s2, '%f  %f')
a2 =

   1
   2
   2


So "\n" seems to break the "Xscanf"

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Fri 01 Mar 2024 03:51:08 PM UTC, comment #2: 

This is also regression. I cannot reproduce this on octave 8.4 nor  7.3.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Fri 01 Mar 2024 03:31:12 PM UTC, comment #1: 

Confirmed with 9.0.91. A simplified case:

$ cat tst2.dat
1        2

octave:1> fd = fopen("tst2.dat", "r")
fd = 19
octave:2> ar = fscanf(fd, '%f  %f')
ar =

   1
   2
   2


Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Fri 01 Mar 2024 03:10:47 PM UTC, original submission:  

This example shows a case where fscanf fails I think to correctly read an input:


np = 101;
x = linspace(0, 1, np)';
a = [x, x.^2];
dlmwrite('tst.dat', a, '\t');
fd = fopen('tst.dat', 'r');
ar = fscanf(fd, '%g %g', [2, Inf])';
fclose(fd);
ar(np - 5: end, :)
try
  [a(np - 5 : end, :), ar(np - 5 : end, :)]
catch me
  error('ran into %s', me.message);
end


octave output, featuring an extra row at the end:


ans =
   0.9500   0.9025
   0.9600   0.9216
   0.9700   0.9409
   0.9800   0.9604
   0.9900   0.9801
   1.0000   1.0000
   1.0000        0

error: ran into horizontal dimensions mismatch (6x2 vs 7x2)
error: called from
    tst at line 12 column 3


matlab output:


ans =
    0.9500    0.9025
    0.9600    0.9216
    0.9700    0.9409
    0.9800    0.9604
    0.9900    0.9801
    1.0000    1.0000

ans =
    0.9500    0.9025    0.9500    0.9025
    0.9600    0.9216    0.9600    0.9216
    0.9700    0.9409    0.9700    0.9409
    0.9800    0.9604    0.9800    0.9604
    0.9900    0.9801    0.9900    0.9801
    1.0000    1.0000    1.0000    1.0000


A.R. Burgers <arb>

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #55762:  scanf-patch.txt added by jwe (1KiB - text/plain)

 

Digest:
   bug dependencies.

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by koerhen (Posted a comment)
  • -email is unavailable- added by jwe (Updated the item)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by dasergatskov (Posted a comment)
  • -email is unavailable- added by arb (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 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-03-02 rik5 Summaryfscanf produces 1 line too much scanf sets error message when input ends with newline
    2024-03-02 rik5 Dependencies- Depends on bugs #63467
    2024-03-01 jwe Attached File- Added scanf-patch.txt, #55762
    2024-03-01 rik5 Item GroupIncorrect Result Regression
        StatusNone Confirmed
        Releasedev stable

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code