bugGNU Octave - Bugs: bug #63383, Error in sscanf in special cases...

 
 

bug #63383: Error in sscanf in special cases when '%f' conversion is used

Submitter:  José Luis García Pallero <jgpallero>
Submitted:  Fri 18 Nov 2022 02:34:46 PM UTC
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  jgpallero Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment (Rich Markup)
   

Comment Type & Canned Response:
       

 

( Jump to the original submission )

Fri 25 Nov 2022 04:58:52 PM UTC, comment #6: 

Thank you very much for your work, Rik

José Luis García Pallero <jgpallero>
Fri 25 Nov 2022 04:53:01 PM UTC, comment #5: 

I finally finished work on the scanf set of function (last changeset: http://hg.savannah.gnu.org/hgweb/octave/rev/2f08a53e0a23).  Everything should be working on the development branch now (Octave 9).  The changes were deemed too uncertain to be placed on the stable branch (Octave 8.1) a mere month ahead of an expected release.  However, with more testing they should be able to be a part of the 8.2 bug fix release.

Marking bug as Fixed and closing report.

Rik <rik5>
Project Administrator
Tue 22 Nov 2022 04:46:49 PM UTC, comment #4: 

I just wanted to note that this bug report has spawned hours of work trying to understand, and then correct the behavior of the octave_stream class.  The bad news is that this code is very complicated, spread over multiple files, and because it is so brittle making even small changes breaks other bits of functionality.

The good news is that progress has been made and that a bare '-' character now issues an error for both %d and %f formats with the stream pointer correctly pointing to the source of the error.  See changesets http://hg.savannah.gnu.org/hgweb/octave/rev/d006285b7509 and http://hg.savannah.gnu.org/hgweb/octave/rev/2d38bc5a35db.

Rik <rik5>
Project Administrator
Sun 20 Nov 2022 01:33:34 AM UTC, comment #3: 

the scanf parsing is complex and distributed across functions in libinterp and in liboctave.  Using the debugger, it appears that reading a double in liboctave is correctly setting the fail bit on the input stream.  However, back in libinterp the test for the fail condition is specifically disabled if the failure occurs at the EOF.

if (! is.eof () && is.rdstate () & std::ios::failbit)

Removing the eof() test with this patch makes things work for this bug report.

diff --git a/libinterp/corefcn/oct-stream.cc b/libinterp/corefcn/oct-stream.cc
--- a/libinterp/corefcn/oct-stream.cc
+++ b/libinterp/corefcn/oct-stream.cc
@@ -5042,7 +5042,8 @@ namespace octave

                     // If it looks like we have a matching failure, then
                     // reset the failbit in the stream state.
-                    if (! is.eof () && is.rdstate () & std::ios::failbit)
+                    //if (! is.eof () && is.rdstate () & std::ios::failbit)
+                    if (is.rdstate () & std::ios::failbit)
                       {
                         error (who, "format failed to match");
                         is.clear (is.rdstate () & (~std::ios::failbit));

But, this then causes a regression BIST to fail for bug #62723.

Overall, this is probably not too difficult to fix.  But, I think it means re-visiting bug #62723 and finding a different solution there.

Rik <rik5>
Project Administrator
Fri 18 Nov 2022 08:02:00 PM UTC, comment #2: 

comentario nº1:

> This problem also happens if you replace 'n' with 'i', and you can also see what I think is a position error with '%d' in the following example:
>

> octave> [val, count, msg, pos] = sscanf ('1 2 3 -', '%d')
> val =
>
>    1
>    2
>    3
>
> count = 3
> msg = sscanf: format failed to match
> pos = 8

>
> What does Matlab do for this case?


Mmmm, in my case Octave returns:

>> [val, count, msg, pos] = sscanf ('1 2 3 -', '%d')
val =

   1
   2
   3

count = 3
msg =
pos = 8

The error message is not set. I'm running Octave 7.3.0 compiled by myself in Debian Sid

On the other hand, Matlab 9.2.0.538062 (R2017a) returns:

>> [val, count, msg, pos] = sscanf ('1 2 3 -', '%d')
val =
     1
     2
     3
count =
     3

msg =
    'Matching failure in format.'

pos =
     7

José Luis García Pallero <jgpallero>
Fri 18 Nov 2022 07:34:43 PM UTC, comment #1: 

This error happens because Octave is trying to parse "nan" and is detecting a conversion error (so only three values are returned) but failing to correctly set the error message.  You'll also see that the position is also not set correctly if you try

[val, count, msg, pos] = sscanf ('1 2 3 n', '%f')

This problem also happens if you replace 'n' with 'i', and you can also see what I think is a position error with '%d' in the following example:

octave> [val, count, msg, pos] = sscanf ('1 2 3 -', '%d')
val =

   1
   2
   3

count = 3
msg = sscanf: format failed to match
pos = 8

What does Matlab do for this case?

John W. Eaton <jwe>
Project Administrator
Fri 18 Nov 2022 02:34:46 PM UTC, original submission:  

When I use the text string

>> a = '1 2 3 t';

in sscanf in order to extract the elements using '%f' I obtain obviously an error message due to the last element:

>> [elements,n,msg]=sscanf(a,'%f')
elements =

   1
   2
   3

n = 3
msg = sscanf: format failed to match

But if the last element is the letter 'n':

>> a = '1 2 3 n';
>> [elements,n,msg]=sscanf(a,'%f')
elements =

   1
   2
   3

n = 3
msg =

Only three elements are extracted, as expected, but the error message is not set, so this can not be used to check for errors (Matlab gets the error message). The proplem is present, I think, only with letter 'n'.

If I use '%d' as conversion there is no problem:

>> a = '1 2 3 n';
>> a = '1 2 3 n';
>> [elements,n,msg]=sscanf(a,'%d')
elements =

   1
   2
   3

n = 3
msg = sscanf: format failed to match

José Luis García Pallero <jgpallero>

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by jgpallero (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 project members can vote.

     

     

     

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-11-25 rik5 StatusIn Progress Fixed
        Open/ClosedOpen Closed
    2022-11-22 rik5 StatusConfirmed In Progress
    2022-11-18 jwe StatusNone Confirmed
        Releaseother dev

    Back to the top


    Powered by Savane 3.9