tonumber("-") returns 0 #6

Closed
opened 2018-03-14 02:01:03 +00:00 by technomancy · 2 comments
technomancy commented 2018-03-14 02:01:03 +00:00 (Migrated from github.com)

It should return nil.

It should return nil.
noncom commented 2019-06-04 20:21:21 +00:00 (Migrated from github.com)

Confirming. This, for example, breaks https://github.com/bakpakin/Fennel

The fix that I am using is in the base method of LuaString.scanlong():

private double scanlong( int base, int start, int end ) {
	long x = 0;
	boolean neg = (m_bytes[start] == '-');
        // --- fix starts here ---
	if(neg && m_bytes.length == 1) {
		return Double.NaN; // this is only a '-' sign, no parsing any number is required
	}
        // --- fix ends here ---
	for ( int i=(neg?start+1:start); i<end; i++ ) {
		int digit = m_bytes[i] - (base<=10||(m_bytes[i]>='0'&&m_bytes[i]<='9')? '0':
				m_bytes[i]>='A'&&m_bytes[i]<='Z'? ('A'-10): ('a'-10));
		if ( digit < 0 || digit >= base )
			return Double.NaN;		
		x = x * base + digit;
		if ( x < 0 )
			return Double.NaN; // overflow
	}
	return neg? -x: x;
}

Can this be considered a good fix for this?

Confirming. This, for example, breaks https://github.com/bakpakin/Fennel The fix that I am using is in the base method of `LuaString.scanlong()`: private double scanlong( int base, int start, int end ) { long x = 0; boolean neg = (m_bytes[start] == '-'); // --- fix starts here --- if(neg && m_bytes.length == 1) { return Double.NaN; // this is only a '-' sign, no parsing any number is required } // --- fix ends here --- for ( int i=(neg?start+1:start); i<end; i++ ) { int digit = m_bytes[i] - (base<=10||(m_bytes[i]>='0'&&m_bytes[i]<='9')? '0': m_bytes[i]>='A'&&m_bytes[i]<='Z'? ('A'-10): ('a'-10)); if ( digit < 0 || digit >= base ) return Double.NaN; x = x * base + digit; if ( x < 0 ) return Double.NaN; // overflow } return neg? -x: x; } Can this be considered a good fix for this?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: open-autonomous-connection/luaj#6