Skip to content

Add a runtime for C++, a header over the one for C - #23

Merged
xadaemon merged 2 commits into
trunkfrom
cpp-bindings
Nov 18, 2026
Merged

xadaemon merged 2 commits into
trunkfrom
cpp-bindings

Conversation

@xadaemon

Copy link
Copy Markdown
Member

packages/asnkitrt-c/include/asnkitrt.hpp is the runtime for C++17 and later: only a header, over asnkitrt.h and the same library. In two commits.

Copy the names of members and alternatives in the runtime for C

asnkit_map_set() and asnkit_value_new_choice() kept the pointer to the name they were given rather than a copy of it, so a name in a buffer that was then freed or used again left a value that read freed memory. It was so from 2.0.0-rc1 on, and names that are string literals were not affected. The temporaries of C++ are what showed it. tests/api.c gives the names in a buffer that it then writes over.

Add a runtime for C++, a header over the one for C

#include "asnkitrt.hpp"

auto spec = asnkit::spec::load_file("types.asni", "der");
auto question = asnkit::value::map({{"id", 1}, {"question", "Is 1+1=3?"}});
auto encoded = spec.encode("Question", question);
auto decoded = spec.decode("Question", encoded);

decoded["question"].as_text();   // a std::string_view
  • Ownership: asnkit::spec and asnkit::value free what they hold. A value is copied deeply and moved cheaply, and what an accessor gives is a view: asnkit::value_view, std::string_view, asnkit::bytes_view.
  • Errors: an error is thrown as an asnkit::error, of a class for each status, and asnkit::decode_error has the offset. The error of a range is asnkit::asn_range_error, so as not to be std::range_error where both namespaces are used.
  • asnkit_last_error_status(): new in asnkitrt.h, the status of the last error of the thread. It tells which error a function that returned a NULL handle had, and the header for C++ throws by it.
  • Tests: tests/c.rs compiles and runs tests/api.cpp and examples/question.cpp as C++17 with -Wall -Wextra -Werror -pedantic, and checks the header as C++20 and C++23. tests/header.rs holds asnkitrt.hpp to use every function of asnkitrt.h. The job runtime-c links the example of C++ with the static library and runs it.
  • Documentation: the READMEs, docs/, CHANGELOG.md and the skill tell of it.

Checks

  • cargo fmt --all -- --check, cargo clippy -p asnkitrt-c --all-targets -- -D warnings and cargo test -p asnkitrt-c pass with Apple clang, and mkdocs build --strict passes.
  • GCC 16.2.1 on Linux: the tests, the header as C++17, C++20, C++23 and C++26, -O2, and -Wshadow -Wconversion -Wold-style-cast -Wuseless-cast -Wnull-dereference -Wduplicated-cond -Wlogical-op with no diagnostics.
  • GCC 13.3 and 14.2 on Ubuntu 24.04: the tests and the static link, before range_error was named asn_range_error.
  • ASan and UBSan with clang and with GCC, LeakSanitizer on Linux and leaks --atExit on macOS: nothing.

MSVC has not compiled it. Text as char8_t and bytes as std::byte are not taken.

asnkit_map_set() and asnkit_value_new_choice() kept the pointer to the
name they were given rather than a copy of it. A program that gave a
name in a buffer it then freed or used again had a value with a name
that was freed or changed, and reading or encoding it read freed
memory. Names that are string literals were not affected. It was so
from 2.0.0-rc1 on.

tests/api.c gives the names in a buffer that it then writes over.

Signed-off-by: Matheus Xavier <mxavier@boxtheta.com>
include/asnkitrt.hpp is for C++17 and later, and has no library of its
own: it is only a header over asnkitrt.h. asnkit::spec and
asnkit::value free what they hold, a value is copied deeply and moved
cheaply, what an accessor gives is a view (asnkit::value_view,
std::string_view, asnkit::bytes_view), and an error is thrown as an
asnkit::error, of a class for each status. The error of a range is
asnkit::asn_range_error, so as not to be std::range_error where both
namespaces are used.

asnkit_last_error_status() is new in asnkitrt.h: the status of the
last error of the thread, which tells which error a function that
returned a NULL handle had. The header for C++ throws by it.

tests/c.rs compiles and runs tests/api.cpp and examples/question.cpp
as C++17 with -Wall -Wextra -Werror -pedantic, as it does the C, and
checks the header as C++20 and C++23. tests/header.rs holds
asnkitrt.hpp to use every function of asnkitrt.h. The job runtime-c
links the example of C++ with the static library and runs it.

The READMEs, the documentation and the skill tell of it.

Signed-off-by: Matheus Xavier <mxavier@boxtheta.com>
@xadaemon
xadaemon merged commit 215c2cb into trunk Nov 18, 2026
22 checks passed
@xadaemon
xadaemon deleted the cpp-bindings branch November 18, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant