Update of /cvsroot/monetdb/pathfinder/runtime
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv21153/runtime

Modified Files:
      Tag: XQuery_0-22
        pathfinder.mx pf_support.mx serialize.mx shttpd.c 
Log Message:
This is a fix for bug [ 1892554 ] PF: loading pre-compiled MIL modules
fails on Windows

There seems to a difference between Windows and Linux in the meaning
of the second argument of snprintf and vsnprintf: in Linux this
argument gives the size of the buffer to which the function writes,
whereas in Windows it gives the number of non-NULL bytes that it can
write (and then it adds a NULL).  This means that on Windows you have
to specify a size which is *smaller* than the actual size of the
buffer.

To make the same code work on both flavors, in some cases we now
overallocate the needed space by one byte so that we can use
(sizeof(buf)-1) as size.


Index: shttpd.c
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/shttpd.c,v
retrieving revision 1.33
retrieving revision 1.33.8.1
diff -u -d -r1.33 -r1.33.8.1
--- shttpd.c    23 Aug 2007 12:47:24 -0000      1.33
+++ shttpd.c    22 Feb 2008 10:39:33 -0000      1.33.8.1
@@ -614,17 +614,17 @@
                return (0);
 
        va_start(ap, fmt);
-       n = vsnprintf(buf, buflen, fmt, ap);
+       n = vsnprintf(buf, buflen-1, 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) {
+       } else if (n >= (int) buflen - 1) {
                elog(ERR_DEBUG, "%s: truncating from %d to %u [%s]",
-                   "Snprintf", n, buflen - 1, buf);
-               n = buflen - 1;
+                   "Snprintf", n, buflen - 2, buf);
+               n = buflen - 2;
                buf[n] = '\0';
        }
 
@@ -757,7 +757,7 @@
        n = strftime(msg, sizeof(msg),"%d/%b/%Y %H:%M:%S ", localtime(&now));
 
        va_start(ap, fmt);
-       n += vsnprintf(msg + n, sizeof(msg) - n, fmt, ap);
+       n += vsnprintf(msg + n, sizeof(msg) - n - 1, fmt, ap);
        va_end(ap);
 
        if (n > sizeof(msg) - 2)
@@ -1258,9 +1258,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, fmt, ap);
-       if (n > (int) sizeof(msg))
-               n = sizeof(msg);
+       n += vsnprintf(msg + n, sizeof(msg) - n - 1, fmt, ap);
+       if (n >= (int) sizeof(msg) - 1)
+               n = sizeof(msg) - 1;
        va_end(ap);
        mystrlcpy(c->local.buf, msg, sizeof(c->local.buf));
        c->local.nread = n;
@@ -1429,13 +1429,13 @@
                   strcmp(dp->d_name, HTPASSWD) == 0)
                        continue;
 
