From: Sam Lang Date: Wed, 8 Jun 2011 17:59:11 +0000 (-0500) Subject: Fix segfault caused by invalid argument string. X-Git-Tag: v0.29.1~12 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5d56dfb7f8a60659eac3420df66d53988ed50bc2;p=ceph.git Fix segfault caused by invalid argument string. This patchset includes minor fixes to the crushtool utility. If an invalid bucket type is speicifed on the command line, the code was iterating through bucket_types for the length of the static array, but the last entry in that array has null (0) values, which was causing a segfault. This patch just checks that bucket_types[i].name is non-null instead. Also, if the wrong bucket type or algorithm is specified, prints the usage string on exit. Signed-off-by: Sam Lang Signed-off-by: Sage Weil --- diff --git a/src/crushtool.cc b/src/crushtool.cc index 45b2d58afab..6c48f290286 100644 --- a/src/crushtool.cc +++ b/src/crushtool.cc @@ -55,6 +55,8 @@ map type_id; map rule_id; +void usage(); + string string_node(node_t &node) { string s = string(node.value.begin(), node.value.end()); @@ -142,8 +144,8 @@ void parse_bucket(iter_t const& i, CrushWrapper &crush) else if (a == "straw") alg = CRUSH_BUCKET_STRAW; else { - cerr << "unknown bucket alg '" << a << "'" << std::endl; - exit(1); + cerr << "unknown bucket alg '" << a << "'" << std::endl << std::endl; + usage(); } } else if (tag == "hash") { @@ -980,14 +982,14 @@ int main(int argc, const char **argv) crush.set_type_name(type, l.name); int buckettype = -1; - for (int i = 0; i < (int)(sizeof(bucket_types)/sizeof(bucket_types[0])); i++) - if (strcmp(l.buckettype, bucket_types[i].name) == 0) { + for (int i = 0; bucket_types[i].name; i++) + if (l.buckettype && strcmp(l.buckettype, bucket_types[i].name) == 0) { buckettype = bucket_types[i].type; break; } if (buckettype < 0) { - cerr << "unknown bucket type '" << l.buckettype << "'" << std::endl; - exit(1); + cerr << "unknown bucket type '" << l.buckettype << "'" << std::endl << std::endl; + usage(); } // build items