detect bad Lua sensor name when registering (#4391)
* format * lua error on bad sensor * comment * name validation * soft check before the hard check causes a fatal error * s
This commit is contained in:
parent
dcc43ee4b4
commit
9f6ee5f359
|
@ -68,9 +68,19 @@ static int lua_getSensorByIndex(lua_State* l) {
|
||||||
return getSensor(l, static_cast<SensorType>(zeroBasedSensorIndex));
|
return getSensor(l, static_cast<SensorType>(zeroBasedSensorIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static SensorType findSensorByName(lua_State* l, const char* name) {
|
||||||
|
SensorType type = findSensorTypeByName(name);
|
||||||
|
|
||||||
|
if (l && type == SensorType::Invalid) {
|
||||||
|
luaL_error(l, "Invalid sensor type: %s", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
static int lua_getSensorByName(lua_State* l) {
|
static int lua_getSensorByName(lua_State* l) {
|
||||||
auto sensorName = luaL_checklstring(l, 1, nullptr);
|
auto sensorName = luaL_checklstring(l, 1, nullptr);
|
||||||
SensorType type = findSensorTypeByName(sensorName);
|
SensorType type = findSensorByName(l, sensorName);
|
||||||
|
|
||||||
return getSensor(l, type);
|
return getSensor(l, type);
|
||||||
}
|
}
|
||||||
|
@ -340,17 +350,46 @@ static int lua_setAirmass(lua_State* l) {
|
||||||
|
|
||||||
#endif // EFI_UNIT_TEST
|
#endif // EFI_UNIT_TEST
|
||||||
|
|
||||||
|
// TODO: PR this back in to https://github.com/gengyong/luaaa
|
||||||
|
namespace LUAAA_NS {
|
||||||
|
template<typename TCLASS, typename ...ARGS>
|
||||||
|
struct PlacementConstructorCaller<TCLASS, lua_State*, ARGS...>
|
||||||
|
{
|
||||||
|
// this speciailization passes the Lua state to the constructor as first argument, as it shouldn't
|
||||||
|
// participate in the index generation as it's not a normal parameter passed via the Lua stack.
|
||||||
|
|
||||||
|
static TCLASS * Invoke(lua_State * state, void * mem)
|
||||||
|
{
|
||||||
|
return InvokeImpl(state, mem, typename make_indices<sizeof...(ARGS)>::type());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<std::size_t ...Ns>
|
||||||
|
static TCLASS * InvokeImpl(lua_State * state, void * mem, indices<Ns...>)
|
||||||
|
{
|
||||||
|
(void)state;
|
||||||
|
return new(mem) TCLASS(state, LuaStack<ARGS>::get(state, Ns + 1)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
struct LuaSensor final : public StoredValueSensor {
|
struct LuaSensor final : public StoredValueSensor {
|
||||||
LuaSensor() : LuaSensor("Invalid") { }
|
LuaSensor() : LuaSensor(nullptr, "Invalid") { }
|
||||||
|
|
||||||
~LuaSensor() {
|
~LuaSensor() {
|
||||||
unregister();
|
unregister();
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaSensor(const char* name)
|
LuaSensor(lua_State* l, const char* name)
|
||||||
: StoredValueSensor(findSensorTypeByName(name), MS2NT(100))
|
: StoredValueSensor(findSensorByName(l, name), MS2NT(100))
|
||||||
{
|
{
|
||||||
Register();
|
// do a soft collision check to avoid a fatal error from the hard check in Register()
|
||||||
|
if (l && Sensor::hasSensor(type())) {
|
||||||
|
luaL_error(l, "Tried to create a Lua sensor of type %s, but one was already registered.", getSensorName());
|
||||||
|
} else {
|
||||||
|
Register();
|
||||||
|
efiPrintf("LUA registered sensor of type %s", getSensorName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void set(float value) {
|
void set(float value) {
|
||||||
|
@ -418,7 +457,7 @@ void configureRusefiLuaHooks(lua_State* l) {
|
||||||
|
|
||||||
LuaClass<LuaSensor> luaSensor(l, "Sensor");
|
LuaClass<LuaSensor> luaSensor(l, "Sensor");
|
||||||
luaSensor
|
luaSensor
|
||||||
.ctor<const char*>()
|
.ctor<lua_State*, const char*>()
|
||||||
.fun("set", &LuaSensor::set)
|
.fun("set", &LuaSensor::set)
|
||||||
.fun("invalidate", &LuaSensor::invalidate);
|
.fun("invalidate", &LuaSensor::invalidate);
|
||||||
|
|
||||||
|
|
|
@ -245,12 +245,13 @@ void Sensor::unregister() {
|
||||||
* todo: some sort of hashmap in the future?
|
* todo: some sort of hashmap in the future?
|
||||||
*/
|
*/
|
||||||
SensorType findSensorTypeByName(const char *name) {
|
SensorType findSensorTypeByName(const char *name) {
|
||||||
for (int i = 0;i<(int)SensorType::PlaceholderLast;i++) {
|
for (int i = 0;i<(int)SensorType::PlaceholderLast;i++) {
|
||||||
SensorType type = (SensorType)i;
|
SensorType type = (SensorType)i;
|
||||||
const char *sensorName = getSensorType(type);
|
const char *sensorName = getSensorType(type);
|
||||||
if (strEqualCaseInsensitive(sensorName, name)) {
|
if (strEqualCaseInsensitive(sensorName, name)) {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return SensorType::Invalid;
|
|
||||||
|
return SensorType::Invalid;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,4 +110,5 @@ TEST_F(SensorBasic, HasSensorMock) {
|
||||||
|
|
||||||
TEST_F(SensorBasic, FindByName) {
|
TEST_F(SensorBasic, FindByName) {
|
||||||
ASSERT_EQ(SensorType::Clt, findSensorTypeByName("Clt"));
|
ASSERT_EQ(SensorType::Clt, findSensorTypeByName("Clt"));
|
||||||
|
ASSERT_EQ(SensorType::Clt, findSensorTypeByName("cLT"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue