Skip to content

Commit

Permalink
Fix binary compat with musl based Linux
Browse files Browse the repository at this point in the history
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
  • Loading branch information
larskanis authored and flavorjones committed Mar 30, 2020
1 parent ff99fa5 commit a3d9438
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions ext/nokogiri/xml_reader.c
Expand Up @@ -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 ;
}
}
Expand Down

0 comments on commit a3d9438

Please sign in to comment.