From: Adam C. Emerson Date: Tue, 24 Mar 2026 22:22:08 +0000 (-0400) Subject: rgw: Remove VLA from `parse_mime_map_line` X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9f14fe663bbec0302e5a218209a8197b2f942683;p=ceph.git rgw: Remove VLA from `parse_mime_map_line` And make other modernizations. Signed-off-by: Adam C. Emerson --- diff --git a/src/rgw/driver/rados/rgw_tools.h b/src/rgw/driver/rados/rgw_tools.h index 1d2d7a02b99f..8312601a6a34 100644 --- a/src/rgw/driver/rados/rgw_tools.h +++ b/src/rgw/driver/rados/rgw_tools.h @@ -105,7 +105,7 @@ int rgw_stat_system_obj(const DoutPrefixProvider *dpp, RGWSI_SysObj* svc_sysobj, real_time *pmtime, uint64_t *psize, optional_yield y, std::map *pattrs = nullptr); -const char *rgw_find_mime_by_ext(std::string& ext); +std::string_view rgw_find_mime_by_ext(std::string_view ext); void rgw_filter_attrset(std::map& unfiltered_attrset, const std::string& check_prefix, std::map *attrset); diff --git a/src/rgw/rgw_rest_swift.cc b/src/rgw/rgw_rest_swift.cc index e63b12458d40..fb5902eacca2 100644 --- a/src/rgw/rgw_rest_swift.cc +++ b/src/rgw/rgw_rest_swift.cc @@ -1037,8 +1037,8 @@ int RGWPutObj_ObjStore_SWIFT::get_params(optional_yield y) suffix++; if (*suffix) { string suffix_str(suffix); - const char *mime = rgw_find_mime_by_ext(suffix_str); - if (mime) { + auto mime = rgw_find_mime_by_ext(suffix_str); + if (!mime.empty()) { s->generic_attrs[RGW_ATTR_CONTENT_TYPE] = mime; } } diff --git a/src/rgw/rgw_tools.cc b/src/rgw/rgw_tools.cc index a19c5776da00..b65c157c98d7 100644 --- a/src/rgw/rgw_tools.cc +++ b/src/rgw/rgw_tools.cc @@ -1,10 +1,12 @@ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- // vim: ts=8 sw=2 sts=2 expandtab ft=cpp -#include +#include +#include #include "common/errno.h" #include "common/safe_io.h" // for safe_read() +#include "common/split.h" #include "driver/rados/rgw_tools.h" @@ -15,33 +17,32 @@ using namespace std; -static std::map* ext_mime_map; +namespace { +std::unique_ptr>> ext_mime_map; -void parse_mime_map_line(const char *start, const char *end) +void parse_mime_map_line(std::string_view line) { - char line[end - start + 1]; - strncpy(line, start, end - start); - line[end - start] = '\0'; - char *l = line; -#define DELIMS " \t\n\r" - - while (isspace(*l)) - l++; - - char *mime = strsep(&l, DELIMS); - if (!mime) + static constexpr std::string_view delims{" \t\n\r"}; + static constexpr std::string_view wschars{" \f\n\r\t\v"}; + auto spaces = line.find_first_not_of(wschars); + if (spaces == line.npos) { return; + } + line.remove_prefix(spaces); - char *ext; - do { - ext = strsep(&l, DELIMS); - if (ext && *ext) { - (*ext_mime_map)[ext] = mime; - } - } while (ext); + auto splitline = ceph::split(line, delims); + auto iter = splitline.begin(); + if (iter == splitline.end()) { + return; + } + auto mime = *iter; + ++iter; + while (iter != splitline.end()) { + (*ext_mime_map)[std::string{*iter}] = mime; + ++iter; + } } - void parse_mime_map(const char *buf) { const char *start = buf, *end = buf; @@ -49,13 +50,13 @@ void parse_mime_map(const char *buf) while (*end && *end != '\n') { end++; } - parse_mime_map_line(start, end); + parse_mime_map_line({start, end}); end++; start = end; } } -static int ext_mime_map_init(const DoutPrefixProvider *dpp, CephContext *cct, const char *ext_map) +int ext_mime_map_init(const DoutPrefixProvider *dpp, CephContext *cct, const char *ext_map) { int fd = open(ext_map, O_RDONLY); char *buf = NULL; @@ -100,19 +101,20 @@ done: close(fd); return ret; } +} // namespace -const char *rgw_find_mime_by_ext(string& ext) +std::string_view rgw_find_mime_by_ext(std::string_view ext) { - map::iterator iter = ext_mime_map->find(ext); + auto iter = ext_mime_map->find(ext); if (iter == ext_mime_map->end()) - return NULL; + return std::string_view{}; - return iter->second.c_str(); + return iter->second; } int rgw_tools_init(const DoutPrefixProvider *dpp, CephContext *cct) { - ext_mime_map = new std::map; + ext_mime_map = std::make_unique>>(); ext_mime_map_init(dpp, cct, cct->_conf->rgw_mime_types_file.c_str()); // ignore errors; missing mime.types is not fatal return 0; @@ -120,6 +122,5 @@ int rgw_tools_init(const DoutPrefixProvider *dpp, CephContext *cct) void rgw_tools_cleanup() { - delete ext_mime_map; ext_mime_map = nullptr; }