Browse Source

runtime cfg: fix sanity check on 64 bits

The sanity check for registered cfg_group_* structures and
cfg_defs was wrong on 64 bits systems when the structures
contained pointers (the possible structure padding was not taken
into account).
Fix: if the structure contains strings (CFG_VAR_STR or
CFG_VAR_STRING) or pointers (CFG_VAR_POINTER), round-up the
computed size to sizeof(pointer), before performing the sanity
check.
Andrei 16 năm trước cách đây
mục cha
commit
97ce716082
1 tập tin đã thay đổi với 8 bổ sung1 xóa
  1. 8 1
      cfg/cfg.c

+ 8 - 1
cfg/cfg.c

@@ -47,6 +47,7 @@ int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
 {
 {
 	int	i, num, size, group_name_len;
 	int	i, num, size, group_name_len;
 	cfg_mapping_t	*mapping = NULL;
 	cfg_mapping_t	*mapping = NULL;
+	int types;
 
 
 	/* check the number of the variables */
 	/* check the number of the variables */
 	for (num=0; def[num].name; num++);
 	for (num=0; def[num].name; num++);
@@ -57,13 +58,15 @@ int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
 		goto error;
 		goto error;
 	}
 	}
 	memset(mapping, 0, sizeof(cfg_mapping_t)*num);
 	memset(mapping, 0, sizeof(cfg_mapping_t)*num);
-
+	types=0;
 	/* calculate the size of the memory block that has to
 	/* calculate the size of the memory block that has to
 	be allocated for the cfg variables, and set the content of the 
 	be allocated for the cfg variables, and set the content of the 
 	cfg_mapping array the same time */
 	cfg_mapping array the same time */
 	for (i=0, size=0; i<num; i++) {
 	for (i=0, size=0; i<num; i++) {
 		mapping[i].def = &(def[i]);
 		mapping[i].def = &(def[i]);
 		mapping[i].name_len = strlen(def[i].name);
 		mapping[i].name_len = strlen(def[i].name);
+		/* record all the types for sanity checks */
+		types|=CFG_VAR_MASK(def[i].type);
 
 
 		/* padding depends on the type of the next variable */
 		/* padding depends on the type of the next variable */
 		switch (CFG_VAR_MASK(def[i].type)) {
 		switch (CFG_VAR_MASK(def[i].type)) {
@@ -128,6 +131,10 @@ int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
 		}
 		}
 	}
 	}
 
 
+	/* fix the computed size (char*, str or pointer members will force 
+	   structure padding to multiple of sizeof(pointer)) */
+	if (types & (CFG_VAR_STRING|CFG_VAR_STR|CFG_VAR_POINTER))
+		size=ROUND_POINTER(size);
 	/* minor validation */
 	/* minor validation */
 	if (size != def_size) {
 	if (size != def_size) {
 		LOG(L_ERR, "ERROR: register_cfg_def(): the specified size (%i) of the config "
 		LOG(L_ERR, "ERROR: register_cfg_def(): the specified size (%i) of the config "