]> granicus.if.org Git - nethack/commitdiff
cppregex regex_error_desc()
authornhmall <nhmall@nethack.org>
Thu, 30 Jun 2022 16:58:19 +0000 (12:58 -0400)
committernhmall <nhmall@nethack.org>
Thu, 30 Jun 2022 17:02:07 +0000 (13:02 -0400)
Address sanitizer caught a use after free.
cppregex.cpp regex_error_desc() was not returning a pointer
to a static buffer, yet the posixregex was. Follow suit.

sys/share/cppregex.cpp

index c3ce58ced41c5e73c28593b219eeb3ecf5ced1b0..0e3a7de1a923d16a3c25b827a5c7f14a85f91c46 100644 (file)
@@ -12,6 +12,7 @@ extern "C" {
   #include <hack.h>
 
   extern const char regex_id[] = "cppregex";
+  static char cppregex_static_buffer[BUFSZ];
 
   struct nhregex {
     std::unique_ptr<std::regex> re;
@@ -39,10 +40,12 @@ extern "C" {
   }
 
   const char *regex_error_desc(struct nhregex *re) {
-    if (re->err)
-      return re->err->what();
-    else
-      return nullptr;
+      if (re->err) {
+          Snprintf(cppregex_static_buffer, sizeof cppregex_static_buffer,
+                   "%s", re->err->what());
+          return cppregex_static_buffer;
+      } else
+          return nullptr;
   }
 
   boolean regex_match(const char *s, struct nhregex *re) {