]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: Remove VLA from `parse_mime_map_line`
authorAdam C. Emerson <aemerson@redhat.com>
Tue, 24 Mar 2026 22:22:08 +0000 (18:22 -0400)
committerAdam C. Emerson <aemerson@redhat.com>
Thu, 26 Mar 2026 04:07:21 +0000 (00:07 -0400)
And make other modernizations.

Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/rgw/driver/rados/rgw_tools.h
src/rgw/rgw_rest_swift.cc
src/rgw/rgw_tools.cc

index 1d2d7a02b99fc7d6bb40f83df41da911e23f6447..8312601a6a34289988e489aaa42741b3a981743b 100644 (file)
@@ -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<std::string, bufferlist> *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<std::string, bufferlist>& unfiltered_attrset, const std::string& check_prefix,
                         std::map<std::string, bufferlist> *attrset);
index e63b12458d40bebe36f13ae796197a62f21c1e11..fb5902eacca23d29d0b6b7a99e7c1da69104a5fa 100644 (file)
@@ -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;
        }
       }
index a19c5776da00a80f13d29fc4f7d09b927f0e82ea..b65c157c98d76db7b016d50228cca579a6765f7e 100644 (file)
@@ -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 <errno.h>
+#include <cerrno>
+#include <unordered_map>
 
 #include "common/errno.h"
 #include "common/safe_io.h" // for safe_read()
+#include "common/split.h"
 
 #include "driver/rados/rgw_tools.h"
 
 
 using namespace std;
 
-static std::map<std::string, std::string>* ext_mime_map;
+namespace {
+std::unique_ptr<std::map<std::string, std::string, std::less<>>> 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<string, string>::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<std::string, std::string>;
+  ext_mime_map = std::make_unique<std::map<std::string, std::string, std::less<>>>();
   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;
 }