Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#include <limits.h>
}

#include "../intl_cppshims.h"
#include "../intl_convertcpp.h"
#include "../intl_common.h"

Expand Down Expand Up @@ -138,7 +139,10 @@ U_CFUNC PHP_METHOD(IntlRuleBasedBreakIterator, getRuleStatusVec)
ZEND_ASSERT(BREAKITER_ERROR_CODE(bio) == U_BUFFER_OVERFLOW_ERROR);
BREAKITER_ERROR_CODE(bio) = U_ZERO_ERROR;

std::unique_ptr<int32_t[]> rules = std::unique_ptr<int32_t[]>(new int32_t[num_rules]);
int32_t *r = zend_mm_safe_alloc<int32_t *>(static_cast<size_t>(num_rules), sizeof(int32_t));

std::unique_ptr<int32_t[], decltype(&zend_mm_destructor<int32_t>)> rules(r, zend_mm_destructor);

num_rules = fetch_rbbi(bio)->getRuleStatusVec(rules.get(), num_rules,
BREAKITER_ERROR_CODE(bio));
if (U_FAILURE(BREAKITER_ERROR_CODE(bio))) {
Expand Down
28 changes: 28 additions & 0 deletions ext/intl/intl_cppshims.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,32 @@
#define _MSC_STDINT_H_ 1
#endif

#include <type_traits>

template<typename T, typename S = std::size_t, typename std::enable_if<std::is_integral<S>::value && std::is_unsigned<S>::value, int>::type = 0>
T zend_mm_fast_alloc(S len) {
return static_cast<T>(emalloc(len));
}

// we try to provide enough flexibility for nm and size types as long they re unsigned together
template<typename T, typename N, typename S,
typename std::enable_if<std::is_integral<N>::value && std::is_unsigned<N>::value && std::is_integral<S>::value && std::is_unsigned<S>::value, int>::type = 0>
T zend_mm_safe_alloc(N num, S len, S offset = 0) {
return static_cast<T>(safe_emalloc(num, len, offset));
}

template<typename T, typename std::enable_if<std::is_class<T>::value, int>::type = 0>
void zend_mm_destructor(T *inst) {
if (inst) {
inst->~T();
efree(inst);
}
}

template<typename T, typename std::enable_if<!std::is_class<T>::value, int>::type = 0>
void zend_mm_destructor(T *ptr) {
if (ptr) {
efree(ptr);
}
}
#endif