Browse Source

LibManual: Error on section_number 0 instead of crashing

Prior to this commit if you tried to access section 0 of the man page
(`man 0 ls`) the application would just crash with an array out of
bounds style error.

This commit just ensures that we return an explicit and helpful error
message when you try to request the 0th section, instead of crashing.
Carwyn Nelson 2 years ago
parent
commit
093cbcd606
1 changed files with 2 additions and 2 deletions
  1. 2 2
      Userland/Libraries/LibManual/SectionNode.cpp

+ 2 - 2
Userland/Libraries/LibManual/SectionNode.cpp

@@ -22,8 +22,8 @@ ErrorOr<NonnullRefPtr<SectionNode>> SectionNode::try_create_from_number(StringVi
     if (!maybe_section_number.has_value())
     if (!maybe_section_number.has_value())
         return Error::from_string_literal("Section is not a number");
         return Error::from_string_literal("Section is not a number");
     auto section_number = maybe_section_number.release_value();
     auto section_number = maybe_section_number.release_value();
-    if (section_number > number_of_sections)
-        return Error::from_string_literal("Section number too large");
+    if (section_number < 1 || section_number > number_of_sections)
+        return Error::from_string_literal("Section number is not valid");
     return sections[section_number - 1];
     return sections[section_number - 1];
 }
 }