[LyX/master] DocBook: avoid using isspace in StartTag::writeTag.

Thibaut Cuvelier tcuvelier at lyx.org
Sat Sep 19 18:18:56 UTC 2020


commit 1a76fb9658cd26db9c224c0560905e6150b10efb
Author: Thibaut Cuvelier <tcuvelier at lyx.org>
Date:   Thu Sep 3 01:23:20 2020 +0200

    DocBook: avoid using isspace in StartTag::writeTag.
    
    This is the cause of crashes (on both Windows and Linux).
---
 src/output_docbook.cpp |   12 +-----------
 src/xml.cpp            |   29 ++++++++++++++++++++++++-----
 src/xml.h              |    6 ++++++
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/output_docbook.cpp b/src/output_docbook.cpp
index addf24a..ce7e641 100644
--- a/src/output_docbook.cpp
+++ b/src/output_docbook.cpp
@@ -429,16 +429,6 @@ void makeBibliography(
 }
 
 
-bool isNotOnlySpace(docstring const & str)
-{
-	for (auto const & c: str) {
-		if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
-			return true;
-	}
-	return false;
-}
-
-
 void makeParagraph(
 		Text const & text,
 		Buffer const & buf,
@@ -531,7 +521,7 @@ void makeParagraph(
 	++nextpar;
 	auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(distance(begin, par)), 0, nextpar == end, special_case);
 	for (docstring const & parXML : pars) {
-		if (isNotOnlySpace(parXML)) {
+		if (xml::isNotOnlySpace(parXML)) {
 			if (open_par)
 				openParTag(xs, &*par, prevpar);
 
diff --git a/src/xml.cpp b/src/xml.cpp
index dd9790f..8e936f8 100644
--- a/src/xml.cpp
+++ b/src/xml.cpp
@@ -108,12 +108,9 @@ docstring StartTag::writeTag() const
 {
 	docstring output = '<' + tag_;
 	if (!attr_.empty()) {
-		docstring attributes = xml::escapeString(attr_, XMLStream::ESCAPE_NONE);
-		attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(),
-                                                          [](int c) {return !std::isspace(c);}));
-		if (!attributes.empty()) {
+		docstring attributes = xml::trimLeft(xml::escapeString(attr_, XMLStream::ESCAPE_NONE));
+		if (!attributes.empty())
 			output += ' ' + attributes;
-		}
 	}
 	output += ">";
 	return output;
@@ -601,6 +598,28 @@ docstring xml::uniqueID(docstring const & label)
 }
 
 
+bool xml::isNotOnlySpace(docstring const & str)
+{
+	for (auto const & c: str) {
+		if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
+		return true;
+	}
+	return false;
+}
+
+
+docstring xml::trimLeft(docstring const & str)
+{
+	size_t i = 0;
+	for (auto const & c: str) {
+		if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
+			return str.substr(i, docstring::npos);
+		i++;
+	}
+	return str;
+}
+
+
 docstring xml::cleanID(docstring const & orig)
 {
 	// The standard xml:id only allows letters, digits, '-' and '.' in a name.
diff --git a/src/xml.h b/src/xml.h
index 52eeb22..d5b4233 100644
--- a/src/xml.h
+++ b/src/xml.h
@@ -160,6 +160,12 @@ docstring cleanID(docstring const &orig);
 /// returns a unique numeric ID
 docstring uniqueID(docstring const & label);
 
+/// determines whether a string only contains space characters
+bool isNotOnlySpace(docstring const & str);
+
+/// trims the string to the left, i.e. remove any space-like character at the beginning of the string
+docstring trimLeft(docstring const & str);
+
 struct FontTag;
 struct EndFontTag;
 


More information about the lyx-cvs mailing list