/*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
static const char failureNoMemory[] = "Unable to allocate memory.";
static const char failureInternal[] = "Internal error.";
+static const char failureBadAlloc[] = "Allocator returned misaligned memory.";
extern const hs_compile_error_t hs_enomem = {
const_cast<char *>(failureNoMemory), 0
extern const hs_compile_error_t hs_einternal = {
const_cast<char *>(failureInternal), 0
};
+extern const hs_compile_error_t hs_badalloc = {
+ const_cast<char *>(failureBadAlloc), 0
+};
namespace ue2 {
hs_compile_error_t *ret =
(struct hs_compile_error *)hs_misc_alloc(sizeof(hs_compile_error_t));
if (ret) {
+ hs_error_t e = hs_check_alloc(ret);
+ if (e != HS_SUCCESS) {
+ hs_misc_free(ret);
+ return const_cast<hs_compile_error_t *>(&hs_badalloc);
+ }
char *msg = (char *)hs_misc_alloc(err.size() + 1);
if (msg) {
+ e = hs_check_alloc(msg);
+ if (e != HS_SUCCESS) {
+ hs_misc_free(msg);
+ return const_cast<hs_compile_error_t *>(&hs_badalloc);
+ }
memcpy(msg, err.c_str(), err.size() + 1);
ret->message = msg;
} else {
if (!error) {
return;
}
- if (error == &hs_enomem || error == &hs_einternal) {
+ if (error == &hs_enomem || error == &hs_einternal ||
+ error == &hs_badalloc) {
// These are not allocated.
return;
}
/*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* The library was unable to allocate temporary storage used during
* compilation time.
*
+ * - *Allocator returned misaligned memory*
+ *
+ * The memory allocator (either malloc() or the allocator set with @ref
+ * hs_set_allocator()) did not correctly return memory suitably aligned
+ * for the largest representable data type on this platform.
+ *
* - *Internal error*
*
* An unexpected error occurred: if this error is reported, please contact
/*
- * Copyright (c) 2015, Intel Corporation
+ * Copyright (c) 2015-2016, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
#include "test_util.h"
#include <cstdlib>
+#include <string>
+
+using std::string;
static void *null_malloc(size_t) { return nullptr; }
hs_set_database_allocator(nullptr, nullptr);
}
+TEST(CustomAllocator, TwoAlignedCompileError) {
+ hs_set_misc_allocator(two_aligned_malloc, two_aligned_free);
+
+ hs_database_t *db = nullptr;
+ hs_compile_error_t *compile_err = nullptr;
+ const hs_platform_info_t *platform = nullptr;
+ hs_error_t err =
+ hs_compile("\\1", 0, HS_MODE_BLOCK, platform, &db, &compile_err);
+ ASSERT_EQ(HS_COMPILER_ERROR, err);
+ ASSERT_EQ(nullptr, db);
+ ASSERT_NE(nullptr, compile_err);
+ EXPECT_STREQ("Allocator returned misaligned memory.", compile_err->message);
+ hs_free_compile_error(compile_err);
+ hs_set_database_allocator(nullptr, nullptr);
+}
+
TEST(CustomAllocator, TwoAlignedDatabaseInfo) {
hs_database_t *db = buildDB("foobar", 0, 0, HS_MODE_BLOCK);
ASSERT_TRUE(db != nullptr);
hs_set_scratch_allocator(nullptr, nullptr);
hs_free_database(db);
}
+
+TEST(CustomAllocator, NullMallocExpressionInfo) {
+ hs_set_allocator(null_malloc, nullptr);
+
+ string pattern = "foobar";
+ hs_expr_info_t *info = nullptr;
+ hs_compile_error_t *c_err = nullptr;
+ hs_error_t err = hs_expression_info(pattern.c_str(), 0, &info, &c_err);
+ ASSERT_EQ(HS_COMPILER_ERROR, err);
+ ASSERT_NE(nullptr, c_err);
+ hs_free_compile_error(c_err);
+ hs_set_allocator(nullptr, nullptr);
+}
+
+TEST(CustomAllocator, TwoAlignedExpressionInfo) {
+ hs_set_misc_allocator(two_aligned_malloc, two_aligned_free);
+
+ string pattern = "\\1";
+ hs_expr_info_t *info = nullptr;
+ hs_compile_error_t *c_err = nullptr;
+ hs_error_t err = hs_expression_info(pattern.c_str(), 0, &info, &c_err);
+ ASSERT_EQ(HS_COMPILER_ERROR, err);
+ ASSERT_NE(nullptr, c_err);
+ EXPECT_STREQ("Allocator returned misaligned memory.", c_err->message);
+ hs_free_compile_error(c_err);
+ hs_set_allocator(nullptr, nullptr);
+}