[zaptel-commits] jpeeler: branch 1.2 r3833 - in /branches/1.2: ./ ppp/ wct4xxp/

SVN commits to the Zaptel project zaptel-commits at lists.digium.com
Fri Feb 15 17:33:44 CST 2008


Author: jpeeler
Date: Fri Feb 15 17:33:44 2008
New Revision: 3833

URL: http://svn.digium.com/view/zaptel?view=rev&rev=3833
Log:
changed all instances of strncpy to zap_copy_string

Modified:
    branches/1.2/fxotune.c
    branches/1.2/jpah.h
    branches/1.2/ppp/zaptel.c
    branches/1.2/sethdlc-new.c
    branches/1.2/sethdlc.c
    branches/1.2/tonezone.c
    branches/1.2/tor2ee.c
    branches/1.2/wct4xxp/wct4xxp-diag.c
    branches/1.2/zaptel-base.c
    branches/1.2/zaptel.h
    branches/1.2/ztcfg-dude.c
    branches/1.2/ztcfg.c
    branches/1.2/ztd-eth.c
    branches/1.2/ztdynamic.c
    branches/1.2/ztmonitor.c
    branches/1.2/zttranscode.c

Modified: branches/1.2/fxotune.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/fxotune.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/fxotune.c (original)
+++ branches/1.2/fxotune.c Fri Feb 15 17:33:44 2008
@@ -167,7 +167,7 @@
 	memset(&dop, 0, sizeof(dop));
 	dop.op = ZT_DIAL_OP_REPLACE;
 	dop.dialstr[0] = 'T';
-	strncpy(dop.dialstr + 1, info->dialstr, sizeof(dop.dialstr) - 1);
+	zap_copy_string(dop.dialstr + 1, info->dialstr, sizeof(dop.dialstr));
 
 	if (ioctl(info->device, ZT_DIAL, &dop)) {
 		fprintf(stderr, "Unable to dial!\n");

Modified: branches/1.2/jpah.h
URL: http://svn.digium.com/view/zaptel/branches/1.2/jpah.h?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/jpah.h (original)
+++ branches/1.2/jpah.h Fri Feb 15 17:33:44 2008
@@ -49,7 +49,7 @@
 
 static void echo_can_identify(char *buf, size_t len)
 {
-	strncpy(buf, "JP1", len);
+	zap_copy_string(buf, "JP1", len);
 }
 
 static void echo_can_shutdown(void)

Modified: branches/1.2/ppp/zaptel.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/ppp/zaptel.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/ppp/zaptel.c (original)
+++ branches/1.2/ppp/zaptel.c Fri Feb 15 17:33:44 2008
@@ -223,7 +223,7 @@
 	}
     }
 
-    strncpy(devnam, cp, sizeof(devnam) - 1);
+    zap_copy_string(devnam, cp, sizeof(devnam));
 
     info("Using zaptel device '%s'\n", devnam);
 

Modified: branches/1.2/sethdlc-new.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/sethdlc-new.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/sethdlc-new.c (original)
+++ branches/1.2/sethdlc-new.c Fri Feb 15 17:33:44 2008
@@ -673,7 +673,7 @@
 	if (sock < 0)
 		error("Unable to create socket: %s\n", strerror(errno));
   
-	strncpy(req.ifr_name, argv[1], sizeof(req.ifr_name) - 1); /* Device name */
+	zap_copy_string(req.ifr_name, argv[1], sizeof(req.ifr_name)); /* Device name */
 
 	if (argc == 2)
 		show_port();

Modified: branches/1.2/sethdlc.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/sethdlc.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/sethdlc.c (original)
+++ branches/1.2/sethdlc.c Fri Feb 15 17:33:44 2008
@@ -293,7 +293,7 @@
 	if (sock<0)
 		error("Unable to create socket: %s\n", strerror(errno));
   
-	strncpy(req.ifr_name, argv[1], sizeof(req.ifr_name) - 1); /* Device name */
+	zap_copy_string(req.ifr_name, argv[1], sizeof(req.ifr_name)); /* Device name */
 
 	if (argc == 2) {
 		show_port(sock);

Modified: branches/1.2/tonezone.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/tonezone.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/tonezone.c (original)
+++ branches/1.2/tonezone.c Fri Feb 15 17:33:44 2008
@@ -274,7 +274,7 @@
 	}
 	h->count = count;
 	h->zone = z->zone;
-	strncpy(h->name, z->description, sizeof(h->name));
+	zap_copy_string(h->name, z->description, sizeof(h->name));
 	x = z->zone;
 	ioctl(fd, ZT_FREEZONE, &x);
 	res = ioctl(fd, ZT_LOADZONE, h);

