Browse code

cfg: fixed pointer aliasing warnings

- changed new_val[] to a union to silence the strict-aliasing
warnings.

Andrei Pelinescu-Onciul authored on 12/10/2009 16:44:13
Showing 2 changed files
... ...
@@ -573,7 +573,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
573 573
 				if (changed->group != group) continue;
574 574
 
575 575
 				memcpy(	temp_handle + changed->var->offset,
576
-					changed->new_val,
576
+					changed->new_val.vraw,
577 577
 					cfg_var_size(changed->var));
578 578
 			}
579 579
 		} else {
... ...
@@ -595,7 +595,8 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
595 595
 	}
596 596
 
597 597
 	/* everything went ok, we can add the new value to the list */
598
-	size = sizeof(cfg_changed_var_t) + cfg_var_size(var) - 1;
598
+	size = sizeof(cfg_changed_var_t) -
599
+			sizeof(((cfg_changed_var_t*)0)->new_val) + cfg_var_size(var);
599 600
 	changed = (cfg_changed_var_t *)shm_malloc(size);
600 601
 	if (!changed) {
601 602
 		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,
608 609
 	switch (CFG_VAR_TYPE(var)) {
609 610
 
610 611
 	case CFG_VAR_INT:
611
-		*(int *)changed->new_val = (int)(long)v;
612
+		changed->new_val.vint = (int)(long)v;
612 613
 		break;
613 614
 
614 615
 	case CFG_VAR_STRING:
... ...
@@ -616,18 +617,18 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
616 617
 		s.s = v;
617 618
 		s.len = (s.s) ? strlen(s.s) : 0;
618 619
 		if (cfg_clone_str(&s, &s)) goto error;
619
-		*(char **)changed->new_val = s.s;
620
+		changed->new_val.vp = s.s;
620 621
 		break;
621 622
 
622 623
 	case CFG_VAR_STR:
623 624
 		/* clone the string to shm mem */
624 625
 		s = *(str *)v;
625 626
 		if (cfg_clone_str(&s, &s)) goto error;
626
-		memcpy(changed->new_val, &s, sizeof(str));
627
+		changed->new_val.vstr=s;
627 628
 		break;
628 629
 
629 630
 	case CFG_VAR_POINTER:
630
-		*(void **)changed->new_val = v;
631
+		changed->new_val.vp=v;
631 632
 		break;
632 633
 
633 634
 	}
... ...
@@ -804,7 +805,7 @@ int cfg_commit(cfg_ctx_t *ctx)
804 805
 		}
805 806
 
806 807
 		memcpy(	p,
807
-			changed->new_val,
808
+			changed->new_val.vraw,
808 809
 			cfg_var_size(changed->var));
809 810
 	}
810 811
 
... ...
@@ -870,8 +871,8 @@ int cfg_rollback(cfg_ctx_t *ctx)
870 871
 
871 872
 		if ((CFG_VAR_TYPE(changed->var) == CFG_VAR_STRING)
872 873
 		|| (CFG_VAR_TYPE(changed->var) == CFG_VAR_STR)) {
873
-			if (*(char **)(changed->new_val))
874
-				shm_free(*(char **)(changed->new_val));
874
+			if (changed->new_val.vp)
875
+				shm_free(changed->new_val.vp);
875 876
 		}
876 877
 		shm_free(changed);
877 878
 	}
... ...
@@ -1016,7 +1017,7 @@ int cfg_diff_next(void **h,
1016 1017
 			unsigned int *val_type)
1017 1018
 {
1018 1019
 	cfg_changed_var_t	*changed;
1019
-	void	*p;
1020
+	union cfg_var_value* pval;
1020 1021
 	static str	old_s, new_s;	/* we need the value even
1021 1022
 					after the function returns */
1022 1023
 
... ...
@@ -1031,29 +1032,30 @@ int cfg_diff_next(void **h,
1031 1032
 	/* use the module's handle to access the variable
1032 1033
 	It means that the variable is read from the local config
1033 1034
 	after forking */
1034
-	p = *(changed->group->handle) + changed->var->offset;
1035
+	pval = (union cfg_var_value*)
1036
+			(*(changed->group->handle) + changed->var->offset);
1035 1037
 
1036 1038
 	switch (CFG_VAR_TYPE(changed->var)) {
1037 1039
 	case CFG_VAR_INT:
1038
-		*old_val = (void *)(long)*(int *)p;
1039
-		*new_val = (void *)(long)*(int *)changed->new_val;
1040
+		*old_val = (void *)(long)pval->vint;
1041
+		*new_val = (void *)(long)changed->new_val.vint;
1040 1042
 		break;
1041 1043
 
1042 1044
 	case CFG_VAR_STRING:
1043
-		*old_val = (void *)*(char **)p;
1044
-		*new_val = (void *)*(char **)changed->new_val;
1045
+		*old_val = pval->vp;
1046
+		*new_val = changed->new_val.vp;
1045 1047
 		break;
1046 1048
 
1047 1049
 	case CFG_VAR_STR:
1048
-		memcpy(&old_s, p, sizeof(str));
1050
+		old_s=pval->vstr;
1049 1051
 		*old_val = (void *)&old_s;
1050
-		memcpy(&new_s, changed->new_val, sizeof(str));
1052
+		new_s=changed->new_val.vstr;
1051 1053
 		*new_val = (void *)&new_s;
1052 1054
 		break;
1053 1055
 
1054 1056
 	case CFG_VAR_POINTER:
1055
-		*old_val = *(void **)p;
1056
-		*new_val = *(void **)changed->new_val;
1057
+		*old_val = pval->vp;
1058
+		*new_val = changed->new_val.vp;
1057 1059
 		break;
1058 1060
 
1059 1061
 	}
... ...
@@ -37,6 +37,17 @@
37 37
 #include "cfg.h"
38 38
 #include "cfg_struct.h"
39 39
 
40
+
41
+/* variable values */
42
+union cfg_var_value{
43
+	void* vp;
44
+	long vlong;
45
+	int vint;
46
+	str vstr;
47
+	unsigned char	vraw[1]; /* variable length */
48
+};
49
+
50
+
40 51
 /* linked list of variables with their new values */
41 52
 typedef struct _cfg_changed_var {
42 53
 	cfg_group_t	*group;
... ...
@@ -44,7 +55,7 @@ typedef struct _cfg_changed_var {
44 55
 	struct _cfg_changed_var	*next;
45 56
 
46 57
 	/* blob that contains the new value */
47
-	unsigned char	new_val[1];
58
+	union cfg_var_value new_val; /* variable size */
48 59
 } cfg_changed_var_t;
49 60
 
50 61
 /* callback that is called when a new group is declared */