From 8f3672dc488a0c29e7590f8f5656a693d2fdc41c Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Thu, 4 Nov 2010 14:33:48 -0700 Subject: [PATCH] Replace sprintf with snprintf Replace sprintf with snprintf. This is especially critical when the format string includes "%s". Signed-off-by: Colin McCabe --- src/client/SyntheticClient.h | 2 +- src/client/test_ioctls.c | 6 ++++-- src/include/types.h | 2 +- src/mds/mdstypes.h | 9 +++++++-- src/messages/MGenericMessage.h | 2 +- src/mon/MonitorStore.h | 8 ++++---- src/mon/PGMap.h | 2 +- src/os/FakeStoreBDBCollections.h | 6 ++---- src/os/FileStore.cc | 4 +++- src/osd/OSD.cc | 2 +- src/osd/OSD.h | 4 ++-- src/osdc/rados_bencher.h | 6 +++--- src/radosacl.cc | 2 +- src/rbd.cc | 10 ++++++---- src/test_trans.cc | 2 +- 15 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/client/SyntheticClient.h b/src/client/SyntheticClient.h index 3a1ef64b965cc..b94c3e0f74b09 100644 --- a/src/client/SyntheticClient.h +++ b/src/client/SyntheticClient.h @@ -155,7 +155,7 @@ class SyntheticClient { filepath sub; char sub_s[50]; const char *make_sub(const char *base) { - sprintf(sub_s, "%s.%d", base, rand() % 100); + snprintf(sub_s, sizeof(sub_s), "%s.%d", base, rand() % 100); string f = sub_s; sub = cwd; sub.push_dentry(f); diff --git a/src/client/test_ioctls.c b/src/client/test_ioctls.c index cfce167a08e08..50709274f8c4c 100644 --- a/src/client/test_ioctls.c +++ b/src/client/test_ioctls.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -19,7 +20,7 @@ int main(int argc, char **argv) int fd, err; struct ceph_ioctl_layout l; struct ceph_ioctl_dataloc dl; - char *new_file_name = (char *)malloc(sizeof(char)*4096); + char new_file_name[PATH_MAX]; if (argc < 3) { printf("usage: test_ioctls \n"); @@ -102,7 +103,8 @@ int main(int argc, char **argv) } printf("set layout, creating file\n"); - sprintf(new_file_name, "%s/testfile", argv[3]); + snprintf(new_file_name, sizeof(new_file_name), + "%s/testfile", argv[3]); fd = open(new_file_name, O_CREAT | O_RDWR, 0644); if (fd < 0) { perror("couldn't open file"); diff --git a/src/include/types.h b/src/include/types.h index cf3c4eef9dbb2..4855f645848ad 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -423,7 +423,7 @@ inline ostream& operator<<(ostream& out, const SnapContext& snapc) { inline ostream& operator<<(ostream& out, const ceph_fsid& f) { char b[37]; - sprintf(b, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + snprintf(b, sizeof(b), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", f.fsid[0], f.fsid[1], f.fsid[2], f.fsid[3], f.fsid[4], f.fsid[5], f.fsid[6], f.fsid[7], f.fsid[8], f.fsid[9], f.fsid[10], f.fsid[11], f.fsid[12], f.fsid[13], f.fsid[14], f.fsid[15]); return out << b; diff --git a/src/mds/mdstypes.h b/src/mds/mdstypes.h index 42b27189bb882..f3ada3ec4ef9a 100644 --- a/src/mds/mdstypes.h +++ b/src/mds/mdstypes.h @@ -3,7 +3,11 @@ #ifndef CEPH_MDSTYPES_H #define CEPH_MDSTYPES_H +#ifndef __STDC_FORMAT_MACROS +#define __STDC_FORMAT_MACROS +#endif +#include #include #include #include @@ -1171,10 +1175,11 @@ struct dentry_key_t { __u32 l = strlen(name) + 1; char b[20]; if (snapid != CEPH_NOSNAP) { - sprintf(b, "%llx", (long long unsigned)snapid); + uint64_t val(snapid); + snprintf(b, sizeof(b), "%" PRIx64, val); l += strlen(b); } else { - strcpy(b, "head"); + snprintf(b, sizeof(b), "%s", "head"); l += 4; } ::encode(l, bl); diff --git a/src/messages/MGenericMessage.h b/src/messages/MGenericMessage.h index 305ea306a9517..7b0d32312ba9a 100644 --- a/src/messages/MGenericMessage.h +++ b/src/messages/MGenericMessage.h @@ -24,7 +24,7 @@ class MGenericMessage : public Message { public: MGenericMessage(int t) : Message(t) { - sprintf(tname, "generic%d", get_type()); + snprintf(tname, sizeof(tname), "generic%d", get_type()); } //void set_pcid(long pcid) { this->pcid = pcid; } diff --git a/src/mon/MonitorStore.h b/src/mon/MonitorStore.h index dc488c31677df..9dc7229615722 100644 --- a/src/mon/MonitorStore.h +++ b/src/mon/MonitorStore.h @@ -65,24 +65,24 @@ public: } bool exists_bl_sn(const char *a, version_t b) { char bs[20]; - sprintf(bs, "%llu", (unsigned long long)b); + snprintf(bs, sizeof(bs), "%llu", (unsigned long long)b); return exists_bl_ss(a, bs); } int get_bl_sn(bufferlist& bl, const char *a, version_t b) { char bs[20]; - sprintf(bs, "%llu", (unsigned long long)b); + snprintf(bs, sizeof(bs), "%llu", (unsigned long long)b); return get_bl_ss(bl, a, bs); } int put_bl_sn(bufferlist& bl, const char *a, version_t b, bool sync=true) { char bs[20]; - sprintf(bs, "%llu", (unsigned long long)b); + snprintf(bs, sizeof(bs), "%llu", (unsigned long long)b); return put_bl_ss(bl, a, bs, sync); } int erase_ss(const char *a, const char *b); int erase_sn(const char *a, version_t b) { char bs[20]; - sprintf(bs, "%llu", (unsigned long long)b); + snprintf(bs, sizeof(bs), "%llu", (unsigned long long)b); return erase_ss(a, bs); } diff --git a/src/mon/PGMap.h b/src/mon/PGMap.h index 7ddb09eba6ae5..f789bd2dd954f 100644 --- a/src/mon/PGMap.h +++ b/src/mon/PGMap.h @@ -310,7 +310,7 @@ public: if (pg_sum.num_objects_degraded) { double pc = (double)pg_sum.num_objects_degraded / (double)pg_sum.num_object_copies * (double)100.0; char b[20]; - sprintf(b, "%.3lf", pc); + snprintf(b, sizeof(b), "%.3lf", pc); out << "; " //<< pg_sum.num_objects_missing_on_primary << "/" << pg_sum.num_objects_degraded << "/" << pg_sum.num_object_copies << " degraded (" << b << "%)"; diff --git a/src/os/FakeStoreBDBCollections.h b/src/os/FakeStoreBDBCollections.h index 7d04e272d3f6a..34b7e43301e3f 100644 --- a/src/os/FakeStoreBDBCollections.h +++ b/src/os/FakeStoreBDBCollections.h @@ -36,13 +36,11 @@ class FakeStoreBDBCollections { // dirs void get_dir(string& dir) { - char s[30]; - sprintf(s, "%d", whoami); - dir = basedir + "/" + s; + dir = basedir + "/" + string(whoami); } void get_collfn(coll_t c, string &fn) { char s[100]; - sprintf(s, "%d/%02llx/%016llx.co", whoami, BDBHASH_FUNC(c), c); + snprintf(s, sizeof(s), "%d/%02llx/%016llx.co", whoami, BDBHASH_FUNC(c), c); fn = basedir + "/" + s; } diff --git a/src/os/FileStore.cc b/src/os/FileStore.cc index ce541b1367c69..eecbe9509e505 100644 --- a/src/os/FileStore.cc +++ b/src/os/FileStore.cc @@ -25,6 +25,8 @@ #include "common/Timer.h" +#define __STDC_FORMAT_MACROS +#include #include #include #include @@ -913,7 +915,7 @@ int FileStore::write_op_seq(int fd, uint64_t seq) { char s[30]; int ret; - sprintf(s, "%lld\n", (long long unsigned)seq); + snprintf(s, sizeof(s), "%" PRId64 "\n", seq); ret = ::pwrite(fd, s, strlen(s), 0); return ret; } diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index 7048d0bf4114b..5ce674d747294 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -1793,7 +1793,7 @@ void OSD::handle_command(MMonCommand *m) utime_t start = g_clock.now(); for (uint64_t pos = 0; pos < count; pos += bsize) { char nm[30]; - sprintf(nm, "disk_bw_test_%lld", (long long)pos); + snprintf(nm, sizeof(nm), "disk_bw_test_%lld", (long long)pos); object_t oid(nm); sobject_t soid(oid, 0); ObjectStore::Transaction *t = new ObjectStore::Transaction; diff --git a/src/osd/OSD.h b/src/osd/OSD.h index 971db37df7314..6cfbaac0288e3 100644 --- a/src/osd/OSD.h +++ b/src/osd/OSD.h @@ -143,12 +143,12 @@ public: static sobject_t get_osdmap_pobject_name(epoch_t epoch) { char foo[20]; - sprintf(foo, "osdmap.%d", epoch); + snprintf(foo, sizeof(foo), "osdmap.%d", epoch); return sobject_t(object_t(foo), 0); } static sobject_t get_inc_osdmap_pobject_name(epoch_t epoch) { char foo[20]; - sprintf(foo, "inc_osdmap.%d", epoch); + snprintf(foo, sizeof(foo), "inc_osdmap.%d", epoch); return sobject_t(object_t(foo), 0); } diff --git a/src/osdc/rados_bencher.h b/src/osdc/rados_bencher.h index dfd11c64f775a..5f1e261658a89 100644 --- a/src/osdc/rados_bencher.h +++ b/src/osdc/rados_bencher.h @@ -52,7 +52,7 @@ void generate_object_name(char *s, int objnum) char hostname[30]; gethostname(hostname, sizeof(hostname)-1); hostname[sizeof(hostname)-1] = 0; - sprintf(s, "%s_%d_object%d", hostname, getpid(), objnum); + snprintf(s, sizeof(hostname), "%s_%d_object%d", hostname, getpid(), objnum); } int write_bench(Rados& rados, rados_pool_t pool, @@ -254,7 +254,7 @@ int write_bench(Rados& rados, rados_pool_t pool, bandwidth = ((double)data->finished)*((double)data->object_size)/(double)timePassed; bandwidth = bandwidth/(1024*1024); // we want it in MB/sec char bw[20]; - sprintf(bw, "%.3lf \n", bandwidth); + snprintf(bw, sizeof(bw), "%.3lf \n", bandwidth); cout << "Total time run: " << timePassed << std::endl << "Total writes made: " << data->finished << std::endl @@ -427,7 +427,7 @@ int seq_read_bench(Rados& rados, rados_pool_t pool, int seconds_to_run, bandwidth = ((double)data->finished)*((double)data->object_size)/(double)runtime; bandwidth = bandwidth/(1024*1024); // we want it in MB/sec char bw[20]; - sprintf(bw, "%.3lf \n", bandwidth); + snprintf(bw, sizeof(bw), "%.3lf \n", bandwidth); cout << "Total time run: " << runtime << std::endl << "Total reads made: " << data->finished << std::endl diff --git a/src/radosacl.cc b/src/radosacl.cc index 82979ea89ed28..126e585cf048e 100644 --- a/src/radosacl.cc +++ b/src/radosacl.cc @@ -145,7 +145,7 @@ int main(int argc, const char **argv) ACLID id; - sprintf(id.id, "%.16x", 0x1234); + snprintf(id.id, ID_SIZE + 1, "%.16x", 0x1234); cout << "id=" << id.id << std::endl; r = rados.exec(pool, oid, "acl", "get", bl, bl2); diff --git a/src/rbd.cc b/src/rbd.cc index 07849edc58898..32b93a3420d71 100644 --- a/src/rbd.cc +++ b/src/rbd.cc @@ -12,6 +12,7 @@ * */ +#define __STDC_FORMAT_MACROS #include "config.h" #include "common/common_init.h" @@ -20,12 +21,12 @@ using namespace librados; #include "include/byteorder.h" +#include +#include #include - #include -#include #include -#include +#include #include "include/rbd_types.h" @@ -121,7 +122,8 @@ static void print_header(const char *imgname, rbd_obj_header_ondisk *header) static string get_block_oid(rbd_obj_header_ondisk *header, uint64_t num) { char o[RBD_MAX_SEG_NAME_SIZE]; - sprintf(o, "%s.%012llx", header->block_name, (unsigned long long)num); + snprintf(o, RBD_MAX_SEG_NAME_SIZE, + "%s.%012" PRIx64, header->block_name, num); return o; } diff --git a/src/test_trans.cc b/src/test_trans.cc index b2a43f869941a..076321622dda2 100644 --- a/src/test_trans.cc +++ b/src/test_trans.cc @@ -60,7 +60,7 @@ int main(int argc, const char **argv) for (int i=0; i