Kaynağa Gözat

cfg: fixed pointer aliasing warnings

- changed new_val[] to a union to silence the strict-aliasing
  warnings.
Andrei Pelinescu-Onciul 16 yıl önce
ebeveyn
işleme
e95c077588
2 değiştirilmiş dosya ile 33 ekleme ve 20 silme
  1. 21 19
      cfg/cfg_ctx.c
  2. 12 1
      cfg/cfg_ctx.h

+ 21 - 19
cfg/cfg_ctx.c

@@ -573,7 +573,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
 				if (changed->group != group) continue;
 				if (changed->group != group) continue;
 
 
 				memcpy(	temp_handle + changed->var->offset,
 				memcpy(	temp_handle + changed->var->offset,
-					changed->new_val,
+					changed->new_val.vraw,
 					cfg_var_size(changed->var));
 					cfg_var_size(changed->var));
 			}
 			}
 		} else {
 		} else {
@@ -595,7 +595,8 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
 	}
 	}
 
 
 	/* everything went ok, we can add the new value to the list */
 	/* everything went ok, we can add the new value to the list */
-	size = sizeof(cfg_changed_var_t) + cfg_var_size(var) - 1;
+	size = sizeof(cfg_changed_var_t) -
+			sizeof(((cfg_changed_var_t*)0)->new_val) + cfg_var_size(var);
 	changed = (cfg_changed_var_t *)shm_malloc(size);
 	changed = (cfg_changed_var_t *)shm_malloc(size);
 	if (!changed) {
 	if (!changed) {
 		LOG(L_ERR, "ERROR: cfg_set_delayed(): not enough shm memory\n");
 		LOG(L_ERR, "ERROR: cfg_set_delayed(): not enough shm memory\n");
@@ -608,7 +609,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
 	switch (CFG_VAR_TYPE(var)) {
 	switch (CFG_VAR_TYPE(var)) {
 
 
 	case CFG_VAR_INT:
 	case CFG_VAR_INT:
-		*(int *)changed->new_val = (int)(long)v;
+		changed->new_val.vint = (int)(long)v;
 		break;
 		break;
 
 
 	case CFG_VAR_STRING:
 	case CFG_VAR_STRING:
@@ -616,18 +617,18 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
 		s.s = v;
 		s.s = v;
 		s.len = (s.s) ? strlen(s.s) : 0;
 		s.len = (s.s) ? strlen(s.s) : 0;
 		if (cfg_clone_str(&s, &s)) goto error;
 		if (cfg_clone_str(&s, &s)) goto error;
-		*(char **)changed->new_val = s.s;
+		changed->new_val.vp = s.s;
 		break;
 		break;
 
 
 	case CFG_VAR_STR:
 	case CFG_VAR_STR:
 		/* clone the string to shm mem */
 		/* clone the string to shm mem */
 		s = *(str *)v;
 		s = *(str *)v;
 		if (cfg_clone_str(&s, &s)) goto error;
 		if (cfg_clone_str(&s, &s)) goto error;
-		memcpy(changed->new_val, &s, sizeof(str));
+		changed->new_val.vstr=s;
 		break;
 		break;
 
 
 	case CFG_VAR_POINTER:
 	case CFG_VAR_POINTER:
-		*(void **)changed->new_val = v;
+		changed->new_val.vp=v;
 		break;
 		break;
 
 
 	}
 	}
@@ -804,7 +805,7 @@ int cfg_commit(cfg_ctx_t *ctx)
 		}
 		}
 
 
 		memcpy(	p,
 		memcpy(	p,
-			changed->new_val,
+			changed->new_val.vraw,
 			cfg_var_size(changed->var));
 			cfg_var_size(changed->var));
 	}
 	}
 
 
