From 7e5970673f1caa54bbf61649f3b4b3f37bceafb4 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 3 May 2017 16:30:22 +0100 Subject: [PATCH 1/3] Ensure GPS coordinates are numbers Fixes #138 --- src/main/resources/assets/computercraft/lua/rom/apis/gps | 2 +- src/main/resources/assets/computercraft/lua/rom/apis/vector | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/gps b/src/main/resources/assets/computercraft/lua/rom/apis/gps index 9f7cb5fd0..5fabbd0dd 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/gps +++ b/src/main/resources/assets/computercraft/lua/rom/apis/gps @@ -103,7 +103,7 @@ function locate( _nTimeout, _bDebug ) local sSide, sChannel, sReplyChannel, tMessage, nDistance = p1, p2, p3, p4, p5 if sSide == sModemSide and sChannel == os.getComputerID() and sReplyChannel == CHANNEL_GPS and nDistance then -- Received the correct message from the correct modem: use it to determine position - if type(tMessage) == "table" and #tMessage == 3 then + if type(tMessage) == "table" and #tMessage == 3 and type(tMessage[1]) == "number" and type(tMessage[2]) == "number" and type(tMessage[3]) == "number" then local tFix = { vPosition = vector.new( tMessage[1], tMessage[2], tMessage[3] ), nDistance = nDistance } if _bDebug then print( tFix.nDistance.." metres from "..tostring( tFix.vPosition ) ) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/vector b/src/main/resources/assets/computercraft/lua/rom/apis/vector index e5d1a981a..29b10e500 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/vector +++ b/src/main/resources/assets/computercraft/lua/rom/apis/vector @@ -76,9 +76,9 @@ local vmetatable = { function new( x, y, z ) local v = { - x = x or 0, - y = y or 0, - z = z or 0 + x = type(x) == "number" and x or 0, + y = type(y) == "number" and y or 0, + z = type(z) == "number" and z or 0 } setmetatable( v, vmetatable ) return v From 67eecd4b1c277195d8e0fc1e0bd62f2c125c94aa Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 3 May 2017 18:21:14 +0100 Subject: [PATCH 2/3] Use tonumber instead of checking type --- src/main/resources/assets/computercraft/lua/rom/apis/vector | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/vector b/src/main/resources/assets/computercraft/lua/rom/apis/vector index 29b10e500..2fd2fbdb6 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/vector +++ b/src/main/resources/assets/computercraft/lua/rom/apis/vector @@ -76,9 +76,9 @@ local vmetatable = { function new( x, y, z ) local v = { - x = type(x) == "number" and x or 0, - y = type(y) == "number" and y or 0, - z = type(z) == "number" and z or 0 + x = tonumber(x) or 0, + y = tonumber(y) or 0, + z = tonumber(z) or 0 } setmetatable( v, vmetatable ) return v From 6b6829e22bcc34427c687d0d382efb2933914816 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 3 May 2017 18:39:32 +0100 Subject: [PATCH 3/3] Use tonumber when validating message format --- src/main/resources/assets/computercraft/lua/rom/apis/gps | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/gps b/src/main/resources/assets/computercraft/lua/rom/apis/gps index 5fabbd0dd..eede2679a 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/gps +++ b/src/main/resources/assets/computercraft/lua/rom/apis/gps @@ -103,7 +103,7 @@ function locate( _nTimeout, _bDebug ) local sSide, sChannel, sReplyChannel, tMessage, nDistance = p1, p2, p3, p4, p5 if sSide == sModemSide and sChannel == os.getComputerID() and sReplyChannel == CHANNEL_GPS and nDistance then -- Received the correct message from the correct modem: use it to determine position - if type(tMessage) == "table" and #tMessage == 3 and type(tMessage[1]) == "number" and type(tMessage[2]) == "number" and type(tMessage[3]) == "number" then + if type(tMessage) == "table" and #tMessage == 3 and tonumber(tMessage[1]) and tonumber(tMessage[2]) and tonumber(tMessage[3]) then local tFix = { vPosition = vector.new( tMessage[1], tMessage[2], tMessage[3] ), nDistance = nDistance } if _bDebug then print( tFix.nDistance.." metres from "..tostring( tFix.vPosition ) )