forked from bonsai/harakit
scrut(1): fix buffer overflow in option parsing
This commit is contained in:
parent
5b364e104e
commit
64a1a19bd8
17
src/scrut.c
17
src/scrut.c
@ -18,7 +18,7 @@
|
||||
|
||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
||||
#include <stdlib.h> /* EXIT_FAILURE */
|
||||
#include <string.h> /* strchr(3) */
|
||||
#include <string.h> /* memset(3), strchr(3) */
|
||||
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
|
||||
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
|
||||
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||
@ -33,17 +33,24 @@ int main(int argc, char *argv[]){
|
||||
struct stat buf;
|
||||
int c;
|
||||
size_t i;
|
||||
char *p;
|
||||
|
||||
if(argc < 2)
|
||||
goto usage;
|
||||
|
||||
i = 0;
|
||||
memset(ops, '\0', sizeof ops);
|
||||
while((c = getopt(argc, argv, args)) != -1)
|
||||
if(strchr(args, c) == NULL)
|
||||
if((p = strchr(args, c)) == NULL)
|
||||
goto usage;
|
||||
else
|
||||
ops[i++] = c;
|
||||
ops[i] = '\0';
|
||||
ops[p - args] = c;
|
||||
/* straighten out ops */
|
||||
for(i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i)
|
||||
if(ops[i] != '\0'){
|
||||
*p = ops[i];
|
||||
if(&ops[i] != p++)
|
||||
ops[i] = '\0';
|
||||
}
|
||||
|
||||
if(optind == argc)
|
||||
goto usage;
|
||||
|
Loading…
Reference in New Issue
Block a user