@@ -870,8 +871,8 @@ int cfg_rollback(cfg_ctx_t *ctx)
 
 
 		if ((CFG_VAR_TYPE(changed->var) == CFG_VAR_STRING)
 		if ((CFG_VAR_TYPE(changed->var) == CFG_VAR_STRING)
 		|| (CFG_VAR_TYPE(changed->var) == CFG_VAR_STR)) {
 		|| (CFG_VAR_TYPE(changed->var) == CFG_VAR_STR)) {
-			if (*(char **)(changed->new_val))
-				shm_free(*(char **)(changed->new_val));
+			if (changed->new_val.vp)
+				shm_free(changed->new_val.vp);
 		}
 		}
 		shm_free(changed);
 		shm_free(changed);
 	}
 	}
@@ -1016,7 +1017,7 @@ int cfg_diff_next(void **h,
 			unsigned int *val_type)
 			unsigned int *val_type)
 {
 {
 	cfg_changed_var_t	*changed;
 	cfg_changed_var_t	*changed;
-	void	*p;
+	union cfg_var_value* pval;
 	static str	old_s, new_s;	/* we need the value even
 	static str	old_s, new_s;	/* we need the value even
 					after the function returns */
 					after the function returns */
 
 
@@ -1031,29 +1032,30 @@ int cfg_diff_next(void **h,
 	/* use the module's handle to access the variable
 	/* use the module's handle to access the variable
 	It means that the variable is read from the local config
 	It means that the variable is read from the local config
 	after forking */
 	after forking */
-	p = *(changed->group->handle) + changed->var->offset;
+	pval = (union cfg_var_value*)
+			(*(changed->group->handle) + changed->var->offset);
 
 
 	switch (CFG_VAR_TYPE(changed->var)) {
 	switch (CFG_VAR_TYPE(changed->var)) {
 	case CFG_VAR_INT:
 	case CFG_VAR_INT:
-		*old_val = (void *)(long)*(int *)p;
-		*new_val = (void *)(long)*(int *)changed->new_val;
+		*old_val = (void *)(long)pval->vint;
+		*new_val = (void *)(long)changed->new_val.vint;
 		break;
 		break;
 
 
 	case CFG_VAR_STRING:
 	case CFG_VAR_STRING:
-		*old_val = (void *)*(char **)p;
-		*new_val = (void *)*(char **)changed->new_val;
+		*old_val = pval->vp;
+		*new_val = changed->new_val.vp;
 		break;
 		break;
 
 
 	case CFG_VAR_STR:
 	case CFG_VAR_STR:
-		memcpy(&old_s, p, sizeof(str));
+		old_s=pval->vstr;
 		*old_val = (void *)&old_s;
 		*old_val = (void *)&old_s;
-		memcpy(&new_s, changed->new_val, sizeof(str));
+		new_s=changed->new_val.vstr;
 		*new_val = (void *)&new_s;
 		*new_val = (void *)&new_s;
 		break;
 		break;
 
 
 	case CFG_VAR_POINTER:
 	case CFG_VAR_POINTER:
-		*old_val = *(void **)p;
-		*new_val = *(void **)changed->new_val;
+		*old_val = pval->vp;
+		*new_val = changed->new_val.vp;
 		break;
 		break;
 
 
 	}
 	}

+ 12 - 1
cfg/cfg_ctx.h

@@ -37,6 +37,17 @@
 #include "cfg.h"
 #include "cfg.h"
 #include "cfg_struct.h"
 #include "cfg_struct.h"
 
 
+
+/* variable values */
+union cfg_var_value{
+	void* vp;
+	long vlong;
+	int vint;
+	str vstr;
+	unsigned char	vraw[1]; /* variable length */
+};
+
+
 /* linked list of variables with their new values */
 /* linked list of variables with their new values */
 typedef struct _cfg_changed_var {
 typedef struct _cfg_changed_var {
 	cfg_group_t	*group;
 	cfg_group_t	*group;
@@ -44,7 +55,7 @@ typedef struct _cfg_changed_var {
 	struct _cfg_changed_var	*next;
 	struct _cfg_changed_var	*next;
 
 
 	/* blob that contains the new value */
 	/* blob that contains the new value */
-	unsigned char	new_val[1];
+	union cfg_var_value new_val; /* variable size */
 } cfg_changed_var_t;
 } cfg_changed_var_t;
 
 
 /* callback that is called when a new group is declared */
 /* callback that is called when a new group is declared */