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

Fixed potential NRE

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