Skip to content

Commit c0d2051

Browse files
committed
upgrade Expects/Ensures to use std::format
1 parent 94db2b3 commit c0d2051

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

api/expects

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,42 @@
3232
#endif
3333

3434
#include <os.hpp>
35-
inline void __expect_fail(const char *expr, const char *file, int line, const char *func){
35+
inline void __expect_emit_failure(std::string_view msg, std::string_view panic_text) {
3636
#ifndef UNITTESTS
3737
#ifdef INCLUDEOS_SMP_ENABLE
3838
SMP::global_lock();
3939
#endif
40-
fprintf(stderr, "%s:%i:%s %s \n",file, line, func, expr);
40+
std::fprintf(stderr, "%.*s\n", int(msg.size()), msg.data());
4141
fflush(NULL);
4242
#ifdef INCLUDEOS_SMP_ENABLE
4343
SMP::global_unlock();
4444
#endif
45-
os::panic(expr);
45+
os::panic(std::string(panic_text).c_str());
4646
#else // TEST
4747
// throw here to allow tests to capture the error
4848
#include <stdexcept>
49-
#include <format>
50-
auto msg = std::format("{}:{}:{} {}",file, line, func, expr);
5149
throw std::runtime_error(msg);
5250
#endif
5351
}
5452

55-
#define Expects(x) ((void)((x) || (__expect_fail("Expects failed: "#x, __FILE__, __LINE__, __func__),0)))
56-
#define Ensures(x) ((void)((x) || (__expect_fail("Ensures failed: "#x, __FILE__, __LINE__, __func__),0)))
53+
template <class... Args>
54+
inline void __expect_failf(const char *err_prefix, const char * /*cond*/, const char *file, int line, const char *func, std::format_string<Args...> fmt, Args&&... args){
55+
auto reason_msg = std::format(fmt, std::forward<Args>(args)...);
56+
auto error_msg = std::format("{}:{}:{}: {}: {}", file, line, func, err_prefix, reason_msg);
57+
__expect_emit_failure(error_msg, reason_msg);
58+
}
59+
60+
inline void __expect_failf(const char *err_prefix, const char *cond, const char *file, int line, const char *func){
61+
auto reason_msg = std::format("{}: {}", err_prefix, cond);
62+
auto error_msg = std::format("{}:{}:{}: {}", file, line, func, err_prefix);
63+
__expect_emit_failure(error_msg, reason_msg);
64+
}
65+
66+
#define Expects(cond) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__),0)))
67+
#define Ensures(cond) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__),0)))
68+
69+
#define Expectsf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0)))
70+
#define Ensuresf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0)))
5771

5872
namespace os {
5973
// parameter for noexcept specifier when bypassing noexcept for testing

src/musl/mmap.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ static void* sys_mmap(void * addr, size_t length, int /*prot*/, int flags,
6060

6161
// TODO: Implement minimal functionality to be POSIX compliant
6262
// https://pubs.opengroup.org/onlinepubs/009695399/functions/mmap.html
63+
if (length <= 0) {
64+
Expectsf(false, "Must always allocate at least 1 byte. Got {}", length);
65+
errno = EINVAL;
66+
return MAP_FAILED;
67+
}
6368

6469
if (fd > -1) {
6570
// None of our file systems support memory mapping at the moment

0 commit comments

Comments
 (0)