From 9f353c7848b62a9f119725c497c21c31d1ac3b77 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Wed, 17 Jan 2024 11:43:32 -0500 Subject: [PATCH] Check string is nonempty before indexing --- src/cmdstan/command_helper.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cmdstan/command_helper.hpp b/src/cmdstan/command_helper.hpp index 7e2cf38c4f..2fa3b115b4 100644 --- a/src/cmdstan/command_helper.hpp +++ b/src/cmdstan/command_helper.hpp @@ -209,9 +209,10 @@ std::pair get_basename_suffix( */ void validate_output_filename(const std::string &fname) { std::string sep = std::string(1, cmdstan::PATH_SEPARATOR); - if (fname[fname.size() - 1] == PATH_SEPARATOR - || boost::algorithm::ends_with(fname, "..") - || boost::algorithm::ends_with(fname, sep + ".")) { + if (!fname.empty() + && (fname[fname.size() - 1] == PATH_SEPARATOR + || boost::algorithm::ends_with(fname, "..") + || boost::algorithm::ends_with(fname, sep + "."))) { std::stringstream msg; msg << "Ill-formed output filename " << fname << std::endl; throw std::invalid_argument(msg.str());