@@ -299,6 +299,13 @@ void static inline attr_alloc(attr_buffer* b, int ask)
299299 b->alloc = buf2;
300300}
301301
302+void set_error(document* d, char* msg)
303+{
304+ if (d->error_message != NULL) return; // keep the first error message
305+ d->error_message = malloc(strlen(msg)+1);
306+ strcpy(d->error_message, msg);
307+}
308+
302309// you now expect a name, perhaps preceeded by whitespace
303310// the name may be empty
304311str static inline parse_name(document* d)
@@ -326,7 +333,7 @@ str static inline parse_attrval(document* d)
326333 int start = doc_position(d);
327334 if (!find(d, c))
328335 {
329− d->error_message = _strdup("Couldn't find closing attribute bit");
336+ set_error(d, "Couldn't find closing attribute bit");
330337 return start_length(0, 0);
331338 }
332339 skip(d, 1);
@@ -385,7 +392,7 @@ void static inline parse_tag(document* d)
385392 }
386393 else if (c != '>')
387394 {
388− d->error_message = _strdup("Gunk at the end of the tag");
395+ set_error(d, "Gunk at the end of the tag");
389396 return;
390397 }
391398 d->nodes.nodes[me].inner.start = doc_position(d);
@@ -408,10 +415,10 @@ void static inline parse_tag(document* d)
408415 return;
409416 }
410417 }
411− d->error_message = _strdup("Mismatch in closing tags");
418+ set_error(d, "Mismatch in closing tags");
412419 return;
413420 }
414− d->error_message = _strdup("Weirdness when trying to close tags");
421+ set_error(d, "Weirdness when trying to close tags");
415422}
416423
417424// Parser until </, return the index of your node children
@@ -483,7 +490,7 @@ document* document_parse(char* s, int slen)
483490
484491 if (d->cursor < d->end && d->error_message == NULL)
485492 {
486− d->error_message = _strdup("Trailing junk at the end of the document");
493+ set_error(d, "Trailing junk at the end of the document");
487494 }
488495 return d;
489496}