Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary nokogiri gems on musl based Linux #1990

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 6 additions & 17 deletions ext/nokogiri/xml_reader.c
Original file line number Diff line number Diff line change
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how much your code deletes! But this seems like a wide departure from the original, with extra string concatenation for something with known size. EIther the string should be allocated ahead of time (which doesn't look that easy to do with cruby's API), or the concatenation should be avoided (easy). Here's the (rough + untested) cruby equivalent to the original code + your encoding differences:

if (ns->prefix) {
  key = rb_enc_sprintf(rb_utf8_encoding(), "%s:%s", XMLNS_PREFIX, ns->prefix);
} else {
  key = rb_enc_sprintf(rb_utf8_encoding(), "%s", XMLNS_PREFIX);
}
key = rb_str_conv_enc(key, rb_utf8_encoding(), rb_default_internal_encoding());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @zenspider for reviewing! rb_enc_str_new_cstr() allocates a struct RString with an embedded string length of 23 bytes at maximum. So each string has a capacity of 23 ASCII characters from the start. Since namespace keys tend to be shorter, usually no reallocation is required. Given that sprintf is usually slower due to format string interpretation, I think the proposed implementation is best how it is.

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
2 changes: 1 addition & 1 deletion test/html/test_document_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_encoding
end

def test_encoding_without_charset
doc = Nokogiri::HTML File.open(SHIFT_JIS_NO_CHARSET, 'r:cp932:cp932').read
doc = Nokogiri::HTML File.open(SHIFT_JIS_NO_CHARSET, 'r:Shift_JIS:Shift_JIS').read

hello = "こんにちは"

Expand Down