-               (void) snprintf(file, sizeof(file),
+               (void) snprintf(file, sizeof(file)-1,
                    "%s%s%s",c->path, slash, dp->d_name);
                (void) mystat(file, &st);
                if (S_ISDIR(st.st_mode))
-                       snprintf(size,sizeof(size),"        %s","&lt;DIR&gt;");
+                       snprintf(size,sizeof(size)-1,"        
%s","&lt;DIR&gt;");
                else
-                       (void) snprintf(size, sizeof(size),"%10.2f kB",
+                       (void) snprintf(size, sizeof(size)-1,"%10.2f kB",
                            (float) st.st_size / 1024);
                (void) strftime(mod, sizeof(mod),
                    "%d-%b-%Y %H:%M", localtime(&st.st_mtime));
@@ -1544,7 +1544,7 @@
        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), "%lx.%lx",
+       (void) snprintf(etag, sizeof(etag)-1, "%lx.%lx",
            (unsigned long) c->st.st_mtime, (unsigned long) c->st.st_size);
 
        /* Local read buffer should be empty */
@@ -1995,7 +1995,7 @@
                                u[LSIZ], d[LSIZ], ha1[LSIZ];
        FILE            *fp = NULL, *fp2 = NULL;
 
-       (void) snprintf(tmp, sizeof(tmp), "%s.tmp", fname);
+       (void) snprintf(tmp, sizeof(tmp)-1, "%s.tmp", fname);
 
        /* Create the file if does not exist */
        if ((fp = fopen(fname, "a+")))
@@ -2014,7 +2014,7 @@
                    strcmp(domain, d) == 0) {
                        found++;
                        md5(ha1, user, ":", domain, ":", pass, NULL);
-                       (void) snprintf(line, sizeof(line),
+                       (void) snprintf(line, sizeof(line)-1,
                            "%s:%s:%s\n", user, domain, ha1);
                }
                (void) fprintf(fp2, "%s", line);
@@ -2023,7 +2023,7 @@
        /* If new user, just add it */
        if (found == 0) {
                md5(ha1, user, ":", domain, ":", pass, NULL);
-               (void) snprintf(line, sizeof(line),
+               (void) snprintf(line, sizeof(line)-1,
                    "%s:%s:%s\n", user, domain, ha1);
                (void) fprintf(fp2, "%s", line);
        }
@@ -2144,14 +2144,14 @@
 
        if (STROPT(OPT_HTPASSWD)) {
                /* Use global passwords file */
-               (void) snprintf(name, sizeof(name), "%s", STROPT(OPT_HTPASSWD));
+               (void) snprintf(name, sizeof(name)-1, "%s", 
STROPT(OPT_HTPASSWD));
        } 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), "%.*s/%s",
+               (void) snprintf(name, sizeof(name)-1, "%.*s/%s",
                    (int) (e - p), p, HTPASSWD);
 
                /* Fix directory separators */
@@ -2269,7 +2269,7 @@
        }
 
        /* Prepare command line */
-       (void) snprintf(cmdline, sizeof(cmdline), "%s%s%s",
+       (void) snprintf(cmdline, sizeof(cmdline)-1, "%s%s%s",
            line + 2, line[2] == '\0' ? "" : " ", prog);
 #if 0
        copypath(cmdline, cmdline, strlen(cmdline) + 1);
@@ -2554,7 +2554,7 @@
                        mystrlcpy(name, s, sizeof(name));
                }
 
-               (void) snprintf(ftry, sizeof(ftry), "%s%c%s", path,DIRSEP,name);
+               (void) snprintf(ftry, sizeof(ftry)-1, "%s%c%s", 
path,DIRSEP,name);
                if (mystat(ftry, &st) == 0) {
                        /* Found ! */
                        mystrlcpy(path, ftry, maxpath);
@@ -2590,13 +2590,13 @@
                c->query = mystrdup(c->query);
        }
        urldecode(c->uri, c->uri);
-       (void) snprintf(path, sizeof(path), "%s%s",STROPT(OPT_DOCROOT),c->uri);
+       (void) snprintf(path, sizeof(path)-1, 
"%s%s",STROPT(OPT_DOCROOT),c->uri);
        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), "%s%s", mp->path,
+               (void) snprintf(path, sizeof(path)-1, "%s%s", mp->path,
                    c->uri + strlen(mp->mountpoint));
                killdots(path + strlen(mp->path));
        }
@@ -2604,7 +2604,7 @@
 
 #ifndef NO_AUTH
        if (checkauth(c, path) != 1) {
-               (void) snprintf(buf, sizeof(buf),
+               (void) snprintf(buf, sizeof(buf)-1,
                    "WWW-Authenticate: Digest qop=\"auth\", realm=\"%s\", "
                    "nonce=\"%lu\"", STROPT(OPT_REALM), (unsigned long) now);
 
@@ -2654,7 +2654,7 @@
        } 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), "Location: %s/", c->uri);
