From 45527243a064c1aafaceb8c4c6fa7dcbd383b449 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Mon, 10 Jun 2013 14:39:19 +0800 Subject: [PATCH] Avoid strlen & strncpy in reading lines from file. --- file.c | 4 ++-- fileio.c | 59 ++++++++++++++++++-------------------------------------- fileio.h | 3 ++- 3 files changed, 23 insertions(+), 43 deletions(-) diff --git a/file.c b/file.c index 8c8d02c..1d63cbe 100644 --- a/file.c +++ b/file.c @@ -275,7 +275,7 @@ int readin(char *fname, int lockfl) mlwrite("(Reading file)"); nline = 0; while ((s = ffgetline()) == FIOSUC) { - nbytes = strlen(fline); + nbytes = fpayload ; if ((lp1 = lalloc(nbytes)) == NULL) { s = FIOMEM; /* Keep message on the */ break; /* display. */ @@ -610,7 +610,7 @@ int ifile(char *fname) nline = 0; while ((s = ffgetline()) == FIOSUC) { - nbytes = strlen(fline); + nbytes = fpayload ; if ((lp1 = lalloc(nbytes)) == NULL) { s = FIOMEM; /* Keep message on the */ break; /* display. */ diff --git a/fileio.c b/fileio.c index 7d9f835..1401076 100644 --- a/fileio.c +++ b/fileio.c @@ -25,8 +25,9 @@ boolean is_crypted ; /* currently encrypting? */ #endif char *fline = NULL ; /* dynamic return line */ -int flen = 0 ; /* current length of fline */ +int flen = 0 ; /* current allocated length of fline */ int ftype ; +int fpayload ; /* actual length of fline content */ static FILE *ffp ; /* File pointer, all functions. */ @@ -139,7 +140,6 @@ fio_code ffgetline(void) { int c; /* current character read */ int i; /* current index into fline */ - char *tmpline; /* temp storage for expanding line */ /* if we are at the end...return it */ if (eofflag) @@ -157,48 +157,27 @@ fio_code ffgetline(void) return FIOMEM; /* read the line in */ -#if 0 /* PKCODE */ - if (!nullflag) { - if (fgets(fline, NSTRING, ffp) == (char *) NULL) { /* EOF ? */ - i = 0; - c = EOF; - } else { - i = strlen(fline); - c = 0; - if (i > 0) { - c = fline[i - 1]; - i--; - } - } - } else { - i = 0; - c = fgetc(ffp); - } - while (c != EOF && c != '\n') { -#else i = 0; while ((c = fgetc(ffp)) != EOF && c != '\r' && c != '\n') { -#endif -#if 0 /* PKCODE */ - if (c) { -#endif - fline[i++] = c; - /* if it's longer, get more room */ - if (i >= flen) { - if ((tmpline = - malloc(flen + NSTRING)) == NULL) - return FIOMEM; - strncpy(tmpline, fline, flen); - flen += NSTRING; - free(fline); - fline = tmpline; - } -#if 0 /* PKCODE */ + fline[i++] = c; + /* if it's longer, get more room */ + if (i >= flen) { + char *tmpline; /* temp storage for expanding line */ + + fpayload = i ; + tmpline = malloc(flen + NSTRING) ; + if( tmpline == NULL) + return FIOMEM ; + + memcpy( tmpline, fline, flen) ; + flen += NSTRING; + free(fline); + fline = tmpline; } - c = fgetc(ffp); -#endif } + fpayload = i ; + /* test for any errors that may have occured */ if (c == EOF) { if (ferror(ffp)) { @@ -224,7 +203,7 @@ fio_code ffgetline(void) fline[i] = 0; #if CRYPT if( is_crypted) - myencrypt(fline, strlen(fline)); + myencrypt( fline, fpayload); #endif return FIOSUC; } diff --git a/fileio.h b/fileio.h index fd2e482..704ab96 100644 --- a/fileio.h +++ b/fileio.h @@ -23,8 +23,9 @@ extern boolean is_crypted ; /* currently encrypting? */ #endif extern char *fline ; /* dynamic return line */ -extern int flen ; /* current length of fline */ +extern int flen ; /* current allocated length of fline */ extern int ftype ; +extern int fpayload ; /* actual length of fline content */ boolean fexist( const char *fname) ; fio_code ffclose( void) ;