From e997ad74ae256937019e9c1c1315c40b25ed7f53 Mon Sep 17 00:00:00 2001 From: dmiller Date: Thu, 18 Jun 2026 23:26:07 +0000 Subject: [PATCH] Catch C++ exceptions at the top level --- main.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.cc b/main.cc index 7b12bad1c..a9bab3223 100644 --- a/main.cc +++ b/main.cc @@ -63,6 +63,7 @@ #include #include +#include #include "nmap.h" #include "NmapOps.h" @@ -139,6 +140,7 @@ int main(int argc, char *argv[]) { mtrace(); #endif + try { if ((cptr = getenv("NMAP_ARGS"))) { if (Snprintf(command, sizeof(command), "nmap %s", cptr) >= (int) sizeof(command)) { error("Warning: NMAP_ARGS variable is too long, truncated"); @@ -168,4 +170,18 @@ int main(int argc, char *argv[]) { } return nmap_main(argc, argv); + } + catch (const std::bad_alloc &) { + fprintf(stderr, "FATAL: Out of memory.\n"); + return 1; + } + catch (const std::exception &e) { + fprintf(stderr, "FATAL: Unhandled C++ exception (%s): %s\n", + typeid(e).name(), e.what()); + return 1; + } + catch (...) { + fprintf(stderr, "FATAL: Unhandled unknown C++ exception.\n"); + return 1; + } }