Modified: branches/1.2/tor2ee.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/tor2ee.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/tor2ee.c (original)
+++ branches/1.2/tor2ee.c Fri Feb 15 17:33:44 2008
@@ -779,7 +779,7 @@
 
 	/* Store revision number */
 	memset(ee->revision, 0, sizeof(ee->revision));
-	strncpy(ee->revision, revision, sizeof(ee->revision));
+	zap_copy_string(ee->revision, revision, sizeof(ee->revision));
 
 	/* Calculate FCS on revision and data */
 	ee->crc16 = htons(calc_crc16((char *)ee, sizeof(*ee) - 2));
@@ -1051,7 +1051,7 @@
 			exit(1);
 		}
 		modify(&ee, reg, val);
-		strncpy(tmp, ee.revision, sizeof(tmp));
+		zap_copy_string(tmp, ee.revision, sizeof(tmp));
 		if (write_eeprom_file(writefile, tmp, &ee)) {
 			fprintf(stderr, "Unable to write EEPROM file\n");
 			closemem();

Modified: branches/1.2/wct4xxp/wct4xxp-diag.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/wct4xxp/wct4xxp-diag.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/wct4xxp/wct4xxp-diag.c (original)
+++ branches/1.2/wct4xxp/wct4xxp-diag.c Fri Feb 15 17:33:44 2008
@@ -378,7 +378,7 @@
 		exit(1);
 	}
 	if (*(argv[1]) == '/')
-		strncpy(fn, argv[1], sizeof(fn) - 1);
+		zap_copy_string(fn, argv[1], sizeof(fn));
 	else
 		snprintf(fn, sizeof(fn), "/dev/zap/%d", atoi(argv[1]));
 	fd = open(fn, O_RDWR);

Modified: branches/1.2/zaptel-base.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/zaptel-base.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/zaptel-base.c (original)
+++ branches/1.2/zaptel-base.c Fri Feb 15 17:33:44 2008
@@ -1212,7 +1212,7 @@
 	/* Set up the path of the file/link itself */
 	tmp_offset = devfs_generate_path(zaptel_devfs_dir, tmp, sizeof(tmp) - 1);
 	sprintf(buf, "/%d", chan->channo);
-	strncpy(path, tmp+tmp_offset, sizeof(path) - 1);
+	zap_copy_string(path, tmp+tmp_offset, sizeof(path));
 	strncat(path, buf, sizeof(path) - 1);
 
 	err = devfs_mk_symlink(NULL, path, DEVFS_FL_DEFAULT, link+link_offset, &chan->fhandle_symlink, NULL);
@@ -2514,7 +2514,7 @@
 	memset(slab, 0, size);
 	/* Grab the zone */
 	z = (struct zt_zone *)slab;
-	strncpy(z->name, th.name, sizeof(z->name) - 1);
+	zap_copy_string(z->name, th.name, sizeof(z->name));
 	for (x=0;x<ZT_MAX_CADENCE;x++)
 		z->ringcadence[x] = th.ringcadence[x];
 	data += sizeof(struct zt_tone_def_header);
@@ -2928,7 +2928,7 @@
 		stack.param.pulseaftertime = chan->pulseaftertime;
 		if (chan->span) stack.param.spanno = chan->span->spanno;
 			else stack.param.spanno = 0;
-		strncpy(stack.param.name, chan->name, sizeof(stack.param.name) - 1);
+		zap_copy_string(stack.param.name, chan->name, sizeof(stack.param.name));
 		stack.param.chanpos = chan->chanpos;
 		/* Return current law */
 		if (chan->xlaw == __zt_alaw)
@@ -3037,8 +3037,8 @@
 		stack.span.spanno = i; /* put the span # in here */
 		stack.span.totalspans = 0;
 		if (maxspans) stack.span.totalspans = maxspans - 1; /* put total number of spans here */
-		strncpy(stack.span.desc, spans[i]->desc, sizeof(stack.span.desc) - 1);
-		strncpy(stack.span.name, spans[i]->name, sizeof(stack.span.name) - 1);
+		zap_copy_string(stack.span.desc, spans[i]->desc, sizeof(stack.span.desc));
+		zap_copy_string(stack.span.name, spans[i]->name, sizeof(stack.span.name));
 		stack.span.alarms = spans[i]->alarms;		/* get alarm status */
 		stack.span.bpvcount = spans[i]->bpvcount;	/* get BPV count */
 		stack.span.rxlevel = spans[i]->rxlevel;	/* get rx level */
