bugGNU Octave - Bugs: bug #47759, scanf should return min/max value...

 
 

bug #47759: scanf should return min/max value on integer range overflow

Submitter:  Mike Miller <mtmiller>
Submitted:  Fri 22 Apr 2016 03:04:08 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  mtmiller
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 11 May 2016 08:26:50 PM UTC, comment #3: 
Mike Miller <mtmiller>
Group Member
Tue 26 Apr 2016 05:20:24 PM UTC, comment #2: 

I tried an mxe-build with your patch on Windows 10 with the following results:

>> format long
>> [x, count, errmsg] = sscanf('9999999999', '%d')
x =  2147483647
count =  1
errmsg =
>> [x, count, errmsg] = sscanf('fffffffff', '%x')
x =  4294967295
count =  1
errmsg =
>> [x, count, errmsg] = sscanf('9999999999999999999', '%ld')
x =  9223372036854775808
count =  1
errmsg =
>> [x, count, errmsg] = sscanf('fffffffffffffffff', '%lx')
x =   1.84467440737096e+019
count =  1
errmsg =


So the original issue seems to be solved with your patch.

Markus Mützel <mmuetzel>
Group administrator
Sat 23 Apr 2016 08:54:16 AM UTC, comment #1: 

Matlab does not emit an error message on integer overflow:

>> format long
>> [x, count, errmsg] = sscanf('9999999999', '%d')
x =
     2.147483647000000e+09
count =
     1
errmsg =
     ''
>> [x, count, errmsg] = sscanf('fffffffff', '%x')
x =
     4.294967295000000e+09
count =
     1
errmsg =
     ''


For completeness, the same for "%ld" and %lx":

>> [x, count, errmsg] = sscanf('9999999999999999999', '%ld')
x =
  9223372036854775807
count =
     1
errmsg =
     ''
>> [x, count, errmsg] = sscanf('fffffffffffffffff', '%lx')
x =
 18446744073709551615
count =
     1
errmsg =
     ''


No error message either.

Maybe, mark the respective place in the code anyway stating this is Matlab's behavior. When this would be possible in the future, one might want to emit an error message anyway?

Markus Mützel <mmuetzel>
Group administrator
Fri 22 Apr 2016 03:04:08 AM UTC, original submission:  

According to tests in bug #47741, Matlab returns the minimum or maximum integer value if the converted value overflows the storage limit of a 32-bit integer (or 64-bit integer if %ld is used).

Octave currently stops conversion when it encounters a value that is out of range.

Does Matlab also return an errmsg string when a value overflows? What does Matlab do for the following:


format long
[x, count, errmsg] = sscanf('9999999999', '%d')
[x, count, errmsg] = sscanf('fffffffff', '%x')


The attached diff fixes the bug so overflow values are returned as the min/max value possible and conversion continues with the next value, but does not set errmsg. See also bug #46493 for why we can't currently set errmsg on success, but we can at least mark it as something to fix later for compatibility.

Mike Miller <mtmiller>
Group Member

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #36978:  scanf-overflow.diff added by mtmiller (3KiB - text/x-diff)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by mtmiller (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 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-05-11 mtmiller StatusIn Progress Fixed
        Open/ClosedOpen Closed
    2016-04-22 mtmiller Attached File- Added scanf-overflow.diff, #36978

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code