Update of /cvsroot/monetdb/pathfinder/runtime
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv30146/runtime
Modified Files:
Tag: XQuery_0-22
pathfinder.mx pf_support.mx serialize.mx shredder.mx shttpd.c
Log Message:
Reevaluated the use of (v)snprintf.
On Windows, as on Linux, the size argument is the size of the buffer
to which is printed. The difference between Linux and Windows is that
on Linux if the data doesn't fit, the buffer is still NUL-terminated
(and truncated, obviously), but on Windows the data is truncated but
not NUL-terminated.
The upshot of this is that we can use the correctly-sized buffers, but
we must make sure that truncated buffers are properly NUL-terminated.
Index: shttpd.c
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/shttpd.c,v
retrieving revision 1.33.8.1
retrieving revision 1.33.8.2
diff -u -d -r1.33.8.1 -r1.33.8.2
--- shttpd.c 22 Feb 2008 10:39:33 -0000 1.33.8.1
+++ shttpd.c 22 Feb 2008 15:13:21 -0000 1.33.8.2
@@ -614,17 +614,18 @@
return (0);
va_start(ap, fmt);
- n = vsnprintf(buf, buflen-1, fmt, ap);
+ n = vsnprintf(buf, buflen, fmt, ap);
va_end(ap);
if (n < 0) {
elog(ERR_DEBUG, "%s: snprintf returned -1, "
"fmt [%s]", "Snprintf", fmt);
n = 0;
- } else if (n >= (int) buflen - 1) {
+ buf[buflen - 1] = 0;
+ } else if (n >= (int) buflen) {
elog(ERR_DEBUG, "%s: truncating from %d to %u [%s]",
- "Snprintf", n, buflen - 2, buf);
- n = buflen - 2;
+ "Snprintf", n, buflen - 1, buf);
+ n = buflen - 1;
buf[n] = '\0';
}
@@ -757,7 +758,9 @@
n = strftime(msg, sizeof(msg),"%d/%b/%Y %H:%M:%S ", localtime(&now));
va_start(ap, fmt);
- n += vsnprintf(msg + n, sizeof(msg) - n - 1, fmt, ap);
+ vsnprintf(msg + n, sizeof(msg) - n, fmt, ap);
+ msg[sizeof(msg) - 1] = 0;
+ n = strlen(msg);
va_end(ap);
if (n > sizeof(msg) - 2)
@@ -1258,9 +1261,9 @@
"HTTP/1.1 %d %s\r\n%s%s\r\n%d ",
status, descr, headers, headers[0] == '\0' ? "" : "\r\n", status);
va_start(ap, fmt);
- n += vsnprintf(msg + n, sizeof(msg) - n - 1, fmt, ap);
- if (n >= (int) sizeof(msg) - 1)
- n = sizeof(msg) - 1;
+ vsnprintf(msg + n, sizeof(msg) - n, fmt, ap);
+ msg[sizeof(msg) - 1] = 0;
+ n = (int) strlen(msg);
va_end(ap);
mystrlcpy(c->local.buf, msg, sizeof(c->local.buf));
c->local.nread = n;
@@ -1429,14 +1432,16 @@
strcmp(dp->d_name, HTPASSWD) == 0)
continue;
- (void) snprintf(file, sizeof(file)-1,
+ (void) snprintf(file, sizeof(file),
"%s%s%s",c->path, slash, dp->d_name);
+ file[sizeof(file) - 1] = 0;
(void) mystat(file, &st);
if (S_ISDIR(st.st_mode))
- snprintf(size,sizeof(size)-1,"
%s","<DIR>");
+ snprintf(size,sizeof(size)," %s","<DIR>");
else
- (void) snprintf(size, sizeof(size)-1,"%10.2f kB",
+ (void) snprintf(size, sizeof(size),"%10.2f kB",
(float) st.st_size / 1024);
+ size[sizeof(size) - 1] = 0;
(void) strftime(mod, sizeof(mod),
"%d-%b-%Y %H:%M", localtime(&st.st_mtime));
@@ -1544,8 +1549,9 @@
fmt = "%a, %d %b %Y %H:%M:%S GMT";
(void) strftime(date, sizeof(date), fmt, localtime(&now));
(void) strftime(lm, sizeof(lm), fmt, localtime(&c->st.st_mtime));
- (void) snprintf(etag, sizeof(etag)-1, "%lx.%lx",
+ (void) snprintf(etag, sizeof(etag), "%lx.%lx",
(unsigned long) c->st.st_mtime, (unsigned long) c->st.st_size);
+ etag[sizeof(etag) - 1] = 0;
/* Local read buffer should be empty */
c->local.nread = c->shlength = Snprintf(c->local.buf,
@@ -1996,6 +2002,7 @@
FILE *fp = NULL, *fp2 = NULL;
(void) snprintf(tmp, sizeof(tmp)-1, "%s.tmp", fname);
+ tmp[sizeof(tmp) - 1] = 0;
/* Create the file if does not exist */
if ((fp = fopen(fname, "a+")))
@@ -2014,8 +2021,9 @@
strcmp(domain, d) == 0) {
found++;
md5(ha1, user, ":", domain, ":", pass, NULL);
- (void) snprintf(line, sizeof(line)-1,
+ (void) snprintf(line, sizeof(line),
"%s:%s:%s\n", user, domain, ha1);
+ line[sizeof(line) - 1] = 0;
}
(void) fprintf(fp2, "%s", line);
}
@@ -2023,8 +2031,9 @@
/* If new user, just add it */
if (found == 0) {
md5(ha1, user, ":", domain, ":", pass, NULL);
- (void) snprintf(line, sizeof(line)-1,
+ (void) snprintf(line, sizeof(line),
"%s:%s:%s\n", user, domain, ha1);
+ line[sizeof(line) - 1] = 0;
(void) fprintf(fp2, "%s", line);
}
@@ -2144,15 +2153,17 @@
if (STROPT(OPT_HTPASSWD)) {
/* Use global passwords file */
- (void) snprintf(name, sizeof(name)-1, "%s",
STROPT(OPT_HTPASSWD));
+ (void) snprintf(name, sizeof(name), "%s", STROPT(OPT_HTPASSWD));
+ name[sizeof(name) - 1] = 0;
} else {
/* Try to find .htpasswd in requested directory */
for (p = path, e = p + strlen(p) - 1; e > p; e--)
if (*e == '/')
break;
assert(*e == '/');
- (void) snprintf(name, sizeof(name)-1, "%.*s/%s",
+ (void) snprintf(name, sizeof(name), "%.*s/%s",
(int) (e - p), p, HTPASSWD);
+ name[sizeof(name) - 1] = 0;
/* Fix directory separators */
copypath(name, name, strlen(name) + 1);
@@ -2269,8 +2280,9 @@
}
/* Prepare command line */
- (void) snprintf(cmdline, sizeof(cmdline)-1, "%s%s%s",
+ (void) snprintf(cmdline, sizeof(cmdline), "%s%s%s",
line + 2, line[2] == '\0' ? "" : " ", prog);
+ cmdline[sizeof(cmdline) - 1] = 0;
#if 0
copypath(cmdline, cmdline, strlen(cmdline) + 1);
#endif
@@ -2554,7 +2566,8 @@
mystrlcpy(name, s, sizeof(name));
}
- (void) snprintf(ftry, sizeof(ftry)-1, "%s%c%s",
path,DIRSEP,name);
+ (void) snprintf(ftry, sizeof(ftry), "%s%c%s", path,DIRSEP,name);
+ ftry[sizeof(ftry) - 1] = 0;
if (mystat(ftry, &st) == 0) {
/* Found ! */
mystrlcpy(path, ftry, maxpath);
@@ -2590,23 +2603,26 @@
c->query = mystrdup(c->query);
}
urldecode(c->uri, c->uri);
- (void) snprintf(path, sizeof(path)-1,
"%s%s",STROPT(OPT_DOCROOT),c->uri);
+ (void) snprintf(path, sizeof(path), "%s%s",STROPT(OPT_DOCROOT),c->uri);
+ path[sizeof(path) - 1] = 0;
killdots(path + strlen(STROPT(OPT_DOCROOT)));
#if EMBEDDED
/* User may use the aliases - check URI for mount point */
if ((mp = ismountpoint(c->uri)) != NULL) {
- (void) snprintf(path, sizeof(path)-1, "%s%s", mp->path,
+ (void) snprintf(path, sizeof(path), "%s%s", mp->path,
c->uri + strlen(mp->mountpoint));
+ path[sizeof(path) - 1] = 0;
killdots(path + strlen(mp->path));
}
#endif /* EMBEDDED */
#ifndef NO_AUTH
if (checkauth(c, path) != 1) {
- (void) snprintf(buf, sizeof(buf)-1,
+ (void) snprintf(buf, sizeof(buf),
"WWW-Authenticate: Digest qop=\"auth\", realm=\"%s\", "
"nonce=\"%lu\"", STROPT(OPT_REALM), (unsigned long) now);
+ buf[sizeof(buf) - 1] = 0;
senderr(c, 401, "Unauthorized", buf, "Authorization required");
} else
@@ -2654,7 +2670,8 @@
} else if (mystat(path, &c->st) != 0) {
senderr(c, 404, "Not Found","", "Not Found");
} else if (S_ISDIR(c->st.st_mode) && path[strlen(path) - 1] != '/') {
- (void) snprintf(buf, sizeof(buf)-1, "Location: %s/", c->uri);
+ (void) snprintf(buf, sizeof(buf), "Location: %s/", c->uri);
+ buf[sizeof(buf) - 1] = 0;
senderr(c, 301, "Moved Permanently", buf, "Moved, %s", buf);
} else if (S_ISDIR(c->st.st_mode) &&
useindex(c, path, sizeof(path) - 1) == -1 &&
@@ -2700,8 +2717,9 @@
{
char fmt[32];
- (void) snprintf(fmt, sizeof(fmt)-1, "%%" LLFMT "s %%" LLFMT "s %%"
LLFMT "s",
+ (void) snprintf(fmt, sizeof(fmt), "%%" LLFMT "s %%" LLFMT "s %%" LLFMT
"s",
(long long)sizeof(c->method) - 1, (long long)sizeof(c->uri) - 1,
(long long)sizeof(c->proto) - 1);
+ fmt[sizeof(fmt) - 1] = 0;
/* Get the request line */
if (sscanf(s, fmt, c->method, c->uri, c->proto) != 3) {
@@ -3376,7 +3394,8 @@
char buf[IO_MAX];
va_start(ap, fmt);
- len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
+ len = vsnprintf(buf, sizeof(buf), fmt, ap);
+ buf[sizeof(buf) - 1] = 0;
if (len >= (int) sizeof(buf) - 1)
len = sizeof(buf) - 1;
else if (len == -1)
Index: pathfinder.mx
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/pathfinder.mx,v
retrieving revision 1.399.2.3
retrieving revision 1.399.2.4
diff -u -d -r1.399.2.3 -r1.399.2.4
--- pathfinder.mx 22 Feb 2008 10:39:28 -0000 1.399.2.3
+++ pathfinder.mx 22 Feb 2008 15:13:17 -0000 1.399.2.4
@@ -4646,10 +4646,18 @@
@= seqbase
BATseqbase(ctx->@1, @2);
- if (ctx->mode&XQ_DEBUG) m += snprintf(mil+m, XQUERY_BUFSIZE-m-1,
"@1.seqbase(" @3 ");\n");
+ if (ctx->mode&XQ_DEBUG) {
+ snprintf(mil+m, XQUERY_BUFSIZE-m, "@1.seqbase(" @3 ");\n");
+ mil[XQUERY_BUFSIZE - 1] = 0;
+ m += strlen(mil+m);
+ }
@= bunappend
if (BUNappend(ctx->@1, @2, FALSE) == NULL) return "xquery_method:
allocation error while inserting in @1";
- if (ctx->mode&XQ_DEBUG) m += snprintf(mil+m, XQUERY_BUFSIZE-m-1,
"@1.append(" @4 @5 ");\n", @3 @2);
+ if (ctx->mode&XQ_DEBUG) {
+ snprintf(mil+m, XQUERY_BUFSIZE-m, "@1.append(" @4 @5 ");\n", @3 @2);
+ mil[XQUERY_BUFSIZE - 1] = 0;
+ m += strlen(mil+m);
+ }
@c
/*
* call a function ns:method(). try to use the function cache (ie re-use a
cached MIL tree).
@@ -4725,13 +4733,15 @@
}
/* call UDF */
- ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil)-1,
+ ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil),
PFudfMIL(),
fun->proc, 0, 0, 0,0, 0, 0, 0, 0, fun->sig->name,
fun->sig->name, 0, 0, 0, 0);
+ mil[XQUERY_BUFSIZE - 1] = 0;
if (ret > 0) cur += ret;
/* destroy working set */
- ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil)-1,
PFstopMIL(fun->sig->update));
+ ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil),
PFstopMIL(fun->sig->update));
+ mil[XQUERY_BUFSIZE - 1] = 0;
if (ret > 0) cur += ret;
/* done! execute the script */
@@ -5080,7 +5090,8 @@
@= find_bat
{ Variable v = VARfind(&ctx->stk, "@1");
char buf[256];
- snprintf(buf, sizeof(buf)-1, "[EMAIL PROTECTED]", ctx->stk);
+ snprintf(buf, sizeof(buf), "[EMAIL PROTECTED]", ctx->stk);
+ buf[sizeof(buf) - 1] = 0;
ctx->@1 = NULL;
if (v && v->binding.vtype == TYPE_bat) {
ctx->@1 = BATdescriptor(v->binding.val.bval);
@@ -5876,6 +5887,7 @@
/* insert into document collection (p is name)*/
snprintf(ctx->buf, ctx->buflen, "shred_stream(Stream("
SZFMT "LL), \"%s\", \"%s\", lng(%d));\n", (size_t) in, dname, cname, perc);
+ ctx->buf[ctx->buflen - 1] = 0;
i = xquery_mil_exec(ctx, ctx->buf); /* use MIL to add
it to the repository */
GDKfree(dname);
if (i < 0) msg = "shred_stream failed";
Index: serialize.mx
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/serialize.mx,v
retrieving revision 1.106.2.1
retrieving revision 1.106.2.2
diff -u -d -r1.106.2.1 -r1.106.2.2
--- serialize.mx 22 Feb 2008 10:39:31 -0000 1.106.2.1
+++ serialize.mx 22 Feb 2008 15:13:20 -0000 1.106.2.2
@@ -304,7 +304,8 @@
(void)ctx;
va_start (msgs, msg);
- vsnprintf (errmsg, sizeof(errmsg)-1, msg, msgs);
+ vsnprintf (errmsg, sizeof(errmsg), msg, msgs);
+ errmsg[sizeof(errmsg) - 1] = 0;
va_end (msgs);
GDKerror("XML Generation: %s\n",errmsg);
Index: pf_support.mx
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/pf_support.mx,v
retrieving revision 1.277.6.3
retrieving revision 1.277.6.4
diff -u -d -r1.277.6.3 -r1.277.6.4
--- pf_support.mx 22 Feb 2008 10:39:30 -0000 1.277.6.3
+++ pf_support.mx 22 Feb 2008 15:13:18 -0000 1.277.6.4
@@ -7500,11 +7500,12 @@
BAT *v = 0, *b = BBPdescriptor(bid);
if (b) *ret = v = VIEWcreate(b,b);
BBPunfix(bid);
- if (v) {
+ if (v) {
/* give the view a name such that lng(bbname(v)) = lng(bbpname(b))
*/
- long_str buf;
- snprintf(buf, sizeof(buf)-1, "%s-%d", BBP_logical(bid),
ABS(b->batCacheid));
- BBPrename(v->batCacheid, buf);
+ long_str buf;
+ snprintf(buf, sizeof(buf), "%s-%d", BBP_logical(bid),
ABS(b->batCacheid));
+ buf[sizeof(buf) - 1] = 0;
+ BBPrename(v->batCacheid, buf);
return GDK_SUCCEED;
} else
if (b) {
Index: shredder.mx
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/shredder.mx,v
retrieving revision 1.134
retrieving revision 1.134.6.1
diff -u -d -r1.134 -r1.134.6.1
--- shredder.mx 11 Jan 2008 10:47:20 -0000 1.134
+++ shredder.mx 22 Feb 2008 15:13:20 -0000 1.134.6.1
@@ -1198,11 +1198,15 @@
char buf[PFSHRED_BUFLEN];
if (*print_error_newline) {
- len += snprintf(buf+len, PFSHRED_BUFLEN-len-1, GDKERROR);
+ snprintf(buf+len, PFSHRED_BUFLEN-len, GDKERROR);
+ buf[PFSHRED_BUFLEN - 1] = 0;
+ len += strlen(buf + len);
}
va_start(args, msg);
- len += vsnprintf(buf+len, PFSHRED_BUFLEN-len-1, msg, args);
+ vsnprintf(buf+len, PFSHRED_BUFLEN-len, msg, args);
+ buf[PFSHRED_BUFLEN - 1] = 0;
+ /* len += strlen(buf + len); */
va_end(args);
*print_error_newline =(strchr(buf,(int)'\n') != NULL);
@@ -1227,11 +1231,15 @@
char buf[PFSHRED_BUFLEN];
if (*print_error_newline) {
- len += snprintf(buf+len, PFSHRED_BUFLEN-len-1, GDKERROR);
+ snprintf(buf+len, PFSHRED_BUFLEN-len, GDKERROR);
+ buf[PFSHRED_BUFLEN - 1] = 0;
+ len += strlen(buf + len);
}
va_start(args, msg);
- len += vsnprintf(buf+len, PFSHRED_BUFLEN-len-1, msg, args);
+ vsnprintf(buf+len, PFSHRED_BUFLEN-len, msg, args);
+ buf[PFSHRED_BUFLEN - 1] = 0;
+ /* len += strlen(buf + len); */
va_end(args);
*print_error_newline =(strchr(buf,(int)'\n') != NULL);
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Monetdb-pf-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-pf-checkins