]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: Use libfmt rather than snprintf when constructing identifiers
authorAdam C. Emerson <aemerson@redhat.com>
Thu, 19 Mar 2026 21:45:09 +0000 (17:45 -0400)
committerAdam C. Emerson <aemerson@redhat.com>
Thu, 26 Mar 2026 04:07:20 +0000 (00:07 -0400)
Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
12 files changed:
src/rgw/driver/rados/rgw_cr_rados.h
src/rgw/driver/rados/rgw_data_sync.cc
src/rgw/driver/rados/rgw_lc_tier.cc
src/rgw/driver/rados/rgw_rados.cc
src/rgw/driver/rados/rgw_sync.cc
src/rgw/rgw_obj_types.h
src/rgw/rgw_rest.h
src/rgw/rgw_rest_swift.cc
src/rgw/rgw_swift_auth.cc
src/rgw/services/svc_bi_rados.cc
src/rgw/services/svc_notify.cc
src/test/rgw/test_rgw_common.h

index df40db5dbc46ac2d6269fc1aed6fd59490a4ed7f..c5858266473491179e86cbb1895de69d83cee510 100644 (file)
@@ -882,9 +882,9 @@ public:
                        store(_store), op(_op), num_shards(_num_shards) {
     shards.reserve(num_shards);
     for (int i = 0; i < num_shards; ++i) {
-      char buf[oid_prefix.size() + 16];
-      snprintf(buf, sizeof(buf), "%s.%d", oid_prefix.c_str(), i);
-      RGWOmapAppend *shard = new RGWOmapAppend(async_rados, store, rgw_raw_obj(pool, buf));
+      RGWOmapAppend *shard = new RGWOmapAppend(
+       async_rados, store, rgw_raw_obj(pool,
+                                       fmt::format("{}.{}", oid_prefix, i)));
       shard->get();
       shards.push_back(shard);
       op->spawn(shard, false);
index 72a20800cd3f82c569437c415c677cc6889de122..e76690da570f744aa1ee12779f60ed05c6a66edd 100644 (file)
@@ -900,9 +900,8 @@ int RGWRemoteDataLog::init_sync_status(const DoutPrefixProvider *dpp, int num_sh
 
 static string full_data_sync_index_shard_oid(const rgw_zone_id& source_zone, int shard_id)
 {
-  char buf[datalog_sync_full_sync_index_prefix.size() + 1 + source_zone.id.size() + 1 + 16];
-  snprintf(buf, sizeof(buf), "%s.%s.%d", datalog_sync_full_sync_index_prefix.c_str(), source_zone.id.c_str(), shard_id);
-  return string(buf);
+  return fmt::format("{}.{}.{}", datalog_sync_full_sync_index_prefix,
+                    source_zone.id, shard_id);
 }
 
 struct read_metadata_list {
@@ -3239,9 +3238,10 @@ int RGWRemoteDataLog::run_sync(const DoutPrefixProvider *dpp, int num_shards, rg
 {
   // construct and start bid manager for data sync fairness
   const auto& control_pool = sc.env->driver->svc()->zone->get_zone_params().control_pool;
-  char buf[data_sync_bids_oid.size() + sc.source_zone.id.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%s", data_sync_bids_oid.c_str(), sc.source_zone.id.c_str());
-  auto control_obj = rgw_raw_obj{control_pool, string(buf)};
+  auto control_obj = rgw_raw_obj{
+    control_pool,
+    fmt::format("{}.{}", data_sync_bids_oid, sc.source_zone.id)
+  };
 
   auto bid_manager = rgw::sync_fairness::create_rados_bid_manager(
       driver, control_obj, num_shards);
@@ -3346,18 +3346,14 @@ std::ostream& RGWDataSyncStatusManager::gen_prefix(std::ostream& out) const
 
 string RGWDataSyncStatusManager::sync_status_oid(const rgw_zone_id& source_zone)
 {
-  char buf[datalog_sync_status_oid_prefix.size() + source_zone.id.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%s", datalog_sync_status_oid_prefix.c_str(), source_zone.id.c_str());
-
-  return string(buf);
+  return fmt::format("{}.{}", datalog_sync_status_oid_prefix, source_zone.id);
 }
 
-string RGWDataSyncStatusManager::shard_obj_name(const rgw_zone_id& source_zone, int shard_id)
+string RGWDataSyncStatusManager::shard_obj_name(const rgw_zone_id& source_zone,
+                                               int shard_id)
 {
-  char buf[datalog_sync_status_shard_prefix.size() + source_zone.id.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%s.%d", datalog_sync_status_shard_prefix.c_str(), source_zone.id.c_str(), shard_id);
-
-  return string(buf);
+  return fmt::format("{}.{}.{}", datalog_sync_status_shard_prefix,
+                    source_zone.id, shard_id);
 }
 
 class RGWInitBucketShardSyncStatusCoroutine : public RGWCoroutine {
index 15aa5653652ee604d893e0cdbcf4d73e1cdc49ba..eb2ac8b4a423073abffd9f85f66e340dcc99f737 100644 (file)
@@ -218,15 +218,7 @@ static void init_headers(map<string, bufferlist>& attrs,
     } else if (strncmp(name, RGW_ATTR_META_PREFIX,
           sizeof(RGW_ATTR_META_PREFIX)-1) == 0) {
       name += sizeof(RGW_ATTR_META_PREFIX) - 1;
-      string sname(name);
-      string name_prefix = RGW_ATTR_META_PREFIX;
-      char full_name_buf[name_prefix.size() + sname.size() + 1];
-      snprintf(full_name_buf, sizeof(full_name_buf), "%.*s%.*s",
-          static_cast<int>(name_prefix.length()),
-          name_prefix.data(),
-          static_cast<int>(sname.length()),
-          sname.data());
-      headers[full_name_buf] = rgw_bl_str(kv.second);
+      headers[fmt::format("{}{}", RGW_ATTR_META_PREFIX, name)] = rgw_bl_str(kv.second);
     } else if (strcmp(name,RGW_ATTR_CONTENT_TYPE) == 0) {
       headers["CONTENT_TYPE"] = rgw_bl_str(kv.second);
     }
index 505a43a7940d2f819fa9d14c60d634b02e333b3b..1a1b07cc92e4bc3f4d16ba5163f4bd1908b06b1e 100644 (file)
@@ -2466,12 +2466,8 @@ int RGWRados::create_pool(const DoutPrefixProvider *dpp, const rgw_pool& pool)
 
 void RGWRados::create_bucket_id(string *bucket_id)
 {
-  uint64_t iid = instance_id();
-  uint64_t bid = next_bucket_id();
-  char buf[svc.zone->get_zone_params().get_id().size() + 48];
-  snprintf(buf, sizeof(buf), "%s.%" PRIu64 ".%" PRIu64,
-           svc.zone->get_zone_params().get_id().c_str(), iid, bid);
-  *bucket_id = buf;
+  *bucket_id = fmt::format("{}.{}.{}", svc.zone->get_zone_params().get_id(),
+                          instance_id(), next_bucket_id());
 }
 
 int RGWRados::create_bucket(const DoutPrefixProvider* dpp,
@@ -3060,10 +3056,10 @@ int RGWRados::swift_versioning_copy(RGWObjectCtx& obj_ctx,
   }
 
   const string& src_name = obj.get_oid();
-  char buf[src_name.size() + 32];
   struct timespec ts = ceph::real_clock::to_timespec(state->mtime);
-  snprintf(buf, sizeof(buf), "%03x%s/%lld.%06ld", (int)src_name.size(),
-           src_name.c_str(), (long long)ts.tv_sec, ts.tv_nsec / 1000);
+  auto buf = fmt::format("{:03x}{}/{}.{:06}", int(src_name.size()),
+                       src_name, static_cast<long long>(ts.tv_sec),
+                       ts.tv_nsec / 1000);
 
   RGWBucketInfo dest_bucket_info;
 
index 76b3c6018b0b5f17e483e27906f20919513412e4..810f35b77e3da22b09bedd27a47120677caeac58 100644 (file)
@@ -33,9 +33,7 @@ RGWSyncErrorLogger::RGWSyncErrorLogger(rgw::sal::RadosStore* _store, const strin
   }
 }
 string RGWSyncErrorLogger::get_shard_oid(const string& oid_prefix, int shard_id) {
-  char buf[oid_prefix.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%d", oid_prefix.c_str(), shard_id);
-  return string(buf);
+  return fmt::format("{}.{}", oid_prefix, shard_id);
 }
 
 RGWCoroutine *RGWSyncErrorLogger::log_error_cr(const DoutPrefixProvider *dpp, const string& source_zone, const string& section, const string& name, uint32_t error_code, const string& message) {
@@ -392,10 +390,7 @@ string RGWMetaSyncEnv::status_oid()
 
 string RGWMetaSyncEnv::shard_obj_name(int shard_id)
 {
-  char buf[mdlog_sync_status_shard_prefix.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%d", mdlog_sync_status_shard_prefix.c_str(), shard_id);
-
-  return string(buf);
+  return fmt::format("{}.{}", mdlog_sync_status_shard_prefix, shard_id);
 }
 
 class RGWAsyncReadMDLogEntries : public RGWAsyncRadosRequest {
@@ -1050,9 +1045,7 @@ public:
 
 static string full_sync_index_shard_oid(int shard_id)
 {
-  char buf[mdlog_sync_full_sync_index_prefix.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%d", mdlog_sync_full_sync_index_prefix.c_str(), shard_id);
-  return string(buf);
+  return fmt::format("{}.{}", mdlog_sync_full_sync_index_prefix, shard_id);
 }
 
 class RGWReadRemoteMetadataCR : public RGWCoroutine {
index d8874a2b71019cc4b16e51aeedb30a9fd77a8c51..a132eab31614ef57342565d76e8d31f9b535077c 100644 (file)
@@ -197,9 +197,7 @@ struct rgw_obj_key {
       return std::string("_") + name;
     };
 
-    char buf[ns.size() + 16];
-    snprintf(buf, sizeof(buf), "_%s_", ns.c_str());
-    return std::string(buf) + name;
+    return fmt::format("_{}_{}", ns, name);
   };
 
   void get_index_key(rgw_obj_index_key* key) const {
index 7b540a494e616991958def91f211ecce1b7a197e..42df336a5bde663a93bd1fec207887d0606cf8e2 100644 (file)
@@ -755,13 +755,7 @@ inline void dump_header_prefixed(req_state* s,
                                 const std::string_view& name_prefix,
                                 const std::string_view& name,
                                 Args&&... args) {
-  char full_name_buf[name_prefix.size() + name.size() + 1];
-  const auto len = snprintf(full_name_buf, sizeof(full_name_buf), "%.*s%.*s",
-                            static_cast<int>(name_prefix.length()),
-                            name_prefix.data(),
-                            static_cast<int>(name.length()),
-                            name.data());
-  std::string_view full_name(full_name_buf, len);
+  const auto full_name = fmt::format("{}{}", name_prefix, name);
   return dump_header(s, std::move(full_name), std::forward<Args>(args)...);
 }
 
@@ -771,15 +765,7 @@ inline void dump_header_infixed(req_state* s,
                                const std::string_view& infix,
                                const std::string_view& sufix,
                                Args&&... args) {
-  char full_name_buf[prefix.size() + infix.size() + sufix.size() + 1];
-  const auto len = snprintf(full_name_buf, sizeof(full_name_buf), "%.*s%.*s%.*s",
-                            static_cast<int>(prefix.length()),
-                            prefix.data(),
-                            static_cast<int>(infix.length()),
-                            infix.data(),
-                            static_cast<int>(sufix.length()),
-                            sufix.data());
-  std::string_view full_name(full_name_buf, len);
+  auto full_name = fmt::format("{}{}{}", prefix, infix, sufix);
   return dump_header(s, std::move(full_name), std::forward<Args>(args)...);
 }
 
@@ -788,10 +774,8 @@ inline void dump_header_quoted(req_state* s,
                               const std::string_view& name,
                               const std::string_view& val) {
   /* We need two extra bytes for quotes. */
-  char qvalbuf[val.size() + 2 + 1];
-  const auto len = snprintf(qvalbuf, sizeof(qvalbuf), "\"%.*s\"",
-                            static_cast<int>(val.length()), val.data());
-  return dump_header(s, name, std::string_view(qvalbuf, len));
+  auto qval = fmt::format("\"{}\"", val);
+  return dump_header(s, name, std::move(qval));
 }
 
 template <class ValueT>
index bc442840fa0fe3c445bdc3882831278fc3d1d065..e63b12458d40bebe36f13ae796197a62f21c1e11 100644 (file)
@@ -3057,29 +3057,29 @@ int RGWHandler_REST_SWIFT::init_from_header(rgw::sal::Driver* driver,
 
   s->prot_flags |= RGW_REST_SWIFT;
 
-  char reqbuf[frontend_prefix.length() + s->decoded_uri.length() + 1];
-  sprintf(reqbuf, "%s%s", frontend_prefix.c_str(), s->decoded_uri.c_str());
-  const char *req_name = reqbuf;
+  auto reqbuf = fmt::format("{}{}", frontend_prefix, s->decoded_uri);
+  std::string_view req_name = reqbuf;
 
-  const char *p;
+  // args.set requires a `const std::string&`
+  std::string p;
 
-  if (*req_name == '?') {
+  if (req_name.starts_with('?')) {
     p = req_name;
   } else {
-    p = s->info.request_params.c_str();
+    p = s->info.request_params;
   }
 
   s->info.args.set(p);
   s->info.args.parse(s);
 
   /* Skip the leading slash of URL hierarchy. */
-  if (req_name[0] != '/') {
+  if (!req_name.starts_with('/')) {
     return 0;
   } else {
-    req_name++;
+    req_name.remove_prefix(1);
   }
 
-  if ('\0' == req_name[0]) {
+  if (req_name.empty()) {
     return g_conf()->rgw_swift_url_prefix == "/" ? -ERR_BAD_URL : 0;
   }
 
@@ -3110,16 +3110,12 @@ int RGWHandler_REST_SWIFT::init_from_header(rgw::sal::Driver* driver,
   }
 
   /* verify that the request_uri conforms with what's expected */
-  char buf[g_conf()->rgw_swift_url_prefix.length() + 16 + tenant_path.length()];
-  int blen;
-  if (g_conf()->rgw_swift_url_prefix == "/") {
-    blen = sprintf(buf, "/v1%s", tenant_path.c_str());
-  } else {
-    blen = sprintf(buf, "/%s/v1%s",
-                   g_conf()->rgw_swift_url_prefix.c_str(), tenant_path.c_str());
-  }
+  const std::string swift_url_prefix =
+      (g_conf()->rgw_swift_url_prefix == "/")
+          ? fmt::format("/v1{}", tenant_path)
+          : fmt::format("/{}/v1{}", g_conf()->rgw_swift_url_prefix, tenant_path);
 
-  if (strncmp(reqbuf, buf, blen) != 0) {
+  if (!reqbuf.starts_with(swift_url_prefix)) {
     return -ENOENT;
   }
 
index 5c87fcac572e8fd2bcc482e2bd0a9856f822983c..df4ebfc1629fc928f065a6cec64a161a55a1976e 100644 (file)
@@ -473,8 +473,7 @@ ExternalTokenEngine::authenticate(const DoutPrefixProvider* dpp,
   }
 
   auth_url.append("token");
-  char url_buf[auth_url.size() + 1 + token.length() + 1];
-  sprintf(url_buf, "%s/%s", auth_url.c_str(), token.c_str());
+  auto url_buf = fmt::format("{}/{}", auth_url, token);
 
   RGWHTTPHeadersCollector validator(cct, "GET", url_buf, { "X-Auth-Groups", "X-Auth-Ttl" });
 
index 7ae12ca8fee75ac86fd415fb8e37c0ac498119d4..b1b5f61d947936bb6535fd93baf4c874713e3029 100644 (file)
@@ -4,12 +4,13 @@
 #include <algorithm>
 #include <iterator>
 
+#include <fmt/format.h>
+
 #include "svc_bi_rados.h"
 #include "svc_bilog_rados.h"
 #include "svc_zone.h"
 
 #include "rgw_asio_thread.h"
-#include "rgw_bucket.h"
 #include "rgw_zone.h"
 #include "driver/rados/rgw_datalog.h"
 
@@ -124,16 +125,22 @@ int RGWSI_BucketIndex_RADOS::open_bucket_index(const DoutPrefixProvider *dpp,
   return 0;
 }
 
-static char bucket_obj_with_generation(char *buf, size_t len, const string& bucket_oid_base, uint64_t gen_id,
-                                    uint32_t shard_id)
+namespace {
+inline std::string
+bucket_obj_with_generation(
+    std::string_view bucket_oid_base,
+    uint64_t gen_id,
+    uint32_t shard_id)
 {
-  return snprintf(buf, len, "%s.%" PRIu64 ".%d", bucket_oid_base.c_str(), gen_id, shard_id);
+  return fmt::format("{}.{}.{}", bucket_oid_base, gen_id, shard_id);
 }
 
-static char bucket_obj_without_generation(char *buf, size_t len, const string& bucket_oid_base, uint32_t shard_id)
+inline std::string
+bucket_obj_without_generation(std::string_view bucket_oid_base, uint32_t shard_id)
 {
-  return snprintf(buf, len, "%s.%d", bucket_oid_base.c_str(), shard_id);
+  return fmt::format("{}.{}", bucket_oid_base, shard_id);
 }
+} // namespace
 
 static void get_bucket_index_objects(const string& bucket_oid_base,
                                      uint32_t num_shards, uint64_t gen_id,
@@ -144,27 +151,26 @@ static void get_bucket_index_objects(const string& bucket_oid_base,
   if (!num_shards) {
     bucket_objects[0] = bucket_oid_base;
   } else {
-    char buf[bucket_oid_base.size() + 64];
     if (shard_id < 0) {
       for (uint32_t i = 0; i < num_shards; ++i) {
         if (gen_id) {
-          bucket_obj_with_generation(buf, sizeof(buf), bucket_oid_base, gen_id, i);
+          bucket_objects[i] = bucket_obj_with_generation(bucket_oid_base, gen_id, i);
         } else {
-          bucket_obj_without_generation(buf, sizeof(buf), bucket_oid_base, i);
+          bucket_objects[i] = bucket_obj_without_generation(bucket_oid_base, i);
         }
-        bucket_objects[i] = buf;
       }
     } else {
       if (std::cmp_greater(shard_id, num_shards)) {
         return;
       } else {
         if (gen_id) {
-          bucket_obj_with_generation(buf, sizeof(buf), bucket_oid_base, gen_id, shard_id);
+          bucket_objects[shard_id] =
+              bucket_obj_with_generation(bucket_oid_base, gen_id, shard_id);
         } else {
           // for backward compatibility, gen_id(0) will not be added in the object name
-          bucket_obj_without_generation(buf, sizeof(buf), bucket_oid_base, shard_id);
+          bucket_objects[shard_id] =
+              bucket_obj_without_generation(bucket_oid_base, shard_id);
         }
-        bucket_objects[shard_id] = buf;
       }
     }
   }
@@ -232,15 +238,13 @@ void RGWSI_BucketIndex_RADOS::get_bucket_index_object(
     // By default with no sharding, we use the bucket oid as itself
     (*bucket_obj) = bucket_oid_base;
   } else {
-    char buf[bucket_oid_base.size() + 64];
     if (gen_id) {
-      bucket_obj_with_generation(buf, sizeof(buf), bucket_oid_base, gen_id, shard_id);
-      (*bucket_obj) = buf;
-         ldout(cct, 10) << "bucket_obj is " << (*bucket_obj) << dendl;
+      (*bucket_obj) =
+        bucket_obj_with_generation(bucket_oid_base, gen_id, shard_id);
+      ldout(cct, 10) << "bucket_obj is " << (*bucket_obj) << dendl;
     } else {
       // for backward compatibility, gen_id(0) will not be added in the object name
-      bucket_obj_without_generation(buf, sizeof(buf), bucket_oid_base, shard_id);
-      (*bucket_obj) = buf;
+      (*bucket_obj) = bucket_obj_without_generation(bucket_oid_base, shard_id);
     }
   }
 }
@@ -262,13 +266,12 @@ int RGWSI_BucketIndex_RADOS::get_bucket_index_object(
         }
       } else {
         uint32_t sid = bucket_shard_index(obj_key, normal.num_shards);
-        char buf[bucket_oid_base.size() + 64];
         if (gen_id) {
-          bucket_obj_with_generation(buf, sizeof(buf), bucket_oid_base, gen_id, sid);
+          (*bucket_obj) =
+              bucket_obj_with_generation(bucket_oid_base, gen_id, sid);
         } else {
-          bucket_obj_without_generation(buf, sizeof(buf), bucket_oid_base, sid);
+          (*bucket_obj) = bucket_obj_without_generation(bucket_oid_base, sid);
         }
-        (*bucket_obj) = buf;
         if (shard_id) {
           *shard_id = (int)sid;
         }
index f190083d5eb1e66cb7d50f4e8e63fe198797ee46..744f6500207c1f00e8df02c2b9cf52c5797a84f5 100644 (file)
@@ -144,10 +144,7 @@ RGWSI_Notify::~RGWSI_Notify()
 
 string RGWSI_Notify::get_control_oid(int i)
 {
-  char buf[notify_oid_prefix.size() + 16];
-  snprintf(buf, sizeof(buf), "%s.%d", notify_oid_prefix.c_str(), i);
-
-  return string(buf);
+  return fmt::format("{}.{}", notify_oid_prefix, i);
 }
 
 // do not call pick_obj_control before init_watch
index c97e9665d0949e6dec8a3eff9158fafa671e6af7..643584eba499b11ea844165367a14c13893481c8 100644 (file)
@@ -236,9 +236,7 @@ public:
       return std::string("_") + orig_obj;
     };
 
-    char buf[ns.size() + 16];
-    snprintf(buf, sizeof(buf), "_%s_", ns.c_str());
-    return std::string(buf) + orig_obj;
+    return fmt::format("_{}_{}", ns, orig_obj);
   };
 
   void from_index_key(old_rgw_bucket& b, const rgw_obj_key& key) {