[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[lvm-devel] [LVM2 PATCH] Add overflow check to find_config_int()



find_config_int() and find_config_tree_int() are the function
to search throught the config tree and return the found value
or given default on failure.

Internally, the values are int64_t but implicitly casted to int
on return.

Though I think it doesn't matter in realistic configs,
it's better to check them and leave explicit log message.

Thanks,
-- 
Jun'ichi Nomura, NEC Corporation of America
Add overflow check to find_config_int()/find_config_tree_int().

Index: LVM2.work/lib/config/config.c
===================================================================
--- LVM2.work.orig/lib/config/config.c
+++ LVM2.work/lib/config/config.c
@@ -963,8 +963,16 @@ static int64_t _find_config_int64(const 
 
 int find_config_int(const struct config_node *cn, const char *path, int fail)
 {
-	/* FIXME Add log_error message on overflow */
-	return (int) _find_config_int64(cn, NULL, path, (int64_t) fail);
+	int64_t r = _find_config_int64(cn, NULL, path, (int64_t) fail);
+	int intval = (int) r;
+
+	if (r != intval) {
+		log_error("%s: value out of range (%" PRId64 "), "
+			  "defaulting to %d", path, r, fail);
+		return fail;
+	}
+
+	return intval;
 }
 
 static float _find_config_float(const struct config_node *cn1,
@@ -1006,8 +1014,16 @@ const char *find_config_tree_str(struct 
 int find_config_tree_int(struct cmd_context *cmd, const char *path,
 			 int fail)
 {
-	/* FIXME Add log_error message on overflow */
-	return (int) _find_config_int64(cmd->cft_override ? cmd->cft_override->root : NULL, cmd->cft->root, path, (int64_t) fail);
+	int64_t r = _find_config_int64(cmd->cft_override ? cmd->cft_override->root : NULL, cmd->cft->root, path, (int64_t) fail);
+	int intval = (int) r;
+
+	if (r != intval) {
+		log_error("%s: value out of range (%" PRId64 "), "
+			  "defaulting to %d", path, r, fail);
+		return fail;
+	}
+
+	return intval;
 }
 
 float find_config_tree_float(struct cmd_context *cmd, const char *path,

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]