Skip to content

Commit

Permalink
fix: Use std::nothrow to disable exceptions throwing in new opera…
Browse files Browse the repository at this point in the history
…tors (fixes #103). (#105)
  • Loading branch information
LinZhihao-723 authored Dec 8, 2024
1 parent b15c040 commit 118b45d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
14 changes: 13 additions & 1 deletion src/clp_ffi_py/ir/native/DeserializerBufferReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <new>
#include <span>

#include <clp/ErrorCode.hpp>
#include <clp/type_utils.hpp>
#include <gsl/gsl>

#include <clp_ffi_py/error_messages.hpp>
#include <clp_ffi_py/ExceptionFFI.hpp>
#include <clp_ffi_py/ir/native/PyDeserializerBuffer.hpp>
#include <clp_ffi_py/PyObjectUtils.hpp>
#include <clp_ffi_py/utils.hpp>

namespace clp_ffi_py::ir::native {
auto DeserializerBufferReader::create(PyObject* input_stream, Py_ssize_t buf_capacity)
Expand All @@ -24,7 +27,16 @@ auto DeserializerBufferReader::create(PyObject* input_stream, Py_ssize_t buf_cap
if (nullptr == py_deserializer_buffer) {
return nullptr;
}
return new DeserializerBufferReader{py_deserializer_buffer.get()};
gsl::owner<DeserializerBufferReader*> reader{new (std::nothrow
) DeserializerBufferReader{py_deserializer_buffer.get()}};
if (nullptr == reader) {
PyErr_SetString(
PyExc_RuntimeError,
get_c_str_from_constexpr_string_view(cOutOfMemoryError)
);
return nullptr;
}
return reader;
}

auto DeserializerBufferReader::try_read(char* buf, size_t num_bytes_to_read, size_t& num_bytes_read)
Expand Down
23 changes: 19 additions & 4 deletions src/clp_ffi_py/ir/native/PyDeserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "PyDeserializer.hpp"

#include <new>
#include <system_error>
#include <type_traits>
#include <utility>
Expand All @@ -15,6 +16,7 @@
#include <clp/TraceableException.hpp>

#include <clp_ffi_py/api_decoration.hpp>
#include <clp_ffi_py/error_messages.hpp>
#include <clp_ffi_py/ir/native/DeserializerBufferReader.hpp>
#include <clp_ffi_py/ir/native/error_messages.hpp>
#include <clp_ffi_py/ir/native/PyKeyValuePairLogEvent.hpp>
Expand Down Expand Up @@ -200,9 +202,17 @@ auto PyDeserializer::init(
);
return false;
}
m_deserializer = new clp::ffi::ir_stream::Deserializer<PyDeserializer::IrUnitHandler>{
std::move(deserializer_result.value())
};
m_deserializer = new (std::nothrow)
clp::ffi::ir_stream::Deserializer<PyDeserializer::IrUnitHandler>{
std::move(deserializer_result.value())
};
if (nullptr == m_deserializer) {
PyErr_SetString(
PyExc_RuntimeError,
get_c_str_from_constexpr_string_view(cOutOfMemoryError)
);
return false;
}
} catch (clp::TraceableException& exception) {
handle_traceable_exception(exception);
return false;
Expand Down Expand Up @@ -279,7 +289,12 @@ auto PyDeserializer::handle_log_event(clp::ffi::KeyValuePairLogEvent&& log_event
// deserialized log event.
clear_deserialized_log_event();
}
m_deserialized_log_event = new clp::ffi::KeyValuePairLogEvent{std::move(log_event)};
m_deserialized_log_event
= new (std::nothrow) clp::ffi::KeyValuePairLogEvent{std::move(log_event)};
if (nullptr == m_deserialized_log_event) {
// TODO: Set this to a proper error code when user-defined error code is supported.
return IRErrorCode::IRErrorCode_Eof;
}
return IRErrorCode::IRErrorCode_Success;
}

Expand Down
4 changes: 3 additions & 1 deletion src/clp_ffi_py/ir/native/PyKeyValuePairLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cstddef>
#include <cstdint>
#include <new>
#include <optional>
#include <span>
#include <stack>
Expand Down Expand Up @@ -642,7 +643,8 @@ auto decode_as_encoded_text_ast(Value const& val) -> std::optional<std::string>
} // namespace

