[librsvg: 3/5] Move attributes limit error into regular limit error




commit 918cfa43bf5396177046e031c7e3e11f50a89bae
Author: Michael Howell <michael notriddle com>
Date:   Thu Jul 21 14:38:02 2022 -0700

    Move attributes limit error into regular limit error
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/715>

 src/error.rs          | 15 +++++++++++++++
 src/limits.rs         |  8 ++++++++
 src/xml/attributes.rs | 15 +++++++--------
 src/xml/mod.rs        |  2 +-
 src/xml/xml2_load.rs  |  5 +++--
 5 files changed, 34 insertions(+), 11 deletions(-)
---
diff --git a/src/error.rs b/src/error.rs
index 1ca9bf0cc..98dc3bd77 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -436,6 +436,15 @@ pub enum ImplementationLimit {
     /// allow loading more than a certain number of elements during
     /// the initial loading process.
     TooManyLoadedElements,
+
+    /// Document exceeded the number of attributes that can be attached to
+    /// an element.
+    ///
+    /// This is here because librsvg uses u16 to address attributes. It should
+    /// be essentially impossible to actually hit this limit, because the
+    /// number of attributes that the SVG standard ascribes meaning to are
+    /// lower than this limit.
+    TooManyAttributes,
 }
 
 impl error::Error for LoadingError {}
@@ -486,6 +495,12 @@ impl fmt::Display for ImplementationLimit {
                 "cannot load more than {} XML elements",
                 limits::MAX_LOADED_ELEMENTS
             ),
+
+            ImplementationLimit::TooManyAttributes => write!(
+                f,
+                "cannot load more than {} XML attributes",
+                limits::MAX_LOADED_ELEMENTS
+            ),
         }
     }
 }
diff --git a/src/limits.rs b/src/limits.rs
index c3e6703c9..89e33da06 100644
--- a/src/limits.rs
+++ b/src/limits.rs
@@ -32,3 +32,11 @@ pub const MAX_REFERENCED_ELEMENTS: usize = 500_000;
 /// in an attempt to exhaust memory.  We don't allow loading more than
 /// this number of elements during the initial streaming load process.
 pub const MAX_LOADED_ELEMENTS: usize = 1_000_000;
+
+/// Maximum number of attributes loadable per document.
+///
+/// This is here because librsvg uses u16 to address attributes. It should
+/// be essentially impossible to actually hit this limit, because the number
+/// of attributes that the SVG standard ascribes meaning to are lower than
+/// this limit.
+pub const MAX_LOADED_ATTRIBUTES: usize = u16::MAX as usize;
diff --git a/src/xml/attributes.rs b/src/xml/attributes.rs
index bf9bd5d7a..1ef0e7616 100644
--- a/src/xml/attributes.rs
+++ b/src/xml/attributes.rs
@@ -6,6 +6,8 @@ use std::str;
 use markup5ever::{namespace_url, LocalName, Namespace, Prefix, QualName};
 use string_cache::DefaultAtom;
 
+use crate::error::{ImplementationLimit, LoadingError};
+use crate::limits;
 use crate::util::{opt_utf8_cstr, utf8_cstr};
 
 /// Type used to store attribute values.
@@ -32,11 +34,6 @@ pub struct Attributes(Box<[(QualName, AttributeValue)]>);
 /// Iterator from `Attributes.iter`.
 pub struct AttributesIter<'a>(slice::Iter<'a, (QualName, AttributeValue)>);
 
-/// Error struct returned when there are too many attributes.
-/// This libraries has a hardcoded limit of [`u16::MAX`].
-#[derive(Clone, Copy, Debug)]
-pub struct TooManyAttributesError;
-
 impl Attributes {
     #[cfg(test)]
     pub fn new() -> Attributes {
@@ -62,11 +59,13 @@ impl Attributes {
     pub unsafe fn new_from_xml2_attributes(
         n_attributes: usize,
         attrs: *const *const libc::c_char,
-    ) -> Result<Attributes, TooManyAttributesError> {
+    ) -> Result<Attributes, LoadingError> {
         let mut array = Vec::with_capacity(n_attributes);
 
-        if n_attributes > u16::MAX.into() {
-            return Err(TooManyAttributesError);
+        if n_attributes > limits::MAX_LOADED_ATTRIBUTES {
+            return Err(LoadingError::LimitExceeded(
+                ImplementationLimit::TooManyAttributes,
+            ));
         }
 
         if n_attributes > 0 && !attrs.is_null() {
diff --git a/src/xml/mod.rs b/src/xml/mod.rs
index e5b0eb2f3..6a414a736 100644
--- a/src/xml/mod.rs
+++ b/src/xml/mod.rs
@@ -33,7 +33,7 @@ mod attributes;
 mod xml2;
 mod xml2_load;
 
-pub use attributes::{AttributeIndex, Attributes, TooManyAttributesError};
+pub use attributes::{AttributeIndex, Attributes};
 
 #[derive(Clone)]
 enum Context {
diff --git a/src/xml/xml2_load.rs b/src/xml/xml2_load.rs
index 7e0d889eb..93dfda064 100644
--- a/src/xml/xml2_load.rs
+++ b/src/xml/xml2_load.rs
@@ -19,8 +19,8 @@ use crate::error::LoadingError;
 use crate::util::{cstr, opt_utf8_cstr, utf8_cstr};
 
 use super::xml2::*;
+use super::Attributes;
 use super::XmlState;
-use super::{Attributes, TooManyAttributesError};
 
 #[rustfmt::skip]
 fn get_xml2_sax_handler() -> xmlSAXHandler {
@@ -218,7 +218,8 @@ unsafe extern "C" fn sax_start_element_ns_cb(
     let attrs =
         match Attributes::new_from_xml2_attributes(nb_attributes, attributes as *const *const _) {
             Ok(attrs) => attrs,
-            Err(TooManyAttributesError) => {
+            Err(e) => {
+                xml2_parser.state.error(e);
                 let parser = xml2_parser.parser.get();
                 xmlStopParser(parser);
                 return;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]