Browse code

Merge remote branch 'origin/andrei/pointer_alias_warnings' into sr_3.0

* origin/andrei/pointer_alias_warnings:
rr(s): fix recently introduced avp_cookie name bug
core: avp aliasing warning fixes
group: fix pointer aliasing warnings
rr: pointer aliasing warning fixes
db_postgres: pointer aliasing warnings fixes
core: pointer aliasing warnings fixed
cfg: fixed pointer aliasing warnings

Conflicts:
route.c

Andrei Pelinescu-Onciul authored on 28/05/2010 18:50:40
Showing 8 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 */
... ...
@@ -91,17 +91,23 @@ int pg_fld(db_fld_t* fld, char* table)
91 91
 }
92 92
 
93 93
 
94
+union ull {
95
+	uint64_t ui64;
96
+	uint32_t ui32[2];
97
+};
98
+
94 99
 static inline uint64_t htonll(uint64_t in)
95 100
 {
96
-	uint32_t* p = (uint32_t*)∈
97
-	return ((uint64_t)htonl(p[0]) << 32) + (uint64_t)htonl(p[1]);
101
+	union ull* p = (union ull*)&in;
102
+	
103
+	return ((uint64_t)htonl(p->ui32[0]) << 32) + (uint64_t)htonl(p->ui32[1]);
98 104
 }
99 105
 
100 106
 
101 107
 static inline uint64_t ntohll(uint64_t in)
102 108
 {
103
-	uint32_t* p = (uint32_t*)&in;
104
-	return ((uint64_t)ntohl(p[0]) << 32) + (uint64_t)ntohl(p[1]);
109
+	union ull* p = (union ull*)&in;
110
+	return ((uint64_t)ntohl(p->ui32[0]) << 32) + (uint64_t)ntohl(p->ui32[1]);
105 111
 }
106 112
 
107 113
 
... ...
@@ -163,6 +163,7 @@ int get_user_group(struct sip_msg *req, char *user, char *avp)
163 163
 	regmatch_t pmatch;
164 164
 	char *c;
165 165
 	int n;
166
+	int* pi;
166 167
 
167 168
 	if (get_username_domain( req, (group_check_p)user, &username, &domain)!=0){
168 169
 		LM_ERR("failed to get username@domain\n");
... ...
@@ -179,7 +180,8 @@ int get_user_group(struct sip_msg *req, char *user, char *avp)
179 180
 		goto error;
180 181
 	}
181 182
 
182
-	*(int*)uri_buf = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
183
+	pi=(int*)uri_buf;
184
+	*pi = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
183 185
 	c = uri_buf + 4;
184 186
 	memcpy( c, username.s, username.len);
185 187
 	c += username.len;
