Whamcloud - gitweb
LU-4629 utils: fix array overflow
[fs/lustre-release.git] / libcfs / libcfs / util / parser.c
index ce2a99e..b40250b 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * Copyright (C) 2001 Cluster File Systems, Inc.
  *
  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  */
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <string.h>
-#include <stddef.h>
-#include <unistd.h>
-#include <sys/param.h>
-#include <assert.h>
 
 #include <libcfs/libcfsutil.h>
 
@@ -69,21 +59,17 @@ static char * skiptowhitespace(char * s)
 
 static int line2args(char *line, char **argv, int maxargs)
 {
-        char *arg;
-        int i = 0;
+       char *arg;
+       int i = 0;
 
-        arg = strtok(line, " \t");
-        if ( arg ) {
-                argv[i] = arg;
-                i++;
-        } else
-                return 0;
+       arg = strtok(line, " \t");
+       if (arg == NULL || maxargs < 1)
+               return 0;
 
-        while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
-                argv[i] = arg;
-                i++;
-        }
-        return i;
+       argv[i++] = arg;
+       while ((arg = strtok(NULL, " \t")) != NULL && i < maxargs)
+               argv[i++] = arg;
+       return i;
 }
 
 /* find a command -- return it if unique otherwise print alternatives */
@@ -137,7 +123,7 @@ static command_t * find_cmd(char * name, command_t cmds[], char ** next)
            this with strtok*/
         name = skipwhitespace(name);
         *next = skiptowhitespace(name);
-        len = *next - name;
+        len = (int)(*next - name);
         if (len == 0)
                 return NULL;
 
@@ -343,7 +329,7 @@ char * readline(char * prompt)
                 if ((c = fgetc(stdin)) != EOF) {
                         if (c == '\n')
                                 goto out;
-                        *ptr++ = c;
+                        *ptr++ = (char)c;
 
                         if (ptr - line >= size - 1) {
                                 char *tmp;
@@ -470,7 +456,9 @@ int Parser_help(int argc, char **argv)
 
         line[0]='\0';
         for ( i = 1 ;  i < argc ; i++ ) {
-                strcat(line, argv[i]);
+               if (strlen(argv[i]) > sizeof(line)-strlen(line)-1)
+                       return -E2BIG;
+               strncat(line, argv[i], sizeof(line)-strlen(line)-1);
         }
 
         switch ( process(line, &next, top_level, &result, &prev) ) {