Replace strncpy by mystrscpy.

This commit is contained in:
Renaud 2019-06-19 19:49:20 +08:00
parent 033012f645
commit f0836bb66a
12 changed files with 81 additions and 66 deletions

View File

@ -1,8 +1,8 @@
# Makefile for emacs, updated Fri, Feb 13, 2015 12:02:52 PM
# Makefile for emacs, updated Tue, Jul 31, 2018 20:37:16
SRC=basic.c bind.c bindable.c buffer.c display.c ebind.c eval.c exec.c execute.c file.c fileio.c flook.c input.c isearch.c line.c lock.c main.c mingw32.c mlout.c names.c pklock.c posix.c random.c region.c search.c spawn.c tcap.c termio.c utf8.c window.c word.c wrapper.c wscreen.c
OBJ=basic.o bind.o bindable.o buffer.o display.o ebind.o eval.o exec.o execute.o file.o fileio.o flook.o input.o isearch.o line.o lock.o main.o mingw32.o mlout.o names.o pklock.o posix.o random.o region.o search.o spawn.o tcap.o termio.o utf8.o window.o word.o wrapper.o wscreen.o
HDR=basic.h bind.h bindable.h buffer.h defines.h display.h ebind.h estruct.h eval.h exec.h execute.h file.h fileio.h flook.h input.h isearch.h line.h lock.h mlout.h names.h pklock.h random.h region.h retcode.h search.h spawn.h terminal.h termio.h utf8.h version.h window.h word.h wrapper.h wscreen.h
SRC=basic.c bind.c bindable.c buffer.c display.c ebind.c eval.c exec.c execute.c file.c fileio.c flook.c input.c isearch.c line.c lock.c main.c mingw32.c mlout.c names.c pklock.c posix.c random.c region.c search.c spawn.c tcap.c termio.c utf8.c util.c window.c word.c wrapper.c wscreen.c
OBJ=basic.o bind.o bindable.o buffer.o display.o ebind.o eval.o exec.o execute.o file.o fileio.o flook.o input.o isearch.o line.o lock.o main.o mingw32.o mlout.o names.o pklock.o posix.o random.o region.o search.o spawn.o tcap.o termio.o utf8.o util.o window.o word.o wrapper.o wscreen.o
HDR=basic.h bind.h bindable.h buffer.h defines.h display.h ebind.h estruct.h eval.h exec.h execute.h file.h fileio.h flook.h input.h isearch.h line.h lock.h mlout.h names.h pklock.h random.h region.h retcode.h search.h spawn.h terminal.h termio.h utf8.h util.h version.h window.h word.h wrapper.h wscreen.h
# DO NOT ADD OR MODIFY ANY LINES ABOVE THIS -- make source creates them
@ -47,7 +47,8 @@ ifeq ($(uname_S),CYGWIN)
LIBS=-lcurses
endif
ifeq ($(uname_S),MINGW32)
DEFINES=-DAUTOCONF -DSYSV -DMINGW32 -DPROGRAM=$(PROGRAM)
# DEFINES=-DAUTOCONF -DSYSV -DMINGW32 -DPROGRAM=$(PROGRAM)
DEFINES=-DAUTOCONF -DPOSIX -DSYSV -DPROGRAM=$(PROGRAM) -IC:/MinGW/include/ncursesw
LIBS=
endif
#DEFINES=-DAUTOCONF
@ -132,13 +133,13 @@ depend: ${SRC}
basic.o: basic.c basic.h retcode.h input.h bind.h mlout.h random.h \
terminal.h defines.h utf8.h window.h buffer.h line.h
bind.o: bind.c bind.h estruct.h bindable.h buffer.h line.h retcode.h \
utf8.h display.h ebind.h exec.h file.h flook.h input.h names.h window.h \
defines.h
utf8.h display.h ebind.h exec.h file.h flook.h input.h names.h util.h \
window.h defines.h
bindable.o: bindable.c bindable.h defines.h buffer.h line.h retcode.h \
utf8.h display.h estruct.h file.h input.h bind.h lock.h mlout.h \
terminal.h
buffer.o: buffer.c buffer.h line.h retcode.h utf8.h defines.h estruct.h \
file.h input.h bind.h mlout.h window.h
file.h input.h bind.h mlout.h util.h window.h
display.o: display.c display.h estruct.h utf8.h buffer.h line.h retcode.h \
input.h bind.h termio.h terminal.h defines.h version.h wrapper.h \
window.h
@ -147,28 +148,29 @@ ebind.o: ebind.c ebind.h basic.h retcode.h bind.h estruct.h bindable.h \
search.h spawn.h window.h defines.h word.h
eval.o: eval.c eval.h basic.h retcode.h bind.h buffer.h line.h utf8.h \
display.h estruct.h exec.h execute.h flook.h input.h random.h search.h \
terminal.h defines.h termio.h version.h window.h
terminal.h defines.h termio.h util.h version.h window.h
exec.o: exec.c exec.h retcode.h buffer.h line.h utf8.h bind.h display.h \
estruct.h eval.h file.h flook.h input.h random.h window.h defines.h
estruct.h eval.h file.h flook.h input.h random.h util.h window.h \
defines.h
execute.o: execute.c execute.h estruct.h bind.h random.h retcode.h \
display.h utf8.h file.h buffer.h line.h input.h mlout.h search.h \
terminal.h defines.h window.h
file.o: file.c file.h buffer.h line.h retcode.h utf8.h defines.h \
display.h estruct.h execute.h fileio.h input.h bind.h lock.h mlout.h \
window.h
util.h window.h
fileio.o: fileio.c fileio.h defines.h retcode.h utf8.h
flook.o: flook.c flook.h retcode.h defines.h fileio.h
input.o: input.c input.h bind.h estruct.h bindable.h display.h utf8.h \
exec.h retcode.h names.h terminal.h defines.h wrapper.h
isearch.o: isearch.c isearch.h basic.h retcode.h buffer.h line.h utf8.h \
display.h estruct.h exec.h input.h bind.h search.h terminal.h defines.h \
window.h
util.h window.h
line.o: line.c line.h retcode.h utf8.h buffer.h estruct.h mlout.h \
window.h defines.h
lock.o: lock.c estruct.h lock.h
main.o: main.c estruct.h basic.h retcode.h bind.h bindable.h buffer.h \
line.h utf8.h display.h eval.h execute.h file.h lock.h mlout.h random.h \
search.h terminal.h defines.h termio.h version.h window.h
search.h terminal.h defines.h termio.h util.h version.h window.h
mingw32.o: mingw32.c
mlout.o: mlout.c mlout.h
names.o: names.c names.h basic.h retcode.h bind.h bindable.h buffer.h \
@ -182,7 +184,8 @@ random.o: random.c random.h retcode.h basic.h buffer.h line.h utf8.h \
region.o: region.c region.h line.h retcode.h utf8.h buffer.h estruct.h \
mlout.h random.h window.h defines.h
search.o: search.c search.h line.h retcode.h utf8.h basic.h buffer.h \
display.h estruct.h input.h bind.h mlout.h terminal.h defines.h window.h
display.h estruct.h input.h bind.h mlout.h terminal.h defines.h util.h \
window.h
spawn.o: spawn.c spawn.h defines.h buffer.h line.h retcode.h utf8.h \
display.h estruct.h exec.h file.h flook.h input.h bind.h terminal.h \
window.h
@ -190,6 +193,7 @@ tcap.o: tcap.c terminal.h defines.h retcode.h utf8.h display.h estruct.h \
termio.h
termio.o: termio.c termio.h utf8.h estruct.h retcode.h
utf8.o: utf8.c utf8.h
util.o: util.c util.h
window.o: window.c window.h defines.h buffer.h line.h retcode.h utf8.h \
basic.h display.h estruct.h execute.h terminal.h wrapper.h
word.o: word.c word.h basic.h retcode.h buffer.h line.h utf8.h estruct.h \

