Browse code

Merge remote branch 'origin/andrei/pointer_alias_warnings'

compile warning fixes

* 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:
cfg/cfg_ctx.h
route.c

Andrei Pelinescu-Onciul authored on 28/01/2010 19:19:49
Showing 8 changed files
... ...
@@ -568,7 +568,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
568 568
 				if (changed->group != group) continue;
569 569
 
570 570
 				memcpy(	temp_handle + changed->var->offset,
571
-					changed->new_val,
571
+					changed->new_val.vraw,
572 572
 					cfg_var_size(changed->var));
573 573
 			}
574 574
 		} else {
... ...
@@ -590,7 +590,8 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
590 590
 	}
591 591
 
592 592
 	/* everything went ok, we can add the new value to the list */
593
-	size = sizeof(cfg_changed_var_t) + cfg_var_size(var) - 1;
593
+	size = sizeof(cfg_changed_var_t) -
594
+			sizeof(((cfg_changed_var_t*)0)->new_val) + cfg_var_size(var);
594 595
 	changed = (cfg_changed_var_t *)shm_malloc(size);
595 596
 	if (!changed) {
596 597
 		LOG(L_ERR, "ERROR: cfg_set_delayed(): not enough shm memory\n");
... ...
@@ -603,7 +604,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
603 603
 	switch (CFG_VAR_TYPE(var)) {
604 604
 
605 605
 	case CFG_VAR_INT:
606
-		*(int *)changed->new_val = (int)(long)v;
606
+		changed->new_val.vint = (int)(long)v;
607 607
 		break;
608 608
 
609 609
 	case CFG_VAR_STRING:
... ...
@@ -611,18 +612,18 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, str *var_name,
611 611
 		s.s = v;
612 612
 		s.len = (s.s) ? strlen(s.s) : 0;
613 613
 		if (cfg_clone_str(&s, &s)) goto error;
614
-		*(char **)changed->new_val = s.s;
614
+		changed->new_val.vp = s.s;
615 615
 		break;
616 616
 
617 617
 	case CFG_VAR_STR:
618 618
 		/* clone the string to shm mem */
619 619
 		s = *(str *)v;
620 620
 		if (cfg_clone_str(&s, &s)) goto error;
621
-		memcpy(changed->new_val, &s, sizeof(str));
621
+		changed->new_val.vstr=s;
622 622
 		break;
623 623
 
624 624
 	case CFG_VAR_POINTER:
625
-		*(void **)changed->new_val = v;
625
+		changed->new_val.vp=v;
626 626
 		break;
627 627
 
628 628
 	}
... ...
@@ -799,7 +800,7 @@ int cfg_commit(cfg_ctx_t *ctx)
799 799
 		}
800 800
 
801 801
 		memcpy(	p,
802
-			changed->new_val,
802
+			changed->new_val.vraw,
803 803
 			cfg_var_size(changed->var));
804 804
 	}
805 805
 
... ...
@@ -865,8 +866,8 @@ int cfg_rollback(cfg_ctx_t *ctx)
865 865
 
866 866
 		if ((CFG_VAR_TYPE(changed->var) == CFG_VAR_STRING)
867 867
 		|| (CFG_VAR_TYPE(changed->var) == CFG_VAR_STR)) {
868
-			if (*(char **)(changed->new_val))
869
-				shm_free(*(char **)(changed->new_val));
868
+			if (changed->new_val.vp)
869
+				shm_free(changed->new_val.vp);
870 870
 		}
871 871
 		shm_free(changed);
872 872
 	}
... ...
@@ -1011,7 +1012,7 @@ int cfg_diff_next(void **h,
1011 1011
 			unsigned int *val_type)
