diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-07-10 13:47:59 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-07-10 13:47:59 +0800 |
commit | a461151d1c95600f859ac8b898e3c4771a6a4e03 (patch) | |
tree | 74812820b8ae31713e6af548d7d962c9b7b87310 /daemon/logind | |
parent | 1d1c09830f1a27b584ea1ac411285d41d12f21d9 (diff) | |
download | pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.tar pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.tar.gz pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.tar.bz2 pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.tar.lz pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.tar.xz pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.tar.zst pttbbs-a461151d1c95600f859ac8b898e3c4771a6a4e03.zip |
* code refine: prevent double free / memory leaks
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4735 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'daemon/logind')
-rw-r--r-- | daemon/logind/logind.c | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/daemon/logind/logind.c b/daemon/logind/logind.c index c720624a..8587c670 100644 --- a/daemon/logind/logind.c +++ b/daemon/logind/logind.c @@ -771,46 +771,56 @@ static char *welcome_screen, *goodbye_screen, *ban_screen; static void load_text_screen_file(const char *filename, char **pptr) { - FILE *fp; - off_t sz, wsz, psz; + FILE *fp = NULL; + off_t sz, wsz=0, psz; char *p, *s = NULL; int max_lines = MAX_TEXT_SCREEN_LINES; sz = dashs(filename); - if (sz < 1) + if (sz > 1) { - free(*pptr); - *pptr = NULL; - return; + wsz = sz*2 +1; // *2 for cr+lf, extra one byte for safe strchr(). + fp = fopen(filename, "rt"); } - wsz = sz*2 +1; // *2 for cr+lf, extra one byte for safe strchr(). + // check valid file assert(pptr); s = *pptr; - s = realloc(s, wsz); + if (!fp) + { + if (s) free(s); + *pptr = NULL; + return; + } + + // check memory buffer + s = realloc(*pptr, wsz); *pptr = s; if (!s) + { + fclose(fp); return; + } + // prepare buffer memset(s, 0, wsz); p = s; psz = wsz; - fp = fopen(filename, "rt"); - if (!fp) - { - free(s); - return; - } while ( max_lines-- > 0 && fgets(p, psz, fp)) { - psz -= strlen(p); - p += strlen(p); - *p ++ = '\r'; + size_t l = strlen(p); + psz -= l; + p += l; + if (l > 0 && *(p-1) == '\n') + { + // convert \n to \r\n + *(p-1)= '\r'; + *p ++ = '\n'; + } } fclose(fp); - *pptr = s; } static void regular_check(); @@ -1586,7 +1596,8 @@ client_cb(int fd, short event, void *arg) { login_conn_ctx *conn = (login_conn_ctx*) arg; int len, r; - unsigned char buf[64], ch, *s = buf; + unsigned char buf[64], ch; + char *s = (char*)buf; // for time-out, simply close connection. if (event & EV_TIMEOUT) @@ -1639,7 +1650,7 @@ client_cb(int fd, short event, void *arg) while (len > 0) { - int c = _handle_term_keys((char**)&s, &len); + int c = _handle_term_keys(&s, &len); // for zero, ignore. if (!c) |