Skip to content

Commit

Permalink
introduced checked variant of strdup()
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Nov 1, 2023
1 parent 3c27ce8 commit 176e4f7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 0 additions & 2 deletions include/nanobind/nb_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
# define NB_INLINE __forceinline
# define NB_INLINE_LAMBDA
# define NB_NOINLINE __declspec(noinline)
# define NB_STRDUP _strdup
#else
# define NB_EXPORT __attribute__ ((visibility("default")))
# define NB_IMPORT NB_EXPORT
Expand All @@ -39,7 +38,6 @@
#else
# define NB_INLINE_LAMBDA
#endif
# define NB_STRDUP strdup
#endif

#if defined(__GNUC__)
Expand Down
6 changes: 3 additions & 3 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ python_error::python_error(const python_error &e)
Py_INCREF(m_value);
}
if (e.m_what)
m_what = NB_STRDUP(e.m_what);
m_what = detail::strdup_check(e.m_what);
}

python_error::python_error(python_error &&e) noexcept
Expand Down Expand Up @@ -95,7 +95,7 @@ python_error::python_error(const python_error &e)
Py_XINCREF(m_traceback);
}
if (e.m_what)
m_what = NB_STRDUP(e.m_what);
m_what = detail::strdup_check(e.m_what);
}

python_error::python_error(python_error &&e) noexcept
Expand Down Expand Up @@ -147,7 +147,7 @@ const char *python_error::what() const noexcept {
#if defined(Py_LIMITED_API) || defined(PYPY_VERSION)
object mod = module_::import_("traceback"),
result = mod.attr("format_exception")(exc_type, exc_value, exc_traceback);
m_what = NB_STRDUP(borrow<str>(str("\n").attr("join")(result)).c_str());
m_what = detail::strdup_check(borrow<str>(str("\n").attr("join")(result)).c_str());
#else
buf.clear();
if (exc_traceback.is_valid()) {
Expand Down
14 changes: 13 additions & 1 deletion src/nb_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ void *malloc_check(size_t size) {
return ptr;
}

char *strdup_check(const char *s) {
char *result;
#if defined(_WIN32)
result = _strdup(s);
#else
result = strdup(s);
#endif
if (!result)
fail("nanobind: strdup() failed!");
return result;
}

/**
* \brief Wrap a C++ function into a Python function object
*
Expand Down Expand Up @@ -1108,7 +1120,7 @@ NB_NOINLINE char *type_name(const std::type_info *t) {
int status = 0;
char *name = abi::__cxa_demangle(name_in, nullptr, nullptr, &status);
#else
char *name = NB_STRDUP(name_in);
char *name = strdup_check(name_in);
strexc(name, "class ");
strexc(name, "struct ");
strexc(name, "enum ");
Expand Down
2 changes: 2 additions & 0 deletions src/nb_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,7 @@ template <typename T> struct scoped_pymalloc {
T *ptr{ nullptr };
};

extern char *strdup_check(const char *);

NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)
4 changes: 2 additions & 2 deletions src/nb_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ static int nb_type_init(PyObject *self, PyObject *args, PyObject *kwds) {
t->flags |= (uint32_t) type_flags::is_python_type;
t->flags &= ~((uint32_t) type_flags::has_implicit_conversions);
PyObject *name = nb_type_name(self);
t->name = NB_STRDUP(PyUnicode_AsUTF8AndSize(name, nullptr));
t->name = strdup_check(PyUnicode_AsUTF8AndSize(name, nullptr));
Py_DECREF(name);
t->type_py = (PyTypeObject *) self;
t->implicit = nullptr;
Expand Down Expand Up @@ -846,7 +846,7 @@ PyObject *nb_type_new(const type_init_data *t) noexcept {
bool base_intrusive_ptr =
tb && (tb->flags & (uint32_t) type_flags::intrusive_ptr);

char *name_copy = NB_STRDUP(name.c_str());
char *name_copy = strdup_check(name.c_str());

constexpr size_t nb_type_max_slots = 10,
nb_extra_slots = 80,
Expand Down

0 comments on commit 176e4f7

Please sign in to comment.