Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 16 Jul 2007 01:30:23 GMT
From:      Fredrik Lindberg <fli@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 123573 for review
Message-ID:  <200707160130.l6G1UNOT091539@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=123573

Change 123573 by fli@fli_nexus on 2007/07/16 01:29:43

	- Change wcsdup() to own-rolled _wcsup() as the former only
	  exists in -current.
	- Fix a nasty bug in var_expand() which caused it to
	  allocated insufficient memory.
	- Style fixes.

Affected files ...

.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/var.c#2 edit

Differences ...

==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/var.c#2 (text+ko) ====

@@ -113,7 +113,8 @@
 
 	i = vt->vt_size;
 	vt->vt_size++;
-	vt->vt_data = realloc(vt->vt_data, sizeof(struct vt_data) * vt->vt_size);
+	vt->vt_data = realloc(vt->vt_data,
+	    sizeof(struct vt_data) * vt->vt_size);
 
 	if (flags & VD_CHAR) {
 		p = data;
@@ -123,7 +124,7 @@
 	}
 	else if (flags & VD_WCHAR) {
 		if (flags & VD_COPY)
-			wp = wcsdup((wchar_t *)data);
+			wp = _wcsdup((wchar_t *)data);
 		else
 			wp = data;
 	}
@@ -168,7 +169,8 @@
 	for (i = 0; i < varev_table_size; i++) {
 		if (varev_table[i].ve_handler == vt->vt_event) {
 			MTX_LOCK(&varev_table[i], ve_mtx);
-			TAILQ_INSERT_TAIL(&varev_table[i].ve_vartags, vt, vt_evlist);
+			TAILQ_INSERT_TAIL(&varev_table[i].ve_vartags,
+			    vt, vt_evlist);
 			MTX_UNLOCK(&varev_table[i], ve_mtx);
 			break;
 		}
@@ -225,14 +227,18 @@
 	error = vt->vt_update(vt, flags);
 	if (error == 0) {
 		if (vt->vt_size == 1) {
-			dprintf(DEBUG_VAR, "Variable %s set to %ls", vt->vt_name,
-			    vt->vt_data[0].vtd_str);
+			dprintf(DEBUG_VAR, "Variable %s set to %ls %x",
+			    vt->vt_name, vt->vt_data[0].vtd_str,
+vt->vt_data[0].vtd_str);
 		}
 		else {
 			dprintf(DEBUG_VAR, "Variable %s set to", vt->vt_name);
+#ifdef DEBUG
 			for (i = 0; i < vt->vt_size; i++) {
-				dprintf(DEBUG_VAR, "\t%ls", vt->vt_data[i].vtd_str);
+				dprintf(DEBUG_VAR, "\t%ls",
+				    vt->vt_data[i].vtd_str);
 			}
+#endif
 		}
 	}
 	else if (error < 0) {
@@ -380,7 +386,8 @@
 			    ifm->ifm_type != RTM_IFINFO)
 				continue;
 			MTX_LOCK(ve, ve_mtx);
-			TAILQ_FOREACH_SAFE(vt, &ve->ve_vartags, vt_evlist, vt2) {
+			TAILQ_FOREACH_SAFE(vt, &ve->ve_vartags, vt_evlist,
+			    vt2) {
 				update_var(vt, VTUF_UPDATE);
 			}
 			MTX_UNLOCK(ve, ve_mtx);
@@ -415,26 +422,26 @@
 	wp = NULL;
 	error = getifaddrs(&ifap);
 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
-		if (ifa->ifa_addr->sa_family == family) {
-			if (strncmp(ifa->ifa_name, ifnam, IFNAMSIZ) == 0) {
+		if (ifa->ifa_addr->sa_family != family)
+			continue;
+		if (strncmp(ifa->ifa_name, ifnam, IFNAMSIZ) != 0)
+			continue;
 
-				wp = realloc(wp, sizeof(wchar_t *) * (*len + 1));
-				sa = ifa->ifa_addr;
-				sinaddr = NULL;
-				if (family == AF_INET)
-					sinaddr = &((struct sockaddr_in *)sa)->sin_addr;
+		wp = realloc(wp, sizeof(wchar_t *) * (*len + 1));
+		sa = ifa->ifa_addr;
+		sinaddr = NULL;
+		if (family == AF_INET)
+			sinaddr = &((struct sockaddr_in *)sa)->sin_addr;
 #ifdef INET6
-				else if (family == AF_INET6)
-					sinaddr = &((struct sockaddr_in6 *)sa)->sin6_addr;
+		else if (family == AF_INET6)
+			sinaddr = &((struct sockaddr_in6 *)sa)->sin6_addr;
 #endif
-				assert(sinaddr != NULL);
-				inet_ntop(family, sinaddr, addr, __ADDRSTRLEN);
-				plen = strlen(addr) + 1;
-				wp[*len] = malloc(plen * sizeof(wchar_t));
-				mbstowcs(wp[*len], addr, plen);
-				(*len)++;
-			}
-		}
+		assert(sinaddr != NULL);
+		inet_ntop(family, sinaddr, addr, __ADDRSTRLEN);
+		plen = strlen(addr) + 1;
+		wp[*len] = malloc(plen * sizeof(wchar_t));
+		mbstowcs(wp[*len], addr, plen);
+		(*len)++;
 	}
 	freeifaddrs(ifap);
 	return (wp);
@@ -511,7 +518,7 @@
 {
 	wchar_t **addrs;
 	int error = 0;
-	size_t alen, i;
+	size_t alen, i, j;
 	struct md_if *mif;
 	struct dbr *dbr;
 	struct vars *v;
@@ -544,9 +551,11 @@
 		if (alen != vt->vt_size)
 			clr_data(vt);
 		else {
-			for (i = 0; i < alen; i++)
-				if (wcscmp(addrs[i], vt->vt_data[i].vtd_str) != 0)
+			for (i = 0; i < alen; i++) {
+				j = wcscmp(addrs[i], vt->vt_data[i].vtd_str);
+				if (j != 0)
 					break;
+			}
 			if (alen == i) {
 				error = 1;
 				for (i = 0; i < alen; i++)
@@ -883,87 +892,103 @@
 
 			break;
 		case VARIABLE:
-			if (*q == ')') {
-				varlen = (q - var);
-				vname = malloc(varlen + 1);
-				wcstombs(vname, var, varlen);
-				vname[varlen] = '\0';
-				vt = get_variable(vars, vname, varlen);
-				if (vt == NULL) {
-					logger(LOG_ERR, "Unknown variable %s", vname);
-					free(vname);
-					state = NORMAL;
-					continue;
-				}
-				else if (!(vt->vt_flags & VT_OK)) {
-					dprintf(DEBUG_VAR, "Variable data on %s not ok", vname);
-					free(vname);
-					state = NORMAL;
-					goto error;
-				}
+			if (*q != ')')
+				break;
 
-				RW_RLOCK(vt, vt_lock);
+			varlen = (q - var);
+			vname = malloc(varlen + 1);
+			wcstombs(vname, var, varlen);
+			vname[varlen] = '\0';
+			vt = get_variable(vars, vname, varlen);
+			if (vt == NULL) {
+				logger(LOG_ERR,
+				    "Unknown variable %s", vname);
+				free(vname);
+				state = NORMAL;
+				continue;
+			}
+			else if (!(vt->vt_flags & VT_OK)) {
+				dprintf(DEBUG_VAR,
+				    "Variable data on %s not ok",
+				    vname);
+				free(vname);
+				state = NORMAL;
+				goto error;
+			}
 
-				/*
-				 * For each existing string we append all variable data
-				 * returned from this variable.
-				 * (n-1) new strings are created in each loop where n
-				 * is the number of data elements the variable holds.
-				 */
-				k = vtdl;
-				for (i = 0; i < vtdl; i++) {
-					j = vt->vt_size;
-					while (--j > 0) {
-						alen = realloc(alen, sizeof(size_t) * (k+1));
-						alen[k] = alen[i];
-						vtd = realloc(vtd, sizeof(struct vt_data) * (k+1));
+			RW_RLOCK(vt, vt_lock);
 
-						/*
-						 * Use the data in string (i) as template to
-						 * our new string
-						 */
-						vtd[k].vtd_str = malloc(alen[k] * sizeof(wchar_t));
-						vtd[k].vtd_len = vtd[i].vtd_len;
-						memcpy(vtd[k].vtd_str, vtd[i].vtd_str,
-							vtd[i].vtd_len * sizeof(wchar_t));
+			/*
+			 * For each existing string we append all
+			 * variable data returned from this variable.
+			 * (n-1) new strings are created in each loop
+			 * where n is the number of data elements the
+			 * variable holds.
+			 */
+			k = vtdl;
+			for (i = 0; i < vtdl; i++) {
+				j = vt->vt_size;
+				while (--j > 0) {
+					alen = realloc(alen,
+					    sizeof(size_t) * (k+1));
+					alen[k] = alen[i];
+					vtd = realloc(vtd,
+					    sizeof(struct vt_data) * (k+1));
 
-						/*
-						 * Expand (if needed) and append variable data
-						 * to this string.
-						 */
-						if ((vtd[k].vtd_len + vt->vt_data[j].vtd_len) >=
-						    alen[k]) {
-							alen[k] *= 2;
-							vtd[k].vtd_str = realloc(vtd[k].vtd_str,
-							    alen[k] * sizeof(wchar_t));
-						}
-						tmp = vtd[k].vtd_str + vtd[k].vtd_len;
-						memcpy(tmp, vt->vt_data[j].vtd_str,
-							vt->vt_data[j].vtd_len * sizeof(wchar_t));
-						vtd[k].vtd_len += vt->vt_data[j].vtd_len;
-						k++;
-					}
+					/*
+					 * Use the data in string (i) as
+					 * template to our new string
+					 */
+					vtd[k].vtd_str =
+					    malloc(alen[k] * sizeof(wchar_t));
+					vtd[k].vtd_len = vtd[i].vtd_len;
+					memcpy(vtd[k].vtd_str, vtd[i].vtd_str,
+					    vtd[i].vtd_len * sizeof(wchar_t));
 
 					/*
-					 * At this point there are only one variable data
-					 * element left, append it to the existing string (i).
+					 * Expand (if needed) and append
+					 * variable data to this string.
 					 */
-					if ((vtd[i].vtd_len + vt->vt_data[i].vtd_len) >= alen[i]) {
-						alen[i] *= 2;
-						vtd[i].vtd_str = realloc(vtd[i].vtd_str,
-						    alen[i] * sizeof(wchar_t));
+					if ((vtd[k].vtd_len +
+					    vt->vt_data[j].vtd_len) >=
+					    alen[k]) {
+						alen[k] = vtd[k].vtd_len +
+						    vt->vt_data[j].vtd_len + 1;
+						vtd[k].vtd_str =
+						    realloc(vtd[k].vtd_str,
+						    alen[k] * sizeof(wchar_t));
 					}
-					tmp = vtd[i].vtd_str + vtd[i].vtd_len;
+					tmp = vtd[k].vtd_str + vtd[k].vtd_len;
 					memcpy(tmp, vt->vt_data[j].vtd_str,
-						vt->vt_data[j].vtd_len * sizeof(wchar_t));
-					vtd[i].vtd_len += vt->vt_data[j].vtd_len;
+					    vt->vt_data[j].vtd_len *
+					    sizeof(wchar_t));
+					vtd[k].vtd_len +=
+					    vt->vt_data[j].vtd_len;
+					k++;
+				}
+
+				/*
+				 * At this point there are only one
+				 * variable data element left, append
+				 * it to the existing string (i).
+				 */
+				if ((vtd[i].vtd_len + vt->vt_data[i].vtd_len) >=
+				    alen[i]) {
+					alen[i] = vtd[i].vtd_len +
+					    vt->vt_data[i].vtd_len + 1;
+					vtd[i].vtd_str = realloc(vtd[i].vtd_str,
+					    alen[i] * sizeof(wchar_t));
+				}
+				tmp = vtd[i].vtd_str + vtd[i].vtd_len;
+				memcpy(tmp, vt->vt_data[j].vtd_str,
+				    vt->vt_data[j].vtd_len * sizeof(wchar_t));
+				vtd[i].vtd_len += vt->vt_data[j].vtd_len;
 					
-				}
-				RW_UNLOCK(vt, vt_lock);
-				vtdl = k;
-				free(vname);
-				state = NORMAL;
 			}
+			RW_UNLOCK(vt, vt_lock);
+			vtdl = k;
+			free(vname);
+			state = NORMAL;
 			break;
 		}
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200707160130.l6G1UNOT091539>