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 hangs reading xml #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ static int configListen(WebdavdConfiguration * config, xmlTextReaderPtr reader,
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT
&& !strcmp(xmlTextReaderConstNamespaceUri(reader),
CONFIG_NAMESPACE)) {
if (!strcmp(xmlTextReaderConstLocalName(reader), "port")) {
const char * elementName = xmlTextReaderConstLocalName(reader);
if (!strcmp(elementName, "port")) {
result = readConfigInt(reader, &config->daemons[index].port, configFile);
} else if (!strcmp(xmlTextReaderConstLocalName(reader), "host")) {
} else if (!strcmp(elementName, "host")) {
result = readConfigString(reader, &config->daemons[index].host);
} else if (!strcmp(xmlTextReaderConstLocalName(reader), "encryption")) {
} else if (!strcmp(elementName, "encryption")) {
const char * encryptionString;
result = stepOverText(reader, &encryptionString);
if (encryptionString) {
Expand All @@ -104,7 +105,7 @@ static int configListen(WebdavdConfiguration * config, xmlTextReaderPtr reader,
}
xmlFree((char *) encryptionString);
}
} else if (!strcmp(xmlTextReaderConstLocalName(reader), "forward-to")) {
} else if (!strcmp(elementName, "forward-to")) {
int depth2 = xmlTextReaderDepth(reader) + 1;
result = stepInto(reader);
config->daemons[index].forwardToIsEncrypted = -1;
Expand Down Expand Up @@ -143,6 +144,9 @@ static int configListen(WebdavdConfiguration * config, xmlTextReaderPtr reader,
config->daemons[index].forwardToPort == 443 ? 1 : 0;
}
}
} else {
stdLogError(0, "Unknown element tag '%s' in listen section", elementName);
result = stepOver(reader);
}
} else {
result = stepOver(reader);
Expand Down
20 changes: 10 additions & 10 deletions xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,54 @@ int stepInto(xmlTextReaderPtr reader) {
int result;
do {
result = xmlTextReaderRead(reader);
} while (result
} while (result > 0
&& (xmlTextReaderNodeType(reader) == XML_READER_TYPE_SIGNIFICANT_WHITESPACE
|| xmlTextReaderNodeType(reader) == XML_READER_TYPE_COMMENT
|| xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT));
return result;
return result > 0;
}

int stepOver(xmlTextReaderPtr reader) {
int depth = xmlTextReaderDepth(reader);
int result;
do {
result = xmlTextReaderRead(reader);
} while (result && xmlTextReaderDepth(reader) > depth);
while (result
} while (result > 0 && xmlTextReaderDepth(reader) > depth);
while (result > 0
&& (xmlTextReaderNodeType(reader) == XML_READER_TYPE_SIGNIFICANT_WHITESPACE
|| xmlTextReaderNodeType(reader) == XML_READER_TYPE_COMMENT
|| xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)) {
result = xmlTextReaderRead(reader);
}
return result;
return result > 0;
}

int stepOut(xmlTextReaderPtr reader) {
int depth = xmlTextReaderDepth(reader) - 1;
int result;
do {
result = xmlTextReaderRead(reader);
} while (result && xmlTextReaderDepth(reader) > depth);
while (result
} while (result > 0 && xmlTextReaderDepth(reader) > depth);
while (result > 0
&& (xmlTextReaderNodeType(reader) == XML_READER_TYPE_SIGNIFICANT_WHITESPACE
|| xmlTextReaderNodeType(reader) == XML_READER_TYPE_COMMENT
|| xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)) {
result = xmlTextReaderRead(reader);
}
return result;
return result > 0;
}

int stepOverText(xmlTextReaderPtr reader, const char ** text) {
int depth = xmlTextReaderDepth(reader);
int result = stepInto(reader);
*text = NULL;
if (result && xmlTextReaderDepth(reader) > depth) {
if (result > 0 && xmlTextReaderDepth(reader) > depth) {
if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) {
*text = xmlTextReaderValue(reader);
}
result = stepOut(reader);
}
return result;
return result > 0;
Copy link
Owner

@couling couling Jun 10, 2021

Choose a reason for hiding this comment

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

I don't know why I missed error handling in this code, that's a bit of a mystery.

However the fix shouldn't deviate from the return codes in libxml2:

Returns: 1 if the node was read successfully, 0 if there is no more nodes to read, or -1 in case of error

In other words 0 means EOF and -1 mean error. For the line return result > 0 fixes the immediate problem of other calls failing to check error, the fix should really be to simply return result and fix those other lines. That lets the code exit(1) at an appropriate point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The existing code handles it ok, it'll skip the problem section and continue parsing. If this returns -1, then all the code using this needs to be updated too, so it detects errors and handles them specially.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah. Sadly that's what it needs. :-(
I'll try to assess how much damage this will do to the rest of the code. It might not be so bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tested a bunch of different errors and it's decent what happens. It doesn't abort, but the parts that aren't bad work ok and it starts. IMHO, it would be better to abort on any error and produce a clear message about it.

}

int elementMatches(xmlTextReaderPtr reader, const char * namespace, const char * nodeName) {
Expand Down