sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit a7ad11ed5e3300c4d8eeab4d01a0a32eaf1f522d
parent c59894bd5c41f2d4b4a03755a8aefbf470be28f2
Author: sin <sin@2f30.org>
Date:   Thu, 13 Nov 2014 14:24:26 +0000

Fix overflow in basename(1)

Diffstat:
Mbasename.c | 13+++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/basename.c b/basename.c @@ -17,8 +17,7 @@ usage(void) int main(int argc, char *argv[]) { - char *s; - size_t n; + char *s, *p; ARGBEGIN { default: @@ -29,13 +28,11 @@ main(int argc, char *argv[]) usage(); s = basename(argv[0]); - if(argc == 2 && argv[1]) { - n = strlen(s) - strlen(argv[1]); - if(!strcmp(&s[n], argv[1])) - s[n] = '\0'; + if(argc == 2) { + p = strstr(s, argv[1]); + if (p && p[strlen(p)] == '\0') + *p = '\0'; } - puts(s); - return 0; }