+               (void) snprintf(buf, sizeof(buf)-1, "Location: %s/", c->uri);
                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,7 +2700,7 @@
 {
        char    fmt[32];
 
-       (void) snprintf(fmt, sizeof(fmt), "%%" LLFMT "s %%" LLFMT "s %%" LLFMT 
"s",
+       (void) snprintf(fmt, sizeof(fmt)-1, "%%" LLFMT "s %%" LLFMT "s %%" 
LLFMT "s",
            (long long)sizeof(c->method) - 1, (long long)sizeof(c->uri) - 1, 
(long long)sizeof(c->proto) - 1);
 
        /* Get the request line */
@@ -3376,9 +3376,9 @@
        char    buf[IO_MAX];
 
        va_start(ap, fmt);
-       len = vsnprintf(buf, sizeof(buf), fmt, ap);
-       if (len > (int) sizeof(buf))
-               len = sizeof(buf);
+       len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
+       if (len >= (int) sizeof(buf) - 1)
+               len = sizeof(buf) - 1;
        else if (len == -1)
                len = 0;
        va_end(ap);

Index: pathfinder.mx
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/pathfinder.mx,v
retrieving revision 1.399.2.2
retrieving revision 1.399.2.3
diff -u -d -r1.399.2.2 -r1.399.2.3
--- pathfinder.mx       18 Feb 2008 16:17:56 -0000      1.399.2.2
+++ pathfinder.mx       22 Feb 2008 10:39:28 -0000      1.399.2.3
@@ -4646,10 +4646,10 @@
 
 @= seqbase
     BATseqbase(ctx->@1, @2);
-    if (ctx->mode&XQ_DEBUG) m += snprintf(mil+m, XQUERY_BUFSIZE-l, 
"@1.seqbase(" @3 ");\n");
+    if (ctx->mode&XQ_DEBUG) m += snprintf(mil+m, XQUERY_BUFSIZE-m-1, 
"@1.seqbase(" @3 ");\n");
 @= 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-l, 
"@1.append(" @4 @5 ");\n", @3 @2);
+    if (ctx->mode&XQ_DEBUG) m += snprintf(mil+m, XQUERY_BUFSIZE-m-1, 
"@1.append(" @4 @5 ");\n", @3 @2);
 @c
 /*
  * call a function ns:method(). try to use the function cache (ie re-use a 
cached MIL tree).
@@ -4725,13 +4725,13 @@
             }
 
             /* call UDF */
-            ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil), 
+            ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil)-1, 
                            PFudfMIL(), 
                            fun->proc, 0, 0, 0,0, 0, 0, 0, 0, fun->sig->name, 
fun->sig->name, 0, 0, 0, 0);
             if (ret > 0) cur += ret;
 
             /* destroy working set */
-            ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil), 
PFstopMIL(fun->sig->update));
+            ret = snprintf(cur, XQUERY_BUFSIZE-(cur-mil)-1, 
PFstopMIL(fun->sig->update));
             if (ret > 0) cur += ret;
 
             /* done! execute the script */
@@ -5080,7 +5080,7 @@
 @= find_bat
 {   Variable v = VARfind(&ctx->stk, "@1");
     char buf[256];
-    snprintf(buf, 256, "[EMAIL PROTECTED]", ctx->stk);
+    snprintf(buf, sizeof(buf)-1, "[EMAIL PROTECTED]", ctx->stk);
     ctx->@1 = NULL;
     if (v && v->binding.vtype == TYPE_bat) {
         ctx->@1 = BATdescriptor(v->binding.val.bval);

Index: serialize.mx
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/runtime/serialize.mx,v
retrieving revision 1.106
retrieving revision 1.106.2.1
diff -u -d -r1.106 -r1.106.2.1
--- serialize.mx        30 Jan 2008 15:54:32 -0000      1.106
+++ serialize.mx        22 Feb 2008 10:39:31 -0000      1.106.2.1
@@ -304,7 +304,7 @@
 
     (void)ctx;
     va_start (msgs, msg);
-    vsnprintf (errmsg, 1024, msg, msgs);
+    vsnprintf (errmsg, sizeof(errmsg)-1, msg, msgs);
     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.2
retrieving revision 1.277.6.3
diff -u -d -r1.277.6.2 -r1.277.6.3
--- pf_support.mx       21 Feb 2008 22:18:08 -0000      1.277.6.2
+++ pf_support.mx       22 Feb 2008 10:39:30 -0000      1.277.6.3
@@ -7503,7 +7503,7 @@
        if (v) {
             /* give the view a name such that lng(bbname(v)) = lng(bbpname(b)) 
*/
            long_str buf;
-            snprintf(buf, sizeof(buf), "%s-%d", BBP_logical(bid), 
ABS(b->batCacheid));
+            snprintf(buf, sizeof(buf)-1, "%s-%d", BBP_logical(bid), 
ABS(b->batCacheid));
            BBPrename(v->batCacheid, buf);
            return GDK_SUCCEED;
         } else


-------------------------------------------------------------------------
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

Reply via email to