changeset 36:ebeeb6c20d0a

Fix SEGV when argument is empty and invalid command
author atton
date Thu, 05 Nov 2015 19:25:56 +0900
parents f01dc83040a9
children 3b8858a63694
files Makefile ie-virsh.c
diffstat 2 files changed, 25 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Thu Nov 05 18:28:03 2015 +0900
+++ b/Makefile	Thu Nov 05 19:25:56 2015 +0900
@@ -1,5 +1,5 @@
 TARGET = ie-virsh
-CFLAGS = -Wall -O2 -g
+CFLAGS = -Wall -O0 -g
 
 INSTALL_DIR = /usr/local/bin
 
--- a/ie-virsh.c	Thu Nov 05 18:28:03 2015 +0900
+++ b/ie-virsh.c	Thu Nov 05 19:25:56 2015 +0900
@@ -77,8 +77,8 @@
     return list;
 }
 
-void 
-print_vmlist(VMLISTPTR list) 
+void
+print_vmlist(VMLISTPTR list)
 {
     for(;list && list->name[0]; list=list->next) {
         fprintf(stdout, "   %s\n",list->name);
@@ -123,10 +123,10 @@
     return 0;
 }
 
-void 
+void
 usage()
 {
-    printf("Usage: COMMAND [define|list|start|destroy|xmldump|undefine|console] [vm-name]\n");
+    printf("Usage: COMMAND [define|list|start|destroy|dumpxml|undefine|console] [vm-name]\n");
     printf("   vm-name should be 01 - 04\n");
 }
 
@@ -134,7 +134,7 @@
 check_user_name(const char *account_name)
 {
     const char *regex = "[ek]([0-9]{6})";
-    
+
     regex_t *pattern = NEW(regex_t);
     int ret = 1;
 
@@ -181,6 +181,21 @@
 void
 make_vm_name(char *vm_name, const int account_type, const char *account_name, const char *vm_num)
 {
+    const char *regex    = "0[1-4]";
+    regex_t *num_pattern = NEW(regex_t);
+
+    if (regcomp(num_pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) {
+        exit(0);
+    }
+
+    int ret = regexec(num_pattern, vm_num, (size_t) 0, NULL, 0);
+    regfree(num_pattern);
+
+    if (ret){
+        fprintf(stderr, "This name is invalid number: %s\n", vm_num);
+        exit(0);
+    }
+
     switch(account_type) {
         case STUDENTS:
             strncat(vm_name, students_sym, VM_NAME_LENGTH);
@@ -243,27 +258,10 @@
     seteuid(0);
     setgid(0);
     setuid(0);
-
     char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH);
     vm_name[0] = '\0';
-    if (!(strncmp(argv[1], "list", 4) == 0 || strncmp(argv[1], debug_command, 5) == 0)) {
-        const char *regex = "0[1-4]";
-        regex_t *num_pattern = NEW(regex_t);
 
-        if (regcomp(num_pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) {
-            exit(0);
-        }
-
-        int ret = regexec(num_pattern, argv[2], (size_t) 0, NULL, 0);
-        regfree(num_pattern);
-
-        if (ret){
-            fprintf(stderr, "This name is not number: %s\n", argv[2]);
-            exit(0);
-        }
-
-        make_vm_name(vm_name, account_type, name, argv[2]);
-    }
+    if ((argc > 2) && (strncmp(argv[1], "list", 4) != 0)) { make_vm_name(vm_name, account_type, name, argv[2]); }
 
     if (argc>=3) {
         if ( strncmp(argv[1], "define", 6) == 0 ) {
@@ -308,9 +306,10 @@
             print_vmlist(vmlist);
             exit(0);
         }
-    } 
+    }
 
-    /* Check argv for proper arguments and run 
+
+    /* Check argv for proper arguments and run
      * the corresponding script, if invoked.
      */