Commit f3367518 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed potential NRE

parent c396ccd7
...@@ -737,12 +737,20 @@ namespace NLua ...@@ -737,12 +737,20 @@ namespace NLua
public int GetInteger(string fullPath) public int GetInteger(string fullPath)
{ {
return (int)(long)GetObjectFromPath(fullPath); object result = GetObjectFromPath(fullPath);
if (result == null)
return 0;
return (int)(long)result;
} }
public long GetLong(string fullPath) public long GetLong(string fullPath)
{ {
return (long)GetObjectFromPath(fullPath); object result = GetObjectFromPath(fullPath);
if (result == null)
return 0L;
return (long)result;
} }
/* /*
...@@ -751,6 +759,9 @@ namespace NLua ...@@ -751,6 +759,9 @@ namespace NLua
public string GetString(string fullPath) public string GetString(string fullPath)
{ {
object obj = GetObjectFromPath(fullPath); object obj = GetObjectFromPath(fullPath);
if (obj == null)
return null;
return obj.ToString(); return obj.ToString();
} }
......
...@@ -84,7 +84,7 @@ namespace NLua ...@@ -84,7 +84,7 @@ namespace NLua
{ {
Lua lua; Lua lua;
if (!TryGet(out lua)) if (!TryGet(out lua))
return null; return new object[0];
return lua.GetTableDict(this).Values; return lua.GetTableDict(this).Values;
} }
......
...@@ -83,6 +83,9 @@ namespace NLua ...@@ -83,6 +83,9 @@ namespace NLua
var state = LuaState.FromIntPtr(luaState); var state = LuaState.FromIntPtr(luaState);
var translator = ObjectTranslatorPool.Instance.Find(state); var translator = ObjectTranslatorPool.Instance.Find(state);
var func = (LuaNativeFunction)translator.GetRawNetObject(state, 1); var func = (LuaNativeFunction)translator.GetRawNetObject(state, 1);
if (func == null)
return state.Error();
state.Remove(1); state.Remove(1);
int result = func(luaState); int result = func(luaState);
var exception = translator.GetObject(state, -1) as LuaScriptException; var exception = translator.GetObject(state, -1) as LuaScriptException;
...@@ -249,7 +252,7 @@ namespace NLua ...@@ -249,7 +252,7 @@ namespace NLua
return result; return result;
} }
static int UnaryNegationLua(LuaState luaState, ObjectTranslator translator) static int UnaryNegationLua(LuaState luaState, ObjectTranslator translator) //-V3009
{ {
object obj1 = translator.GetRawNetObject(luaState, 1); object obj1 = translator.GetRawNetObject(luaState, 1);
...@@ -347,7 +350,8 @@ namespace NLua ...@@ -347,7 +350,8 @@ namespace NLua
if (type == LuaType.UserData) if (type == LuaType.UserData)
{ {
object obj = translator.GetRawNetObject(luaState, i); object obj = translator.GetRawNetObject(luaState, i);
strrep = obj.ToString();
strrep = obj == null ? "(null)" : obj.ToString();
} }
Debug.WriteLine("{0}: ({1}) {2}", i, typestr, strrep); Debug.WriteLine("{0}: ({1}) {2}", i, typestr, strrep);
...@@ -403,7 +407,7 @@ namespace NLua ...@@ -403,7 +407,7 @@ namespace NLua
if (TryAccessByArray(luaState, objType, obj, index)) if (TryAccessByArray(luaState, objType, obj, index))
return 1; return 1;
int fallback = GetMethodFallback(luaState, objType, obj, index, methodName); int fallback = GetMethodFallback(luaState, objType, obj, methodName);
if (fallback != 0) if (fallback != 0)
return fallback; return fallback;
...@@ -518,7 +522,6 @@ namespace NLua ...@@ -518,7 +522,6 @@ namespace NLua
(LuaState luaState, (LuaState luaState,
Type objType, Type objType,
object obj, object obj,
object index,
string methodName) string methodName)
{ {
object method; object method;
...@@ -529,18 +532,18 @@ namespace NLua ...@@ -529,18 +532,18 @@ namespace NLua
// Try to use get_Item to index into this .net object // Try to use get_Item to index into this .net object
MethodInfo[] methods = objType.GetMethods(); MethodInfo[] methods = objType.GetMethods();
int res = TryIndexMethods(luaState, methods, obj, index); int res = TryIndexMethods(luaState, methods, obj);
if (res != 0) if (res != 0)
return res; return res;
// Fallback to GetRuntimeMethods // Fallback to GetRuntimeMethods
methods = objType.GetRuntimeMethods().ToArray(); methods = objType.GetRuntimeMethods().ToArray();
res = TryIndexMethods(luaState, methods, obj, index); res = TryIndexMethods(luaState, methods, obj);
if (res != 0) if (res != 0)
return res; return res;
res = TryGetValueForKeyMethods(luaState, methods, obj, index); res = TryGetValueForKeyMethods(luaState, methods, obj);
if (res != 0) if (res != 0)
return res; return res;
...@@ -564,7 +567,7 @@ namespace NLua ...@@ -564,7 +567,7 @@ namespace NLua
return 0; return 0;
} }
private int TryGetValueForKeyMethods(LuaState luaState, MethodInfo[] methods, object obj, object index) private int TryGetValueForKeyMethods(LuaState luaState, MethodInfo[] methods, object obj)
{ {
foreach (MethodInfo methodInfo in methods) foreach (MethodInfo methodInfo in methods)
{ {
...@@ -578,7 +581,7 @@ namespace NLua ...@@ -578,7 +581,7 @@ namespace NLua
ParameterInfo[] actualParams = methodInfo.GetParameters(); ParameterInfo[] actualParams = methodInfo.GetParameters();
// Get the index in a form acceptable to the getter // Get the index in a form acceptable to the getter
index = _translator.GetAsType(luaState, 2, actualParams[0].ParameterType); object index = _translator.GetAsType(luaState, 2, actualParams[0].ParameterType);
// If the index type and the parameter doesn't match, just skip it // If the index type and the parameter doesn't match, just skip it
if (index == null) if (index == null)
...@@ -617,7 +620,7 @@ namespace NLua ...@@ -617,7 +620,7 @@ namespace NLua
} }
private int TryIndexMethods(LuaState luaState, MethodInfo [] methods, object obj, object index) private int TryIndexMethods(LuaState luaState, MethodInfo [] methods, object obj)
{ {
foreach (MethodInfo methodInfo in methods) foreach (MethodInfo methodInfo in methods)
{ {
...@@ -631,7 +634,7 @@ namespace NLua ...@@ -631,7 +634,7 @@ namespace NLua
ParameterInfo[] actualParams = methodInfo.GetParameters(); ParameterInfo[] actualParams = methodInfo.GetParameters();
// Get the index in a form acceptable to the getter // Get the index in a form acceptable to the getter
index = _translator.GetAsType(luaState, 2, actualParams[0].ParameterType); object index = _translator.GetAsType(luaState, 2, actualParams[0].ParameterType);
// If the index type and the parameter doesn't match, just skip it // If the index type and the parameter doesn't match, just skip it
if (index == null) if (index == null)
......
using System; using System;
namespace NLua.Method namespace NLua.Method
{ {
...@@ -32,6 +32,9 @@ namespace NLua.Method ...@@ -32,6 +32,9 @@ namespace NLua.Method
int iRefArgs; int iRefArgs;
object[] returnValues = function.Call(inArgs, returnTypes); object[] returnValues = function.Call(inArgs, returnTypes);
if (returnValues == null || returnTypes.Length == 0)
return null;
if (returnTypes[0] == typeof(void)) if (returnTypes[0] == typeof(void))
{ {
returnValue = null; returnValue = null;
......
...@@ -516,7 +516,7 @@ namespace NLua ...@@ -516,7 +516,7 @@ namespace NLua
return result; return result;
} }
private int GetMethodSignatureInternal(LuaState luaState) private int GetMethodSignatureInternal(LuaState luaState) //-V3009
{ {
ProxyType klass; ProxyType klass;
object target; object target;
...@@ -581,7 +581,7 @@ namespace NLua ...@@ -581,7 +581,7 @@ namespace NLua
return result; return result;
} }
private int GetConstructorSignatureInternal(LuaState luaState) private int GetConstructorSignatureInternal(LuaState luaState) //-V3009
{ {
ProxyType klass = null; ProxyType klass = null;
int udata = luaState.CheckUObject(1, "luaNet_class"); int udata = luaState.CheckUObject(1, "luaNet_class");
...@@ -955,7 +955,7 @@ namespace NLua ...@@ -955,7 +955,7 @@ namespace NLua
int newTop = luaState.GetTop(); int newTop = luaState.GetTop();
if (oldTop == newTop) if (oldTop == newTop)
return null; return new object[0];
var returnValues = new List<object>(); var returnValues = new List<object>();
for (int i = oldTop + 1; i <= newTop; i++) for (int i = oldTop + 1; i <= newTop; i++)
...@@ -975,7 +975,7 @@ namespace NLua ...@@ -975,7 +975,7 @@ namespace NLua
int newTop = luaState.GetTop(); int newTop = luaState.GetTop();
if (oldTop == newTop) if (oldTop == newTop)
return null; return new object[0];
int iTypes; int iTypes;
var returnValues = new List<object>(); var returnValues = new List<object>();
......
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using NLua.Exceptions;
using LuaState = KeraLua.Lua; using LuaState = KeraLua.Lua;
namespace NLua namespace NLua
...@@ -29,7 +29,7 @@ namespace NLua ...@@ -29,7 +29,7 @@ namespace NLua
LuaState main = luaState.MainThread; LuaState main = luaState.MainThread;
if (!translators.TryGetValue(main, out translator)) if (!translators.TryGetValue(main, out translator))
return null; throw new Exception("Invalid luaState, couldn't find ObjectTranslator");
} }
return translator; return translator;
} }
......
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
<Version>3.12.0</Version> <Version>3.12.0</Version>
</PackageReference> </PackageReference>
<PackageReference Include="NUnit3TestAdapter"> <PackageReference Include="NUnit3TestAdapter">
<Version>3.16.1</Version> <Version>3.17.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment