From 659c923f8b5e9a16b122827f89338f1dfd6a4203 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Fri, 7 Feb 2020 09:16:43 +0100 Subject: [PATCH] Fix binary compat with musl based Linux The binary nokogiri gem built with rake-compiler-dock-1.0.0 segfaults on x86_64-linux-musl. This happens in: lib/nokogiri/xml/reader.rb:92:in `namespaces' It turned out that any calls to sprintf() with %-arguments fail. I did not dig deeper on assembly level, since this is the only use of sprintf in nokogiri. Moreover it can be replaced by calls to ruby string functions easily. Related to #1983 --- ext/nokogiri/xml_reader.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/ext/nokogiri/xml_reader.c b/ext/nokogiri/xml_reader.c index aea150d0cb..661e14cf9f 100644 --- a/ext/nokogiri/xml_reader.c +++ b/ext/nokogiri/xml_reader.c @@ -28,35 +28,24 @@ static int has_attributes(xmlTextReaderPtr reader) static void Nokogiri_xml_node_namespaces(xmlNodePtr node, VALUE attr_hash) { xmlNsPtr ns; - static char buffer[XMLNS_BUFFER_LEN] ; - char *key ; - size_t keylen ; + VALUE key; if (node->type != XML_ELEMENT_NODE) return ; ns = node->nsDef; while (ns != NULL) { - keylen = XMLNS_PREFIX_LEN + (ns->prefix ? (strlen((const char*)ns->prefix) + 1) : 0) ; - if (keylen > XMLNS_BUFFER_LEN) { - key = (char*)malloc(keylen) ; - } else { - key = buffer ; - } - + key = rb_enc_str_new_cstr(XMLNS_PREFIX, rb_utf8_encoding()); if (ns->prefix) { - sprintf(key, "%s:%s", XMLNS_PREFIX, ns->prefix); - } else { - sprintf(key, "%s", XMLNS_PREFIX); + rb_str_cat_cstr(key, ":"); + rb_str_cat_cstr(key, (const char*)ns->prefix); } + key = rb_str_conv_enc(key, rb_utf8_encoding(), rb_default_internal_encoding()); rb_hash_aset(attr_hash, - NOKOGIRI_STR_NEW2(key), + key, (ns->href ? NOKOGIRI_STR_NEW2(ns->href) : Qnil) ); - if (key != buffer) { - free(key); - } ns = ns->next ; } }