fix copyinstr(9) bug (from Charles M. Hannum).

This commit is contained in:
msaitoh 1999-12-08 17:12:21 +00:00
parent 9758d8ed66
commit e7c433b9d5
1 changed files with 38 additions and 48 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: Locore.c,v 1.1 1999/09/13 10:31:26 itojun Exp $ */ /* $NetBSD: Locore.c,v 1.2 1999/12/08 17:12:21 msaitoh Exp $ */
/*- /*-
* Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
@ -245,34 +245,31 @@ copyoutstr(kaddr, uaddr, maxlen, lencopied)
curpcb->pcb_onfault = &&Err999; curpcb->pcb_onfault = &&Err999;
if ((cnt = (char *)VM_MAXUSER_ADDRESS - to) < maxlen) if ((cnt = (char *)VM_MAXUSER_ADDRESS - to) > maxlen)
maxlen = cnt; cnt = maxlen;
else
cnt = maxlen - 1;
while (cnt--) { while (cnt--) {
if ((*to++ = *from++) == 0) if ((*to++ = *from++) == 0) {
break; rc = 0;
goto out;
}
} }
*lencopied = from - from_top; if (to >= (char *)VM_MAXUSER_ADDRESS)
rc = EFAULT;
if (cnt == 0) { else
if (to >= (char *)VM_MAXUSER_ADDRESS) rc = ENAMETOOLONG;
rc = EFAULT;
else
rc = ENAMETOOLONG;
} else
rc = 0;
out:
if (lencopied)
*lencopied = from - from_top;
curpcb->pcb_onfault = 0; curpcb->pcb_onfault = 0;
return rc; return rc;
Err999: Err999:
curpcb->pcb_onfault = 0; if (lencopied)
if (lencopied != 0)
*lencopied = from - from_top; *lencopied = from - from_top;
curpcb->pcb_onfault = 0;
return EFAULT; return EFAULT;
} }
@ -298,36 +295,31 @@ copyinstr(uaddr, kaddr, maxlen, lencopied)
curpcb->pcb_onfault = &&Err999; curpcb->pcb_onfault = &&Err999;
if ((cnt = (char *)VM_MAXUSER_ADDRESS - from) < maxlen) if ((cnt = (char *)VM_MAXUSER_ADDRESS - to) > maxlen)
maxlen = cnt; cnt = maxlen;
else
cnt = maxlen - 1;
while (cnt--) { while (cnt--) {
if ((*to++ = *from++) == 0) if ((*to++ = *from++) == 0) {
break; rc = 0;
goto out;
}
} }
if (lencopied != NULL) if (to >= (char *)VM_MAXUSER_ADDRESS)
rc = EFAULT;
else
rc = ENAMETOOLONG;
out:
if (lencopied)
*lencopied = from - from_top; *lencopied = from - from_top;
if (cnt == 0 && *(from - 1) != 0) {
if (to >= (char *)VM_MAXUSER_ADDRESS)
rc = EFAULT;
else
rc = ENAMETOOLONG;
} else
rc = 0;
curpcb->pcb_onfault = 0; curpcb->pcb_onfault = 0;
return rc; return rc;
Err999: Err999:
curpcb->pcb_onfault = 0; if (lencopied)
if (lencopied != 0)
*lencopied = from - from_top; *lencopied = from - from_top;
curpcb->pcb_onfault = 0;
return EFAULT; return EFAULT;
} }
@ -349,18 +341,16 @@ copystr(kfaddr, kdaddr, maxlen, lencopied)
int i; int i;
for (i = 0; i < maxlen; i++) { for (i = 0; i < maxlen; i++) {
if ((*to++ = *from++) == NULL) if ((*to++ = *from++) == NULL) {
break; if (lencopied)
*lencopied = i + 1;
return (0);
}
} }
if (i == maxlen) { if (lencopied)
*lencopied = i; *lencopied = i;
return ENAMETOOLONG; return (ENAMETOOLONG);
} else {
if (lencopied)
*lencopied = i + 1;
return 0;
}
} }
/* /*