use mrb_debug_strdup() and mrb_debug_strndup()
authorcremno <[email protected]>
Tue, 25 Nov 2014 23:54:55 +0000 (26 00:54 +0100)
committercremno <[email protected]>
Mon, 1 Dec 2014 23:00:55 +0000 (2 00:00 +0100)
As they are safer to use than mrb_malloc()+strlen()+strncpy() (see #2652).

mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c
mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c
mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c

index f1bf34a..9515e8b 100755 (executable)
@@ -14,6 +14,7 @@
 #include "mruby/variable.h"
 #include "mrdberror.h"
 #include "apibreak.h"
+#include "apistring.h"
 
 #define MAX_BREAKPOINTNO (MAX_BREAKPOINT * 1024)
 #define MRB_DEBUG_BP_FILE_OK   (0x0001)
@@ -202,7 +203,7 @@ mrb_debug_set_break_line( mrb_state *mrb, mrb_debug_context *dbg, const char *fi
     return MRB_DEBUG_BREAK_INVALID_LINENO;
   } 
 
-  set_file = mrb_malloc(mrb, strlen(file) + 1);
+  set_file = mrb_debug_strdup(mrb, file);
   if(set_file == NULL) {
     return MRB_DEBUG_NOBUF;
   }
@@ -215,8 +216,6 @@ mrb_debug_set_break_line( mrb_state *mrb, mrb_debug_context *dbg, const char *fi
   dbg->bp[index].point.linepoint.lineno = lineno;
   dbg->bpnum++;
 
-  strncpy(set_file, file, strlen(file) + 1);
-
   dbg->bp[index].point.linepoint.file = set_file;
 
   return dbg->bp[index].bpno;
@@ -242,18 +241,16 @@ mrb_debug_set_break_method( mrb_state *mrb, mrb_debug_context *dbg, const char *
   }
 
   if(class_name != NULL) {
-    set_class = mrb_malloc(mrb, strlen(class_name) + 1);
+    set_class = mrb_debug_strdup(mrb, class_name);
     if(set_class == NULL) {
       return MRB_DEBUG_NOBUF;
     }
-    
-    strncpy(set_class, class_name, strlen(class_name) + 1);
   }
   else {
     set_class = NULL;
   }
 
-  set_method = mrb_malloc(mrb, strlen(method_name) + 1);
+  set_method = mrb_debug_strdup(mrb, method_name);
   if(set_method == NULL) {
     if(set_class != NULL) {
       mrb_free(mrb, (void*)set_class);
@@ -261,8 +258,6 @@ mrb_debug_set_break_method( mrb_state *mrb, mrb_debug_context *dbg, const char *
     return MRB_DEBUG_NOBUF;
   }
 
-  strncpy(set_method, method_name, strlen(method_name) + 1);
-
   index = dbg->bpnum;
   dbg->bp[index].bpno = dbg->next_bpno;
   dbg->next_bpno++;
index 734f03f..8eddc7b 100755 (executable)
@@ -9,6 +9,7 @@
 #include "mrdb.h"
 #include "mrdberror.h"
 #include "apilist.h"
+#include "apistring.h"
 #include "mruby/compile.h"
 #include "mruby/irep.h"
 #include "mruby/debug.h"
@@ -64,7 +65,7 @@ static char*
 dirname(mrb_state *mrb, const char *path)
 {
   size_t len;
-  char *p, *dir;
+  char *p;
 
   if (path == NULL) {
     return NULL;
@@ -73,11 +74,7 @@ dirname(mrb_state *mrb, const char *path)
   p = strrchr(path, '/');
   len = p != NULL ? p - path : strlen(path);
 
-  if ((dir = mrb_malloc(mrb, len + 1)) != NULL) {
-    strncpy(dir, path, len);
-    dir[len] = '\0';
-  }
-  return dir;
+  return mrb_debug_strndup(mrb, path, len);
 }
 
 static source_file*
@@ -98,8 +95,10 @@ source_file_new(mrb_state *mrb, mrb_debug_context *dbg, char *filename)
   }
 
   file->lineno = 1;
-  file->path = mrb_malloc(mrb, strlen(filename) + 1);
-  strcpy(file->path, filename);
+  if ((file->path = mrb_debug_strdup(mrb, filename)) == NULL) {
+    source_file_free(mrb, file);
+    return NULL;
+  }
   return file;
 }
 
index 74e10ab..7b0806e 100755 (executable)
@@ -8,6 +8,7 @@
 #include <string.h>
 
 #include "apilist.h"
+#include "apistring.h"
 #include "mruby/compile.h"
 
 typedef struct help_msg {
@@ -226,10 +227,7 @@ parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st)
     len = strlen(*sp);
   }
 
-  if (len > 0) {
-    st->filename = mrb_malloc(mrb, len + 1);
-    strncpy(st->filename, *sp, len);
-    st->filename[len] = '\0';
+  if (len > 0 && (st->filename = mrb_debug_strndup(mrb, *sp, len)) != NULL) {
     *sp += len;
     return TRUE;
   }