auto PyKeyValuePairLogEvent::init(clp::ffi::KeyValuePairLogEvent kv_pair_log_event) -> bool {
m_kv_pair_log_event = new clp::ffi::KeyValuePairLogEvent{std::move(kv_pair_log_event)};
m_kv_pair_log_event
= new (std::nothrow) clp::ffi::KeyValuePairLogEvent{std::move(kv_pair_log_event)};
if (nullptr == m_kv_pair_log_event) {
PyErr_SetString(
PyExc_RuntimeError,
Expand Down
4 changes: 3 additions & 1 deletion src/clp_ffi_py/ir/native/PyLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "PyLogEvent.hpp"

#include <new>

#include <clp_ffi_py/error_messages.hpp>
#include <clp_ffi_py/ir/native/LogEvent.hpp>
#include <clp_ffi_py/ir/native/PyQuery.hpp>
Expand Down Expand Up @@ -494,7 +496,7 @@ auto PyLogEvent::init(
std::optional<std::string_view> formatted_timestamp
) -> bool {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
m_log_event = new LogEvent(log_message, timestamp, index, formatted_timestamp);
m_log_event = new (std::nothrow) LogEvent(log_message, timestamp, index, formatted_timestamp);
if (nullptr == m_log_event) {
PyErr_SetString(
PyExc_RuntimeError,
Expand Down
20 changes: 11 additions & 9 deletions src/clp_ffi_py/ir/native/PyMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "PyMetadata.hpp"

#include <new>

#include <clp_ffi_py/error_messages.hpp>
#include <clp_ffi_py/ExceptionFFI.hpp>
#include <clp_ffi_py/ir/native/Metadata.hpp>
Expand Down Expand Up @@ -228,7 +230,7 @@ auto PyMetadata::init(
char const* input_timezone
) -> bool {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
m_metadata = new Metadata(ref_timestamp, input_timestamp_format, input_timezone);
m_metadata = new (std::nothrow) Metadata(ref_timestamp, input_timestamp_format, input_timezone);
if (nullptr == m_metadata) {
PyErr_SetString(
PyExc_RuntimeError,
Expand All @@ -242,19 +244,19 @@ auto PyMetadata::init(
auto PyMetadata::init(nlohmann::json const& metadata, bool is_four_byte_encoding) -> bool {
try {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
m_metadata = new Metadata(metadata, is_four_byte_encoding);
m_metadata = new (std::nothrow) Metadata(metadata, is_four_byte_encoding);
if (nullptr == m_metadata) {
PyErr_SetString(
PyExc_RuntimeError,
get_c_str_from_constexpr_string_view(clp_ffi_py::cOutOfMemoryError)
);
return false;
}
} catch (clp_ffi_py::ExceptionFFI& ex) {
handle_traceable_exception(ex);
m_metadata = nullptr;
return false;
}
if (nullptr == m_metadata) {
PyErr_SetString(
PyExc_RuntimeError,
get_c_str_from_constexpr_string_view(clp_ffi_py::cOutOfMemoryError)
);
return false;
}
return init_py_timezone();
}

Expand Down
13 changes: 7 additions & 6 deletions src/clp_ffi_py/ir/native/PyQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "PyQuery.hpp"

#include <new>

#include <clp/string_utils/string_utils.hpp>

#include <clp_ffi_py/error_messages.hpp>
Expand Down Expand Up @@ -627,12 +629,11 @@ auto PyQuery::init(
) -> bool {
try {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
m_query = new Query(
search_time_lower_bound,
search_time_upper_bound,
wildcard_queries,
search_time_termination_margin
);
m_query = new (std::nothrow)
Query(search_time_lower_bound,
search_time_upper_bound,
wildcard_queries,
search_time_termination_margin);
if (nullptr == m_query) {
PyErr_SetString(
PyExc_RuntimeError,
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PySerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ auto PySerializer::init(
) -> bool {
m_output_stream = output_stream;
Py_INCREF(output_stream);
m_serializer = new PySerializer::ClpIrSerializer{std::move(serializer)};
m_buffer_size_limit = buffer_size_limit;
m_serializer = new (std::nothrow) PySerializer::ClpIrSerializer{std::move(serializer)};
if (nullptr == m_serializer) {
PyErr_SetString(
PyExc_RuntimeError,
Expand Down

0 comments on commit 118b45d

Please sign in to comment.