Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/httpd better approach to NUL terminate strings



details:   https://anonhg.NetBSD.org/src/rev/bf9837b4f517
branches:  trunk
changeset: 790545:bf9837b4f517
user:      mbalmer <mbalmer%NetBSD.org@localhost>
date:      Thu Oct 17 07:54:19 2013 +0000

description:
better approach to NUL terminate strings

diffstat:

 libexec/httpd/lua-bozo.c |  28 +++++++++++++++++-----------
 1 files changed, 17 insertions(+), 11 deletions(-)

diffs (63 lines):

diff -r cec1adfd09f8 -r bf9837b4f517 libexec/httpd/lua-bozo.c
--- a/libexec/httpd/lua-bozo.c  Thu Oct 17 07:49:06 2013 +0000
+++ b/libexec/httpd/lua-bozo.c  Thu Oct 17 07:54:19 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lua-bozo.c,v 1.3 2013/10/17 07:49:06 mbalmer Exp $     */
+/*     $NetBSD: lua-bozo.c,v 1.4 2013/10/17 07:54:19 mbalmer Exp $     */
 
 /*
  * Copyright (c) 2013 Marc Balmer <marc%msys.ch@localhost>
@@ -81,7 +81,7 @@
 lua_read(lua_State *L)
 {
        bozohttpd_t *httpd;
-       int len;
+       int n, len;
        char *data;
 
        lua_pushstring(L, "bozohttpd");
@@ -91,9 +91,12 @@
 
        len = luaL_checkinteger(L, -1);
        data = bozomalloc(httpd, len + 1);
-       memset(data, 0, len + 1);
-       bozo_read(httpd, STDIN_FILENO, data, len);
-       lua_pushstring(L, data);
+       n = bozo_read(httpd, STDIN_FILENO, data, len);
+       if (n >= 0) {
+               data[n] = '\0';
+               lua_pushstring(L, data);
+       } else
+               lua_pushnil(L);
        free(data);
        return 1;
 }
@@ -294,7 +297,7 @@
        bozohttpd_t *httpd = request->hr_httpd;
        lua_state_map_t *map;
        lua_handler_t *hndlr;
-       int ret, length;
+       int n, ret, length;
        char date[40];
        bozoheaders_t *headp;
        char *s, *query, *uri, *file, *command, *info, *content;
@@ -410,11 +413,14 @@
                                                length = atol(clen);
                                                content = bozomalloc(httpd,
                                                    length + 1);
-                                               memset(content, 0, length + 1);
-                                               bozo_read(httpd, STDIN_FILENO,
-                                                   content, length);
-                                               lua_decode_query(map->L,
-                                                   content);
+                                               n = bozo_read(httpd,
+                                                   STDIN_FILENO, content,
+                                                   length);
+                                               if (n >= 0) {
+                                                       content[n] = '\0';
+                                                       lua_decode_query(map->L,
+                                                           content);
+                                               }
                                                free(content);
                                        }
                                }



Home | Main Index | Thread Index | Old Index