Changeset 6646


Ignore:
Timestamp:
Aug 26, 2008, 3:22:00 PM (15 years ago)
Author:
charles
Message:

#1217: libt/shttpd: "shttpd crash in decide_what_to_do()"

Location:
trunk/third-party/shttpd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/third-party/shttpd/config.h

    r6645 r6646  
    1616#define HTPASSWD        ".htpasswd"     /* Passwords file name          */
    1717#define URI_MAX         16384           /* Default max request size     */
     18#define IO_BUFSIZE      65536           /* IO buffer size               */
    1819#define LISTENING_PORTS "80"            /* Default listening ports      */
    1920#define INDEX_FILES     "index.html,index.htm,index.php,index.cgi"
  • trunk/third-party/shttpd/log.c

    r6644 r6646  
    1717_shttpd_elog(int flags, struct conn *c, const char *fmt, ...)
    1818{
    19         char    date[64], buf[URI_MAX];
    20         int     len;
    2119        FILE    *fp = c == NULL ? NULL : c->ctx->error_log;
    2220        va_list ap;
     
    3028        }
    3129
    32         strftime(date, sizeof(date), "%a %b %d %H:%M:%S %Y",
    33             localtime(&_shttpd_current_time));
     30        if (fp != NULL && (flags & (E_FATAL | E_LOG))) {
     31                char    date[64];
     32                char    *buf = malloc( URI_MAX );
     33                int     len;
    3434
    35         len = _shttpd_snprintf(buf, sizeof(buf),
    36             "[%s] [error] [client %s] \"%s\" ",
    37             date, c ? inet_ntoa(c->sa.u.sin.sin_addr) : "-",
    38             c && c->request ? c->request : "-");
     35                strftime(date, sizeof(date), "%a %b %d %H:%M:%S %Y",
     36                    localtime(&_shttpd_current_time));
    3937
    40         va_start(ap, fmt);
    41         (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
    42         va_end(ap);
     38                len = _shttpd_snprintf(buf, URI_MAX,
     39                    "[%s] [error] [client %s] \"%s\" ",
     40                    date, c ? inet_ntoa(c->sa.u.sin.sin_addr) : "-",
     41                    c && c->request ? c->request : "-");
    4342
    44         buf[sizeof(buf) - 1] = '\0';
     43                va_start(ap, fmt);
     44                (void) vsnprintf(buf + len, URI_MAX - len, fmt, ap);
     45                va_end(ap);
    4546
    46         if (fp != NULL && (flags & (E_FATAL | E_LOG))) {
     47                buf[URI_MAX - 1] = '\0';
     48
    4749                (void) fprintf(fp, "%s\n", buf);
    4850                (void) fflush(fp);
     51
     52                free(buf);
    4953        }
    5054
     
    5862        static const struct vec dash = {"-", 1};
    5963
    60         const struct vec        *user = &c->ch.user.v_vec;
    61         const struct vec        *referer = &c->ch.referer.v_vec;
    62         const struct vec        *user_agent = &c->ch.useragent.v_vec;
    63         char                    date[64], buf[URI_MAX], *q1 = "\"", *q2 = "\"";
     64        const struct vec        *user;
     65        const struct vec        *referer;
     66        const struct vec        *user_agent;
     67        char                    date[64], *buf, *q1, *q2;
     68
     69        if (fp == NULL)
     70                return;
     71
     72        user = &c->ch.user.v_vec;
     73        referer = &c->ch.referer.v_vec;
     74        user_agent = &c->ch.useragent.v_vec;
     75        q1 = q2 = "\"";
    6476
    6577        if (user->len == 0)
     
    7991                        localtime(&c->birth_time));
    8092
    81         (void) _shttpd_snprintf(buf, sizeof(buf),
     93        buf = malloc(URI_MAX);
     94
     95        (void) _shttpd_snprintf(buf, URI_MAX,
    8296            "%s - %.*s [%s %+05d] \"%s\" %d %lu %s%.*s%s %s%.*s%s",
    8397            inet_ntoa(c->sa.u.sin.sin_addr), user->len, user->ptr,
     
    87101            q2, user_agent->len, user_agent->ptr, q2);
    88102
    89         if (fp != NULL) {
    90                 (void) fprintf(fp, "%s\n", buf);
    91                 (void) fflush(fp);
    92         }
     103        (void) fprintf(fp, "%s\n", buf);
     104        (void) fflush(fp);
     105
     106        free(buf);
    93107}
  • trunk/third-party/shttpd/shttpd.c

    r6645 r6646  
    534534decide_what_to_do(struct conn *c)
    535535{
    536         char            path[URI_MAX], buf[1024], *root;
     536        char            *path;
     537        const char      *root;
    537538        struct vec      alias_uri, alias_path;
    538539        struct stat     st;
     
    549550       
    550551        root = c->ctx->options[OPT_ROOT];
    551         if (strlen(c->uri) + strlen(root) >= sizeof(path)) {
     552        if (strlen(c->uri) + strlen(root) >= URI_MAX) {
    552553                _shttpd_send_server_error(c, 400, "URI is too long");
    553554                return;
    554555        }
    555556
    556         (void) _shttpd_snprintf(path, sizeof(path), "%s%s", root, c->uri);
     557        path = malloc( URI_MAX );
     558        (void) _shttpd_snprintf(path, URI_MAX, "%s%s", root, c->uri);
    557559
    558560        /* User may use the aliases - check URI for mount point */
    559561        if (is_alias(c->ctx, c->uri, &alias_uri, &alias_path) != NULL) {
    560                 (void) _shttpd_snprintf(path, sizeof(path), "%.*s%s",
     562                (void) _shttpd_snprintf(path, URI_MAX, "%.*s%s",
    561563                    alias_path.len, alias_path.ptr, c->uri + alias_uri.len);
    562564                DBG(("using alias %.*s -> %.*s", alias_uri.len, alias_uri.ptr,
     
    613615                _shttpd_send_server_error(c, 404, "Not Found");
    614616        } else if (S_ISDIR(st.st_mode) && path[strlen(path) - 1] != '/') {
     617                char buf[1024];
    615618                (void) _shttpd_snprintf(buf, sizeof(buf),
    616619                        "Moved Permanently\r\nLocation: %s/", c->uri);
    617620                _shttpd_send_server_error(c, 301, buf);
    618621        } else if (S_ISDIR(st.st_mode) &&
    619             find_index_file(c, path, sizeof(path) - 1, &st) == -1 &&
     622            find_index_file(c, path, URI_MAX - 1, &st) == -1 &&
    620623            !IS_TRUE(c->ctx, OPT_DIR_LIST)) {
    621624                _shttpd_send_server_error(c, 403, "Directory Listing Denied");
     
    656659                _shttpd_send_server_error(c, 500, "Internal Error");
    657660        }
     661
     662        free(path);
    658663}
    659664
     
    782787                SSL_free(ssl);
    783788#endif /* NO_SSL */
    784         } else if ((c = calloc(1, sizeof(*c) + 2 * URI_MAX)) == NULL) {
     789        } else if ((c = calloc(1, sizeof(*c) + 2 * IO_BUFSIZE)) == NULL) {
    785790#if !defined(NO_SSL)
    786791                if (ssl)
     
    809814                /* Set IO buffers */
    810815                c->loc.io.buf   = (char *) (c + 1);
    811                 c->rem.io.buf   = c->loc.io.buf + URI_MAX;
    812                 c->loc.io.size  = c->rem.io.size = URI_MAX;
     816                c->rem.io.buf   = c->loc.io.buf + IO_BUFSIZE;
     817                c->loc.io.size  = c->rem.io.size = IO_BUFSIZE;
    813818
    814819#if !defined(NO_SSL)
Note: See TracChangeset for help on using the changeset viewer.