... ...
@@ -56,6 +56,9 @@ str *rr_get_avp_cookies(void) {
56 56
 	str *avp_name;
57 57
 	str *result = 0;
58 58
 	rr_avp_flags_t avp_flags;
59
+	struct str_int_data *sid;
60
+	struct str_str_data *ssd;
61
+
59 62
 
60 63
 	len = sizeof(crc);
61 64
 	for (avp_list_no=0; avp_list_no<MAX_AVP_DIALOG_LISTS; avp_list_no++) {
... ...
@@ -66,11 +69,13 @@ str *rr_get_avp_cookies(void) {
66 69
 
67 70
 			if ((avp->flags&(AVP_NAME_STR|AVP_VAL_STR)) == AVP_NAME_STR) {
68 71
 				/* avp type str, int value */
69
-				avp_name = &  ((struct str_int_data*)&avp->d.data[0])->name;
72
+				sid = (struct str_int_data*)&avp->d.data[0];
73
+				avp_name = &sid->name;
70 74
 			}
71 75
 			else if ((avp->flags&(AVP_NAME_STR|AVP_VAL_STR)) == (AVP_NAME_STR|AVP_VAL_STR)) {
72 76
 				/* avp type str, str value */
73
-				avp_name = & ((struct str_str_data*)&avp->d.data[0])->name;
77
+				ssd = (struct str_str_data*)&avp->d.data[0];
78
+				avp_name = &ssd->name;
74 79
 			}
75 80
 			else
76 81
 				avp_name = 0;  /* dummy */
... ...
@@ -160,6 +160,7 @@ int send_fd(int unix_socket, void* data, int data_len, int fd)
160 160
 	struct iovec iov[1];
161 161
 	int ret;
162 162
 #ifdef HAVE_MSGHDR_MSG_CONTROL
163
+	int* pi;
163 164
 	struct cmsghdr* cmsg;
164 165
 	/* make sure msg_control will point to properly aligned data */
165 166
 	union {
... ...
@@ -176,7 +177,8 @@ int send_fd(int unix_socket, void* data, int data_len, int fd)
176 177
 	cmsg->cmsg_level = SOL_SOCKET;
177 178
 	cmsg->cmsg_type = SCM_RIGHTS;
178 179
 	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
179
-	*(int*)CMSG_DATA(cmsg)=fd;
180
+	pi=(int*)CMSG_DATA(cmsg);
181
+	*pi=fd;
180 182
 	msg.msg_flags=0;
181 183
 #else
182 184
 	msg.msg_accrights=(caddr_t) &fd;
... ...
@@ -225,6 +227,7 @@ int receive_fd(int unix_socket, void* data, int data_len, int* fd, int flags)
225 227
 	int f;
226 228
 #endif /*NO_MSG_WAITALL */
227 229
 #ifdef HAVE_MSGHDR_MSG_CONTROL
230
+	int* pi;
228 231
 	struct cmsghdr* cmsg;
229 232
 	union{
230 233
 		struct cmsghdr cm;
... ...
@@ -308,7 +311,8 @@ poll_again:
308 311
 			ret=-1;
309 312
 			goto error;
310 313
 		}
311
-		*fd=*((int*) CMSG_DATA(cmsg));
314
+		pi=(int*) CMSG_DATA(cmsg);
315
+		*fd=*pi;
312 316
 	}else{
313 317
 		/*
314 318
 		LOG(L_ERR, "ERROR: receive_fd: no descriptor passed, cmsg=%p,"
... ...
@@ -733,7 +733,7 @@ int fix_actions(struct action* a)
733 733
 						return E_UNSPEC;
734 734
 					}
735 735
 					*/
736
-					if ((ret=fix_rval_expr((void**)&rve))<0)
736
+					if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
737 737
 						goto error;
738 738
 				}
739 739
 				if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
... ...
@@ -813,7 +813,7 @@ int fix_actions(struct action* a)
813 813
 						ret = E_UNSPEC;
814 814
 						goto error;
815 815
 					}
816
-					if ((ret=fix_rval_expr((void**)&rve))<0)
816
+					if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
817 817
 						goto error;
818 818
 				}else{
819 819
 					LOG(L_CRIT, "BUG: fix_actions: null while()"
... ...
@@ -857,7 +857,7 @@ int fix_actions(struct action* a)
857 857
 						ret = E_UNSPEC;
858 858
 						goto error;
859 859
 					}
860
-					if ((ret=fix_rval_expr((void**)&rve))<0)
860
+					if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
861 861
 						goto error;
862 862
 				}else{
863 863
 					LOG(L_CRIT, "BUG: fix_actions: null drop/return"
... ...
@@ -296,6 +296,8 @@ int add_avp_before(avp_t *avp, avp_flags_t flags, avp_name_t name, avp_value_t v
296 296
 /* get value functions */
297 297
 inline str* get_avp_name(avp_t *avp)
298 298
 {
299
+	struct str_int_data *sid;
300
+	struct str_str_data *ssd;
299 301
 	
300 302
 	switch ( avp->flags&(AVP_NAME_STR|AVP_VAL_STR) )
301 303
 	{
... ...
@@ -306,10 +308,12 @@ inline str* get_avp_name(avp_t *avp)
306 308
 			return 0;
307 309
 		case AVP_NAME_STR:
308 310
 			/* avp type str, int value */
309
-			return &((struct str_int_data*)&avp->d.data[0])->name;
311
+			sid = (struct str_int_data*)&avp->d.data[0];
312
+			return &sid->name;
310 313
 		case AVP_NAME_STR|AVP_VAL_STR:
311 314
 			/* avp type str, str value */
312
-			return &((struct str_str_data*)&avp->d.data[0])->name;
315
+			ssd = (struct str_str_data*)&avp->d.data[0];
316
+			return &ssd->name;
313 317
 	}
314 318
 
315 319
 	LOG(L_ERR,"BUG:avp:get_avp_name: unknown avp type (name&val) %d\n",
... ...
@@ -320,6 +324,9 @@ inline str* get_avp_name(avp_t *avp)
320 324
 
321 325
 inline void get_avp_val(avp_t *avp, avp_value_t *val)
322 326
 {
327
+	str *s;
328
+	struct str_int_data *sid;
329
+	struct str_str_data *ssd;
323 330
 	
324 331
 	if (avp==0 || val==0)
325 332
 		return;
... ...
@@ -331,15 +338,18 @@ inline void get_avp_val(avp_t *avp, avp_value_t *val)
331 338
 			break;
332 339
 		case AVP_NAME_STR:
333 340
 			/* avp type str, int value */
334
-			val->n = ((struct str_int_data*)&avp->d.data[0])->val;
341
+			sid = (struct str_int_data*)&avp->d.data[0];
342
+			val->n = sid->val;
335 343
 			break;
336 344
 		case AVP_VAL_STR:
337 345
 			/* avp type ID, str value */
338
-			val->s = *(str*)&avp->d.data[0];
346
+			s = (str*)&avp->d.data[0];
347
+			val->s = *s;
339 348
 			break;
340 349
 		case AVP_NAME_STR|AVP_VAL_STR:
341 350
 			/* avp type str, str value */
342
-			val->s = ((struct str_str_data*)&avp->d.data[0])->val;
351
+			ssd = (struct str_str_data*)&avp->d.data[0];
352
+			val->s = ssd->val;
343 353
 			break;
344 354
 	}
345 355
 }