4
bind.c
View File

@ -25,6 +25,7 @@
#include "input.h"
#include "line.h"
#include "names.h"
#include "util.h"
#include "window.h"
@ -348,8 +349,7 @@ static int buildlist( char *mstring) {
continue ;
#endif
/* add in the command name */
strncpy( outseq, nptr->n_name, sizeof outseq - 1) ;
outseq[ sizeof outseq - 1] = '\0' ;
mystrscpy( outseq, nptr->n_name, sizeof outseq) ;
cpos = strlen(outseq);
/* search down any keys bound to this */

View File

@ -21,6 +21,7 @@
#include "file.h"
#include "input.h"
#include "mlout.h"
#include "util.h"
#include "window.h"
@ -247,8 +248,7 @@ ask:
}
/* copy buffer name to structure */
strncpy( curbp->b_bname, bufn, sizeof( bname_t) - 1) ;
curbp->b_bname[ sizeof( bname_t) - 1] = '\0' ;
mystrscpy( curbp->b_bname, bufn, sizeof( bname_t)) ;
free( bufn) ;
curwp->w_flag |= WFMODE ; /* make mode line replot */
@ -525,8 +525,7 @@ struct buffer *bfind( const char *bname, int cflag, int bflag)
bp->b_nwnd = 0;
bp->b_linep = lp;
bp->b_fname[ 0] = '\0' ;
strncpy( bp->b_bname, bname, sizeof( bname_t) - 1) ;
bp->b_bname[ sizeof( bname_t) - 1] = '\0' ;
mystrscpy( bp->b_bname, bname, sizeof( bname_t)) ;
lp->l_fp = lp;
lp->l_bp = lp;
}