1012 1012
 {
1013 1013
 	cfg_changed_var_t	*changed;
1014
-	void	*p;
1014
+	union cfg_var_value* pval;
1015 1015
 	static str	old_s, new_s;	/* we need the value even
1016 1016
 					after the function returns */
1017 1017
 
... ...
@@ -1026,29 +1027,30 @@ int cfg_diff_next(void **h,
1026 1026
 	/* use the module's handle to access the variable
1027 1027
 	It means that the variable is read from the local config
1028 1028
 	after forking */
1029
-	p = *(changed->group->handle) + changed->var->offset;
1029
+	pval = (union cfg_var_value*)
1030
+			(*(changed->group->handle) + changed->var->offset);
1030 1031
 
1031 1032
 	switch (CFG_VAR_TYPE(changed->var)) {
1032 1033
 	case CFG_VAR_INT:
1033
-		*old_val = (void *)(long)*(int *)p;
1034
-		*new_val = (void *)(long)*(int *)changed->new_val;
1034
+		*old_val = (void *)(long)pval->vint;
1035
+		*new_val = (void *)(long)changed->new_val.vint;
1035 1036
 		break;
1036 1037
 
1037 1038
 	case CFG_VAR_STRING:
1038
-		*old_val = (void *)*(char **)p;
1039
-		*new_val = (void *)*(char **)changed->new_val;
1039
+		*old_val = pval->vp;
1040
+		*new_val = changed->new_val.vp;
1040 1041
 		break;
1041 1042
 
1042 1043
 	case CFG_VAR_STR:
1043
-		memcpy(&old_s, p, sizeof(str));
1044
+		old_s=pval->vstr;
1044 1045
 		*old_val = (void *)&old_s;
1045
-		memcpy(&new_s, changed->new_val, sizeof(str));
1046
+		new_s=changed->new_val.vstr;
1046 1047
 		*new_val = (void *)&new_s;
1047 1048
 		break;
1048 1049
 
1049 1050
 	case CFG_VAR_POINTER:
1050
-		*old_val = *(void **)p;
1051
-		*new_val = *(void **)changed->new_val;
1051
+		*old_val = pval->vp;
1052
+		*new_val = changed->new_val.vp;
1052 1053
 		break;
1053 1054
 
1054 1055
 	}
... ...
@@ -32,14 +32,25 @@
32 32
 #include "cfg.h"
33 33
 #include "cfg_struct.h"
34 34
 
35
-/*! \brief linked list of variables with their new values */
35
+
36
+/* variable values */
37
+union cfg_var_value{
38
+	void* vp;
39
+	long vlong;
40
+	int vint;
41
+	str vstr;
42
+	unsigned char	vraw[1]; /* variable length */
43
+};
44
+
45
+
46
+/** linked list of variables with their new values. */
36 47
 typedef struct _cfg_changed_var {
37 48
 	cfg_group_t	*group;
38 49
 	cfg_mapping_t	*var;
39 50
 	struct _cfg_changed_var	*next;
40 51
 
41 52
 	/* blob that contains the new value */
42
-	unsigned char	new_val[1];
53
+	union cfg_var_value new_val; /* variable size */
43 54
 } cfg_changed_var_t;
44 55
 
45 56
 /*! \brief 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 179
 		goto error;
180 180
 	}
181 181
 
182
-	*(int*)uri_buf = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
182
+	pi=(int*)uri_buf;
183
+	*pi = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
183 184
 	c = uri_buf + 4;
184 185
 	memcpy( c, username.s, username.len);
185 186
 	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 66
 
67 67
 			if ((avp->flags&(AVP_NAME_STR|AVP_VAL_STR)) == AVP_NAME_STR) {
68 68
 				/* avp type str, int value */
69
-				avp_name = &  ((struct str_int_data*)&avp->d.data[0])->name;
69
+				sid = (struct str_int_data*)&avp->d.data[0];
70
+				avp_name = &sid->name;
70 71
 			}
71 72
 			else if ((avp->flags&(AVP_NAME_STR|AVP_VAL_STR)) == (AVP_NAME_STR|AVP_VAL_STR)) {
72 73
 				/* avp type str, str value */
73
-				avp_name = & ((struct str_str_data*)&avp->d.data[0])->name;
74
+				ssd = (struct str_str_data*)&avp->d.data[0];
75
+				avp_name = &ssd->name;
74 76
 			}
75 77
 			else
76 78
 				avp_name = 0;  /* dummy */
... ...
@@ -167,6 +167,7 @@ int send_fd(int unix_socket, void* data, int data_len, int fd)
167 167
 	struct iovec iov[1];
168 168
 	int ret;
169 169
 #ifdef HAVE_MSGHDR_MSG_CONTROL
170
+	int* pi;
170 171
 	struct cmsghdr* cmsg;
171 172
 	/* make sure msg_control will point to properly aligned data */
172 173
 	union {
... ...
@@ -183,7 +184,8 @@ int send_fd(int unix_socket, void* data, int data_len, int fd)
183 183
 	cmsg->cmsg_level = SOL_SOCKET;
184 184
 	cmsg->cmsg_type = SCM_RIGHTS;
185 185
 	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
186
-	*(int*)CMSG_DATA(cmsg)=fd;
186
+	pi=(int*)CMSG_DATA(cmsg);
187
+	*pi=fd;
187 188
 	msg.msg_flags=0;
188 189
 #else
189 190
 	msg.msg_accrights=(caddr_t) &fd;
... ...
@@ -232,6 +234,7 @@ int receive_fd(int unix_socket, void* data, int data_len, int* fd, int flags)
232 232
 	int f;
233 233
 #endif /*NO_MSG_WAITALL */
234 234
 #ifdef HAVE_MSGHDR_MSG_CONTROL
235
+	int* pi;
235 236
 	struct cmsghdr* cmsg;
236 237
 	union{
237 238
 		struct cmsghdr cm;
... ...
@@ -315,7 +318,8 @@ poll_again:
315 315
 			ret=-1;
316 316
 			goto error;
317 317
 		}
318
-		*fd=*((int*) CMSG_DATA(cmsg));
318
+		pi=(int*) CMSG_DATA(cmsg);
319
+		*fd=*pi;
319 320
 	}else{
320 321
 		/*
321 322
 		LOG(L_ERR, "ERROR: receive_fd: no descriptor passed, cmsg=%p,"
... ...
@@ -740,7 +740,7 @@ int fix_actions(struct action* a)
740 740
 						return E_UNSPEC;
741 741
 					}
742 742
 					*/
743
-					if ((ret=fix_rval_expr((void**)&rve))<0)
743
+					if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
744 744
 						goto error;
745 745
 				}
746 746
 				if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
... ...
@@ -820,7 +820,7 @@ int fix_actions(struct action* a)
820 820
 						ret = E_UNSPEC;
821 821
 						goto error;
822 822
 					}
