From 6ae2d5c4876ba82d1e08a16163a86128c17dc9db Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 2 Dec 2024 10:42:50 -0500 Subject: Avoid snprintf when writing MIDI events --- NEWS | 3 ++- src/sratom.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 13548b7..7b3ebdd 100644 --- a/NEWS +++ b/NEWS @@ -1,10 +1,11 @@ sratom (0.6.17) unstable; urgency=medium * Add lint option with project metadata and code formatting tests + * Avoid snprintf when writing MIDI events * Enable clang nullability checks * Fix library current_version on MacOS - -- David Robillard Sun, 06 Oct 2024 17:37:06 +0000 + -- David Robillard Mon, 02 Dec 2024 15:41:31 +0000 sratom (0.6.16) stable; urgency=medium diff --git a/src/sratom.c b/src/sratom.c index 579f42f..b51be47 100644 --- a/src/sratom.c +++ b/src/sratom.c @@ -237,6 +237,8 @@ sratom_write(Sratom* sratom, uint32_t size, const void* body) { + static const char hex_chars[] = "0123456789ABCDEF"; + const char* const type = unmap->unmap(unmap->handle, type_urid); const uint8_t idbuf[12] = "b0000000000"; SerdNode id = serd_node_from_string(SERD_BLANK, idbuf); @@ -323,13 +325,12 @@ sratom_write(Sratom* sratom, new_node = true; datatype = serd_node_from_string(SERD_URI, USTR(LV2_MIDI__MidiEvent)); - const size_t len = (size_t)size * 2U; - uint8_t* const str = (uint8_t*)calloc(len + 1, 1); - for (uint32_t i = 0; i < size; ++i) { - snprintf((char*)str + ((size_t)2 * i), - len - ((size_t)2 * i) + 1U, - "%02X", - (unsigned)*((const uint8_t*)body + i)); + const size_t len = (size_t)size * 2U; + char* const str = (char*)calloc(len + 1, 1); + for (uint32_t i = 0U; i < size; ++i) { + const uint8_t byte = ((const uint8_t*)body)[i]; + str[2U * i] = hex_chars[byte >> 4U]; + str[2U * i + 1U] = hex_chars[byte & 0x0FU]; } object = serd_node_from_string(SERD_LITERAL, USTR(str)); -- cgit v1.2.1