From 6669aafa173f750e02f43c1ef49d09ca0a963b91 Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Fri, 28 Apr 2023 07:44:18 +0000 Subject: [PATCH] dokan: handle std::stoul exceptions We're using std::stoul to parse cli args, however we aren't catching the exceptions. This change will handle the exceptions and log the according error message. For consistency, we'll use the std conversion functions throughout ceph-dokan. Signed-off-by: Lucian Petrut --- src/dokan/options.cc | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/dokan/options.cc b/src/dokan/options.cc index 4c968e13af5..1ed90ef9d34 100644 --- a/src/dokan/options.cc +++ b/src/dokan/options.cc @@ -122,10 +122,21 @@ int parse_args( cfg->win_vol_name = to_wstring(win_vol_name); } else if (ceph_argparse_witharg(args, i, &win_vol_serial, "--win-vol-serial", (char *)NULL)) { - cfg->win_vol_serial = std::stoul(win_vol_serial); + try { + cfg->win_vol_serial = std::stoul(win_vol_serial); + } catch (std::logic_error&) { + *err_msg << "ceph-dokan: invalid volume serial number: " << win_vol_serial; + return -EINVAL; + } } else if (ceph_argparse_witharg(args, i, &max_path_len, "--max-path-len", (char*)NULL)) { - unsigned long max_path_length = std::stoul(max_path_len); + unsigned long max_path_length = 0; + try { + max_path_length = std::stoul(max_path_len); + } catch (std::logic_error&) { + *err_msg << "ceph-dokan: invalid maximum path length: " << max_path_len; + return -EINVAL; + } if (max_path_length > 32767) { *err_msg << "ceph-dokan: maximum path length should not " @@ -141,18 +152,31 @@ int parse_args( cfg->max_path_len = max_path_length; } else if (ceph_argparse_witharg(args, i, &file_mode, "--file-mode", (char *)NULL)) { - mode_t mode = strtol(file_mode.c_str(), NULL, 8); + mode_t mode; + try { + mode = std::stol(file_mode, nullptr, 8); + } catch (std::logic_error&) { + *err_msg << "ceph-dokan: invalid file access mode: " << file_mode; + return -EINVAL; + } + if (!std::regex_match(file_mode, std::regex("^[0-7]{3}$")) || mode < 01 || mode > 0777) { - *err_msg << "ceph-dokan: invalid file access mode"; + *err_msg << "ceph-dokan: invalid file access mode: " << file_mode; return -EINVAL; } cfg->file_mode = mode; } else if (ceph_argparse_witharg(args, i, &dir_mode, "--dir-mode", (char *)NULL)) { - mode_t mode = strtol(dir_mode.c_str(), NULL, 8); + mode_t mode; + try { + mode = std::stol(dir_mode, nullptr, 8); + } catch (std::logic_error&) { + *err_msg << "ceph-dokan: invalid directory access mode: " << dir_mode; + return -EINVAL; + } if (!std::regex_match(dir_mode, std::regex("^[0-7]{3}$")) || mode < 01 || mode > 0777) { - *err_msg << "ceph-dokan: invalid directory access mode"; + *err_msg << "ceph-dokan: invalid directory access mode: " << dir_mode; return -EINVAL; } cfg->dir_mode = mode; -- 2.39.5