19
eval.c
View File

@ -29,6 +29,7 @@
#include "search.h"
#include "terminal.h"
#include "termio.h"
#include "util.h"
#include "version.h"
#include "window.h"
@ -437,8 +438,7 @@ static char *gtfun( char *fname) {
ressize = sz + 1 ;
}
strncpy( result, arg1, sz) ;
result[ sz] = 0 ;
mystrscpy( result, arg1, sz + 1) ;
retstr = result ;
}
break ;
@ -482,8 +482,7 @@ static char *gtfun( char *fname) {
ressize = sz + 1 ;
}
strncpy( result, &arg1[ start], sz) ;
result[ sz] = 0 ;
mystrscpy( result, &arg1[ start], sz + 1) ;
retstr = result ;
}
break ;
@ -838,9 +837,8 @@ int setvar(int f, int n)
if( value == NULL)
return FALSE ;
/* a bit overcautious here in using strncpy */
strncpy( value, i_to_a( n), NSTRING - 1) ;
value[ NSTRING - 1] = '\0' ;
/* a bit overcautious here */
mystrscpy( value, i_to_a( n), NSTRING) ;
} else {
status = newmlarg( &value, "Value: ", 0) ;
if (status != TRUE)
@ -928,8 +926,7 @@ fvar:
for (vnum = 0; vnum < MAXVARS; vnum++)
if (uv[vnum].u_name[0] == 0) {
vtype = TKVAR;
strncpy( uv[ vnum].u_name, &var[ 1], NVSIZE) ;
uv[ vnum].u_name[ NVSIZE] = '\0' ;
mystrscpy( uv[ vnum].u_name, &var[ 1], NVSIZE + 1) ;
break;
}
break;
@ -1254,8 +1251,8 @@ char *getval(char *token)
blen = bp->b_dotp->l_used - bp->b_doto;
if( blen >= sizeof buf)
blen = sizeof buf - 1 ;
strncpy(buf, bp->b_dotp->l_text + bp->b_doto, blen);
buf[blen] = 0;
mystrscpy( buf, bp->b_dotp->l_text + bp->b_doto, blen + 1) ;
/* and step the buffer's line ptr ahead a line */
bp->b_dotp = bp->b_dotp->l_fp;

20
exec.c
View File

@ -24,6 +24,7 @@
#include "input.h"
#include "line.h"
#include "random.h"
#include "util.h"
#include "window.h"
@ -182,8 +183,7 @@ static int docmd( char *cline) {
if( !is_it_cmd( tkn)) {
f = TRUE;
/* macarg already includes a getval, skip for now
strncpy( tkn, getval( tkn), sizeof tkn - 1) ;
tkn[ sizeof tkn - 1] = '\0' ;
mystrscpy( tkn, getval( tkn), sizeof tkn) ;
*/
n = atoi(tkn);
@ -316,8 +316,7 @@ static char *token( char *srcstr, char *tok, int maxtoksize) {
if( newtok == NULL)
tok[ 0] = 0 ;
else {
strncpy( tok, newtok, maxtoksize - 1) ;
tok[ maxtoksize - 1] = 0 ;
mystrscpy( tok, newtok, maxtoksize) ;
free( newtok) ;
}
@ -344,8 +343,7 @@ boolean gettokval( char *tok, int size) {
return FALSE ;
/* evaluate it */
strncpy( tok, getval( tmpbuf), size - 1) ;
tok[ size - 1] = '\0' ;
mystrscpy( tok, getval( tmpbuf), size) ;
free( tmpbuf) ;
return TRUE ;
}
@ -457,8 +455,7 @@ int storeproc( int f, int n) {
/* construct the macro buffer name */
bname[ 0] = '*';
strncpy( &bname[ 1], name, sizeof bname - 3) ;
bname[ sizeof bname - 2] = '\0' ;
mystrscpy( &bname[ 1], name, sizeof bname - 2) ;
strcat( bname, "*") ;
free( name) ;
@ -497,8 +494,7 @@ int execproc( int f, int n) {
/* construct the buffer name */
bufn[ 0] = '*' ;
strncpy( &bufn[ 1], name, sizeof bufn - 3) ;
bufn[ sizeof bufn - 2] = '\0' ;
mystrscpy( &bufn[ 1], name, sizeof bufn - 2) ;
strcat( bufn, "*") ;
free( name) ;
@ -689,8 +685,8 @@ static int dobuf(struct buffer *bp)
freewhile(whlist);
return FALSE;
}
strncpy(eline, lp->l_text, linlen);
eline[linlen] = 0; /* make sure it ends */
mystrscpy( eline, lp->l_text, linlen + 1) ;
/* trim leading whitespace */
while (*eline == ' ' || *eline == '\t')

13
file.c
View File

@ -26,6 +26,7 @@
#include "line.h"
#include "lock.h"
#include "mlout.h"
#include "util.h"
#include "window.h"
typedef enum {
@ -223,8 +224,7 @@ int getfile( const char *fname, boolean lockfl) {
makename( bname, fname) ;
break ;
} else { /* TRUE */
strncpy( bname, new_bname, sizeof bname - 1) ;
bname[ sizeof bname - 1] = '\0' ;
mystrscpy( bname, new_bname, sizeof bname) ;
free( new_bname) ;
}
}
@ -282,10 +282,8 @@ int readin(const char *fname, boolean lockfl)
return status ;
bp->b_flag &= ~(BFINVS | BFCHG);
if( fname != bp->b_fname) { /* Copy if source differs from destination */
strncpy( bp->b_fname, fname, sizeof( fname_t) - 1) ;
bp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
}
if( fname != bp->b_fname) /* Copy if source differs from destination */
mystrscpy( bp->b_fname, fname, sizeof( fname_t)) ;
/* let a user macro get hold of things...if he wants */
execute(META | SPEC | 'R', FALSE, 1);
@ -567,8 +565,7 @@ int filename( int f, int n) {
else if( status == FALSE)
curbp->b_fname[ 0] = '\0' ;
else { /* TRUE */
strncpy( curbp->b_fname, fname, sizeof( fname_t) - 1) ;
curbp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
mystrscpy( curbp->b_fname, fname, sizeof( fname_t)) ;
free( fname) ;
}

View File

@ -36,6 +36,7 @@
#include "line.h"
#include "search.h"
#include "terminal.h"
#include "util.h"
#include "window.h"
/*
@ -198,8 +199,7 @@ static int isearch(int f, int n)
cmd_reexecute = -1; /* We're not re-executing (yet?) */
cmd_offset = 0; /* Start at the beginning of the buff */
cmd_buff[0] = '\0'; /* Init the command buffer */
strncpy( pat_save, pat, sizeof pat_save - 1) ; /* Save the old pattern string */
pat_save[ sizeof pat_save - 1] = '\0' ;
mystrscpy( pat_save, pat, sizeof pat_save) ; /* Save the old pattern string */
curline = curwp->w_dotp; /* Save the current line pointer */
curoff = curwp->w_doto; /* Save the current offset */
init_direction = n; /* Save the initial search direction */
@ -277,7 +277,7 @@ static int isearch(int f, int n)
curwp->w_dotp = curline; /* Reset the line pointer */
curwp->w_doto = curoff; /* and the offset */
n = init_direction; /* Reset the search direction */
strncpy( pat, pat_save, sizeof pat) ; /* Restore the old search str */
mystrscpy( pat, pat_save, sizeof pat) ; /* Restore the old search str */
cmd_reexecute = 0; /* Start the whole mess over */
goto start_over; /* Let it take care of itself */

7
main.c
View File

@ -88,6 +88,7 @@
#include "search.h"
#include "terminal.h"
#include "termio.h"
#include "util.h"
#include "version.h"
#include "window.h"
@ -221,8 +222,7 @@ int main(int argc, char **argv)
case 's': /* -s for initial search string */
case 'S':
searchflag = TRUE;
strncpy( pat, &argv[ carg][ 2], sizeof pat - 1) ;
pat[ sizeof pat -1] = 0 ;
mystrscpy( pat, &argv[ carg][ 2], sizeof pat) ;
break;
case 'v': /* -v for View File */
case 'V':
@ -268,8 +268,7 @@ int main(int argc, char **argv)
exit( EXIT_FAILURE) ;
}
strncpy( bp->b_fname, argv[ carg], sizeof bp->b_fname - 1) ; /* max filename length limited to NFILEN - 1 (79) */
bp->b_fname[ sizeof bp->b_fname - 1] = 0 ;
mystrscpy( bp->b_fname, argv[ carg], sizeof bp->b_fname) ; /* max filename length limited to NFILEN - 1 */
bp->b_active = FALSE;
if (firstfile) {
firstbp = bp;

View File

@ -20,6 +20,8 @@
#endif
#include <errno.h>
#include "util.h"
/* Maximum file length name 255 */
#define MAXLOCK 256
#define MAXNAME 128
@ -51,8 +53,7 @@ char *dolock( const char *fname)
int mask;
struct stat sbuf;
strncpy( lname, fname, sizeof lname - 1 - 6) ;
lname[ sizeof lname - 1 - 6] = 0 ;
mystrscpy( lname, fname, sizeof lname - 6) ;
strcat( lname, ".lock~") ;
/* check that we are not being cheated, qname must point to */
@ -126,8 +127,7 @@ char *dolock( const char *fname)
char *undolock( const char *fname) {
char lname[ MAXLOCK] ;
strncpy( lname, fname, sizeof lname - 1 - 6) ;
lname[ sizeof lname - 1 - 6] = 0 ;
mystrscpy( lname, fname, sizeof lname - 6) ;
strcat( lname, ".lock~") ;
if (unlink(lname) != 0) {
if (errno == EACCES || errno == ENOENT)

View File

@ -72,6 +72,7 @@
#include "line.h"
#include "mlout.h"
#include "terminal.h"
#include "util.h"
#include "window.h"
/* The variable matchlen holds the length of the matched
@ -683,8 +684,7 @@ int eq(unsigned char bc, unsigned char pc)
}
void setprompt( char *tpat, unsigned tpat_size, char *prompt, char *apat) {
strncpy( tpat, prompt, 14) ; /* copy prompt to output string */
tpat[ 14] = '\0' ; /* longest prompt is "Reverse Search" */
mystrscpy( tpat, prompt, 15) ; /* copy prompt to output string */
strcat( tpat, " (") ; /* build new prompt string */
expandp( apat, &tpat[ strlen( tpat)], tpat_size) ; /* add old pattern */
strcat( tpat, ")<Meta>: ") ;
@ -716,8 +716,7 @@ static int readpattern(char *prompt, char *apat, int srch)
*/
status = newmlargt( &dynpat, tpat, NPAT) ;
if( status == TRUE) {
strncpy( apat, dynpat, NPAT - 1) ;
apat[ NPAT - 1] = 0 ;
mystrscpy( apat, dynpat, NPAT) ;
free( dynpat) ;
if (srch) { /* If we are doing the search string. */
/* Reverse string copy, and remember

17
util.c Normal file
View File

@ -0,0 +1,17 @@
#include "util.h"
/* Safe zeroing, no complaining about overlap */
void mystrscpy(char *dst, const char *src, int size)
{
if (!size)
return;
while (--size) {
char c = *src++;
if (!c)
break;
*dst++ = c;
}
*dst = 0;
}

7
util.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef UTIL_H_
#define UTIL_H_
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
void mystrscpy(char *dst, const char *src, int size);
#endif /* UTIL_H_ */