]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
secret: use strncpy, snprintf
authorSage Weil <sage.weil@dreamhost.com>
Sun, 21 Aug 2011 21:07:21 +0000 (14:07 -0700)
committerSage Weil <sage.weil@dreamhost.com>
Sun, 21 Aug 2011 21:07:21 +0000 (14:07 -0700)
Coverity cid 42

Signed-off-by: Sage Weil <sage.weil@dreamhost.com>
src/common/secret.c

index 158343e8f5a39121769cc8662b0e2cecf5c4a6a5..3aa45c54e3a8a093eadc4b8c409cc0cc1b7e71ec 100644 (file)
@@ -75,13 +75,16 @@ static int add_secret_to_kernel(const char *secret, const char *key_name)
 int get_secret_option(const char *secret, const char *key_name, char *secret_option, size_t max_len)
 {
   int ret;
-  char option[strlen(secret) + strlen(key_name) + 7];
+  int olen = strlen(secret) + strlen(key_name) + 7;
+  char option[olen+1];
+
+  option[olen] = '\0';
 
   ret = add_secret_to_kernel(secret, key_name);
   if (ret < 0) {
     if (ret == -ENODEV || ret == -ENOSYS) {
       /* running against older kernel; fall back to secret= in options */
-      sprintf(option, "secret=%s", secret);
+      snprintf(option, olen, "secret=%s", secret);
       ret = 0;
     } else {
       fprintf(stderr, "adding ceph secret key to kernel failed: %s.\n", strerror(-ret));
@@ -89,13 +92,14 @@ int get_secret_option(const char *secret, const char *key_name, char *secret_opt
     }
   } else {
     /* add key= option to identify key to use */
-    sprintf(option, "key=%s", key_name);
+    snprintf(option, olen, "key=%s", key_name);
   }
 
   if (strlen(option) + 1 > max_len) {
     ret = -ERANGE;
   } else {
-    strcpy(secret_option, option);
+    secret_option[max_len-1] = '\0';
+    strncpy(secret_option, option, max_len-1);
   }
 
   return ret;