fix: actually return int instead of bool (#5651)

This commit is contained in:
Mayukha Vadari
2025-08-15 13:58:38 -04:00
committed by GitHub
parent 647b47567e
commit 77875c9133
5 changed files with 31 additions and 26 deletions

View File

@@ -1442,15 +1442,15 @@ struct HostFuncImpl_test : public beast::unit_test::suite
BEAST_EXPECT(compareKeylet(actual.value(), expected)); \
} \
}
#define COMPARE_KEYLET_FAIL(hfsFunc, keyletFunc, expected, ...) \
{ \
auto actual = hfs.hfsFunc(__VA_ARGS__); \
if (BEAST_EXPECT(!actual.has_value())) \
{ \
BEAST_EXPECTS( \
actual.error() == expected, \
std::to_string(static_cast<int32_t>(actual.error()))); \
} \
#define COMPARE_KEYLET_FAIL(hfsFunc, keyletFunc, expected, ...) \
{ \
auto actual = hfs.hfsFunc(__VA_ARGS__); \
if (BEAST_EXPECT(!actual.has_value())) \
{ \
BEAST_EXPECTS( \
actual.error() == expected, \
std::to_string(HfErrorToInt(actual.error()))); \
} \
}
// accountKeylet

View File

@@ -376,9 +376,11 @@ struct Wasm_test : public beast::unit_test::suite
BadTestHostFunctions nfs(env);
auto re =
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100000);
BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 5831));
// std::cout << "bad case (access nonexistent field) result "
// << re.error() << std::endl;
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECT(re->result == -201);
BEAST_EXPECT(re->cost == 5831);
}
}
{ // fail because trying to allocate more than MAX_PAGES memory
@@ -396,9 +398,11 @@ struct Wasm_test : public beast::unit_test::suite
BadTestHostFunctions nfs(env);
auto re =
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100'000);
BEAST_EXPECT(re.has_value() && !re->result && (re->cost == 5831));
// std::cout << "bad case (more than MAX_PAGES) result "
// << re.error() << std::endl;
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECT(re->result == -201);
BEAST_EXPECT(re->cost == 5831);
}
}
{ // fail because recursion too deep

View File

@@ -54,6 +54,12 @@ enum class HostFunctionError : int32_t {
FLOAT_COMPUTATION_ERROR = -20,
};
inline int32_t
HfErrorToInt(HostFunctionError e)
{
return static_cast<int32_t>(e);
}
std::string
floatToString(Slice const& data);

View File

@@ -41,22 +41,18 @@ setData(
return 0; // LCOV_EXCL_LINE
if (dst < 0 || dstSize < 0 || !src || srcSize < 0)
return static_cast<std::underlying_type_t<HostFunctionError>>(
HostFunctionError::INVALID_PARAMS);
return HfErrorToInt(HostFunctionError::INVALID_PARAMS);
auto memory = runtime ? runtime->getMem() : wmem();
// LCOV_EXCL_START
if (!memory.s)
return static_cast<std::underlying_type_t<HostFunctionError>>(
HostFunctionError::NO_MEM_EXPORTED);
return HfErrorToInt(HostFunctionError::NO_MEM_EXPORTED);
// LCOV_EXCL_STOP
if (dst + dstSize > memory.s)
return static_cast<std::underlying_type_t<HostFunctionError>>(
HostFunctionError::POINTER_OUT_OF_BOUNDS);
return HfErrorToInt(HostFunctionError::POINTER_OUT_OF_BOUNDS);
if (srcSize > dstSize)
return static_cast<std::underlying_type_t<HostFunctionError>>(
HostFunctionError::BUFFER_TOO_SMALL);
return HfErrorToInt(HostFunctionError::BUFFER_TOO_SMALL);
memcpy(memory.p + dst, src, srcSize);
@@ -212,8 +208,7 @@ hfResult(wasm_val_vec_t* results, int32_t value)
std::nullptr_t
hfResult(wasm_val_vec_t* results, HostFunctionError value)
{
results->data[0] = WASM_I32_VAL(
static_cast<std::underlying_type_t<HostFunctionError>>(value));
results->data[0] = WASM_I32_VAL(HfErrorToInt(value));
results->num_elems = 1;
return nullptr;
}

View File

@@ -150,7 +150,7 @@ runEscrowWasm(
std::cout << ", ret: " << ret->result << ", gas spent: " << ret->cost
<< std::endl;
#endif
return EscrowResult{ret->result > 0, ret->cost};
return EscrowResult{ret->result, ret->cost};
}
NotTEC