@@ -3577,7 +3577,7 @@
 				rv = -EBUSY;
 				break;
 			   }
-			strncpy(chan->txdialbuf + strlen(chan->txdialbuf), stack.tdo.dialstr, ZT_MAX_DTMF_BUF - strlen(chan->txdialbuf));
+			zap_copy_string(chan->txdialbuf + strlen(chan->txdialbuf), stack.tdo.dialstr, ZT_MAX_DTMF_BUF - strlen(chan->txdialbuf));
 			if (!chan->dialing)
 			   {
 				chan->dialing = 1;

Modified: branches/1.2/zaptel.h
URL: http://svn.digium.com/view/zaptel/branches/1.2/zaptel.h?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/zaptel.h (original)
+++ branches/1.2/zaptel.h Fri Feb 15 17:33:44 2008
@@ -1733,4 +1733,30 @@
 /*! Maximum audio mask */
 #define ZT_FORMAT_AUDIO_MASK	((1 << 16) - 1)
 
+/*!
+	\brief Size-limited null-terminating string copy.
+	\param dst The destination buffer
+	\param src The source string
+	\param size The size of the destination buffer
+	\return Nothing.
+
+	This is similar to \a strncpy, with two important differences:
+	- the destination buffer will \b always be null-terminated
+	- the destination buffer is not filled with zeros past the copied string length
+	These differences make it slightly more efficient, and safer to use since it will
+	not leave the destination buffer unterminated. There is no need to pass an artificially
+	reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
+	to be initialized to zeroes prior to calling this function.
+*/
+static inline void zap_copy_string(char *dst, const char *src, unsigned int size)
+{
+	while (*src && size) {
+		*dst++ = *src++;
+		size--;
+	}
+	if (__builtin_expect(!size, 0))
+		dst--;
+	*dst = '\0';
+}
+
 #endif /* _LINUX_ZAPTEL_H */

Modified: branches/1.2/ztcfg-dude.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/ztcfg-dude.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/ztcfg-dude.c (original)
+++ branches/1.2/ztcfg-dude.c Fri Feb 15 17:33:44 2008
@@ -182,8 +182,8 @@
 	}
 
 
-	strncpy(zds[numdynamic].driver, realargs[0], sizeof(zds[numdynamic].driver));
-	strncpy(zds[numdynamic].addr, realargs[1], sizeof(zds[numdynamic].addr));
+	zap_copy_string(zds[numdynamic].driver, realargs[0], sizeof(zds[numdynamic].driver));
+	zap_copy_string(zds[numdynamic].addr, realargs[1], sizeof(zds[numdynamic].addr));
 	zds[numdynamic].numchans = chans;
 	zds[numdynamic].timing = timing;
 	
@@ -291,7 +291,7 @@
 	for (x=0;x<res;x++) {
 		if (strchr(args[x], '-')) {
 			/* It's a range */
-			strncpy(argcopy, args[x], sizeof(argcopy));
+			zap_copy_string(argcopy, args[x], sizeof(argcopy));
 			res2 = parseargs(argcopy, range, 2, '-');
 			if (res2 != 2) {
 				error("Syntax error in range '%s'.  Should be <val1>-<val2>.\n", args[x]);
@@ -566,7 +566,7 @@
 		error("Too many tone zones specified\n");
 		return 0;
 	}
-	strncpy(zonestoload[numzones++], args, sizeof(zonestoload[0]));
+	zap_copy_string(zonestoload[numzones++], args, sizeof(zonestoload[0]));
 	return 0;
 }
 

Modified: branches/1.2/ztcfg.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/ztcfg.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/ztcfg.c (original)
+++ branches/1.2/ztcfg.c Fri Feb 15 17:33:44 2008
@@ -256,8 +256,8 @@
 	}
 
 
-	strncpy(zds[numdynamic].driver, realargs[0], sizeof(zds[numdynamic].driver));
-	strncpy(zds[numdynamic].addr, realargs[1], sizeof(zds[numdynamic].addr));
+	zap_copy_string(zds[numdynamic].driver, realargs[0], sizeof(zds[numdynamic].driver));
+	zap_copy_string(zds[numdynamic].addr, realargs[1], sizeof(zds[numdynamic].addr));
 	zds[numdynamic].numchans = chans;
 	zds[numdynamic].timing = timing;
 	
@@ -365,7 +365,7 @@
 	for (x=0;x<res;x++) {
 		if (strchr(args[x], '-')) {
 			/* It's a range */
-			strncpy(argcopy, args[x], sizeof(argcopy));
+			zap_copy_string(argcopy, args[x], sizeof(argcopy));
 			res2 = parseargs(argcopy, range, 2, '-');
 			if (res2 != 2) {
 				error("Syntax error in range '%s'.  Should be <val1>-<val2>.\n", args[x]);
@@ -612,7 +612,7 @@
 		error("Too many tone zones specified\n");
 		return 0;
 	}
-	strncpy(zonestoload[numzones++], args, sizeof(zonestoload[0]));
+	zap_copy_string(zonestoload[numzones++], args, sizeof(zonestoload[0]));
 	return 0;
 }
 
@@ -868,7 +868,7 @@
 	for (x=0;x<res;x++) {
 		if (strchr(args[x], '-')) {
 			/* It's a range */
-			strncpy(argcopy, args[x], sizeof(argcopy));
+			zap_copy_string(argcopy, args[x], sizeof(argcopy));
 			res2 = parseargs(argcopy, range, 2, '-');
 			if (res2 != 2) {
 				error("Syntax error in range '%s'.  Should be <val1>-<val2>.\n", args[x]);

Modified: branches/1.2/ztd-eth.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/ztd-eth.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/ztd-eth.c (original)
+++ branches/1.2/ztd-eth.c Fri Feb 15 17:33:44 2008
@@ -297,12 +297,12 @@
 		memset(z, 0, sizeof(struct ztdeth));
 
 		/* Address should be <dev>/<macaddr>[/subaddr] */
-		strncpy(tmp, addr, sizeof(tmp) - 1);
+		zap_copy_string(tmp, addr, sizeof(tmp));
 		tmp2 = strchr(tmp, '/');
 		if (tmp2) {
 			*tmp2 = '\0';
 			tmp2++;
-			strncpy(z->ethdev, tmp, sizeof(z->ethdev) - 1);
+			zap_copy_string(z->ethdev, tmp, sizeof(z->ethdev));
 		} else {
 			printk("Invalid TDMoE address (no device) '%s'\n", addr);
 			kfree(z);

Modified: branches/1.2/ztdynamic.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/ztdynamic.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/ztdynamic.c (original)
+++ branches/1.2/ztdynamic.c Fri Feb 15 17:33:44 2008
@@ -579,8 +579,8 @@
 	memset(z->msgbuf, 0, bufsize);
 
 	/* Setup parameters properly assuming we're going to be okay. */
-	strncpy(z->dname, zds->driver, sizeof(z->dname) - 1);
-	strncpy(z->addr, zds->addr, sizeof(z->addr) - 1);
+	zap_copy_string(z->dname, zds->driver, sizeof(z->dname));
+	zap_copy_string(z->addr, zds->addr, sizeof(z->addr));
 	z->timing = zds->timing;
 	sprintf(z->span.name, "ZTD/%s/%s", zds->driver, zds->addr);
 	sprintf(z->span.desc, "Dynamic '%s' span at '%s'", zds->driver, zds->addr);

Modified: branches/1.2/ztmonitor.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/ztmonitor.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/ztmonitor.c (original)
+++ branches/1.2/ztmonitor.c Fri Feb 15 17:33:44 2008
@@ -155,10 +155,10 @@
 	memset(bar+barlen+2, '>', 1);
 	memset(bar+barlen+3, '\0', 1);
 
-	strncpy(bar+(barlen/2), "(RX)", 4);
+	zap_copy_string(bar+(barlen/2), "(RX)", 4);
 	printf("%s", bar);
 
-	strncpy(bar+(barlen/2), "(TX)", 4);
+	zap_copy_string(bar+(barlen/2), "(TX)", 4);
 	printf(" %s\n", bar);
 }
 

Modified: branches/1.2/zttranscode.c
URL: http://svn.digium.com/view/zaptel/branches/1.2/zttranscode.c?view=diff&rev=3833&r1=3832&r2=3833
==============================================================================
--- branches/1.2/zttranscode.c (original)
+++ branches/1.2/zttranscode.c Fri Feb 15 17:33:44 2008
@@ -311,7 +311,7 @@
 	if (!tc)
 		return -ENOSYS;
 
-	strncpy(info.name, tc->name, sizeof(info.name) - 1);
+	zap_copy_string(info.name, tc->name, sizeof(info.name));
 	info.numchannels = tc->numchannels;
 	info.srcfmts = tc->srcfmts;
 	info.dstfmts = tc->dstfmts;




More information about the zaptel-commits mailing list