michaelrsweet / mxml

Tiny XML library.
https://www.msweet.org/mxml
Apache License 2.0
426 stars 157 forks source link

mxmlSaveString crashes when attempting to just get length with 0-length buffer #284

Closed WaterJuice closed 2 years ago

WaterJuice commented 2 years ago

mxmlSaveString in theory can be used to determine the length of the XML string without writing to a buffer. You might want to do this first to determine the length of the buffer to allocate. If you pass it a NULL buffer with bufsize 0 it will crash, but only because it attempts to zero terminate the string at the end.

https://github.com/michaelrsweet/mxml/blob/990d4b54ea917174b16f89065fb61b1ebb5cc563/mxml-file.c#L379

This line will cause the crash. In my version I have patched this to wrap the line with if(bufsize > 0)

This prevents the crash and makes the function usable for simply determining length of string.

michaelrsweet commented 2 years ago

Will consider this for a future release, however mxmlSaveString is not documented to support passing a NULL buffer pointer. As for computing the size for allocation, that's why I provided mxmlSaveAllocString.

michaelrsweet commented 2 years ago

[master 3a4ece2] Fix mxmlSaveString with a 0-length buffer (Issue #284)