823
-					if ((ret=fix_rval_expr((void**)&rve))<0)
823
+					if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
824 824
 						goto error;
825 825
 				}else{
826 826
 					LOG(L_CRIT, "BUG: fix_actions: null while()"
... ...
@@ -864,7 +864,7 @@ int fix_actions(struct action* a)
864 864
 						ret = E_UNSPEC;
865 865
 						goto error;
866 866
 					}
867
-					if ((ret=fix_rval_expr((void**)&rve))<0)
867
+					if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
868 868
 						goto error;
869 869
 				}else{
870 870
 					LOG(L_CRIT, "BUG: fix_actions: null drop/return"
... ...
@@ -303,6 +303,8 @@ int add_avp_before(avp_t *avp, avp_flags_t flags, avp_name_t name, avp_value_t v
303 303
 /* get value functions */
304 304
 inline str* get_avp_name(avp_t *avp)
305 305
 {
306
+	struct str_int_data *sid;
307
+	struct str_str_data *ssd;
306 308
 	
307 309
 	switch ( avp->flags&(AVP_NAME_STR|AVP_VAL_STR) )
308 310
 	{
... ...
@@ -313,10 +315,12 @@ inline str* get_avp_name(avp_t *avp)
313 313
 			return 0;
314 314
 		case AVP_NAME_STR:
315 315
 			/* avp type str, int value */
316
-			return &((struct str_int_data*)&avp->d.data[0])->name;
316
+			sid = (struct str_int_data*)&avp->d.data[0];
317
+			return &sid->name;
317 318
 		case AVP_NAME_STR|AVP_VAL_STR:
318 319
 			/* avp type str, str value */
319
-			return &((struct str_str_data*)&avp->d.data[0])->name;
320
+			ssd = (struct str_str_data*)&avp->d.data[0];
321
+			return &ssd->name;
320 322
 	}
321 323
 
322 324
 	LOG(L_ERR,"BUG:avp:get_avp_name: unknown avp type (name&val) %d\n",
... ...
@@ -327,6 +331,9 @@ inline str* get_avp_name(avp_t *avp)
327 327
 
328 328
 inline void get_avp_val(avp_t *avp, avp_value_t *val)
329 329
 {
330
+	str *s;
331
+	struct str_int_data *sid;
332
+	struct str_str_data *ssd;
330 333
 	
331 334
 	if (avp==0 || val==0)
332 335
 		return;
... ...
@@ -338,15 +345,18 @@ inline void get_avp_val(avp_t *avp, avp_value_t *val)
338 338
 			break;
339 339
 		case AVP_NAME_STR:
340 340
 			/* avp type str, int value */
341
-			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;
342 343
 			break;
343 344
 		case AVP_VAL_STR:
344 345
 			/* avp type ID, str value */
345
-			val->s = *(str*)&avp->d.data[0];
346
+			s = (str*)&avp->d.data[0];
347
+			val->s = *s;
346 348
 			break;
347 349
 		case AVP_NAME_STR|AVP_VAL_STR:
348 350
 			/* avp type str, str value */
349
-			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;
350 353
 			break;
351 354
 	}
352 355
 }