[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: gEDA-user: Removing My* memory alllocation functions



On Wed, 2010-12-08 at 03:43 +1100, Stephen Ecob wrote:
> That sounds good.  In the interest of saving your development time (I
> don't want to keep you from PCB+GL !), I suggest:
> Of the 51 calls to MyStrdup()
>     7 are "safe" because of calling with a fixed string eg MyStrdup
> ("Font", "FontEdit")
>     2 are "safe" because of calling with the UNKNOWN macro, which can
> never expand to NULL
>     the remaining 42 should be assumed to be unsafe and replaced with
> (x) ? strdup(x) : NULL or equivalent
> I'd guess that many of the 42 are safe but I don't think it's worth
> the time to exhaustively prove each safe or unsafe.

Some are easily determined to be safe, the major examples of those
being:

1) if (x) {
     ...
     y = strdup (x);
   }

2) char x[BUFLEN];
   ...
   y = strdup (x);

There are further cases, for example, where the string comes from an
action argument, and the function checks to ensure the correct number of
arguments are present.

I've come up with the attached as the "safe" changes. (26 cases).


-- 
Peter Clifton

Electrical Engineering Division,
Engineering Department,
University of Cambridge,
9, JJ Thomson Avenue,
Cambridge
CB3 0FA

Tel: +44 (0)7729 980173 - (No signal in the lab!)
Tel: +44 (0)1223 748328 - (Shared lab phone, ask for me)
From 9432c61dc1248f761db2b0b6e3964ddcc0054640 Mon Sep 17 00:00:00 2001
From: Peter Clifton <pcjc2@xxxxxxxxx>
Date: Tue, 7 Dec 2010 16:58:35 +0000
Subject: [PATCH] Convet some "safe" usage of MyStrdup to plain strdup

These cases are ones where we know the caller will not risk
passing a NULL string to duplicate.
---
 src/action.c   |    6 +++---
 src/buffer.c   |    5 ++---
 src/create.c   |   13 +++++--------
 src/file.c     |   15 ++++++---------
 src/fontmode.c |    8 ++++----
 src/main.c     |    4 ++--
 src/misc.c     |    4 ++--
 src/move.c     |    2 +-
 src/netlist.c  |    2 +-
 9 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/src/action.c b/src/action.c
index f52fcf4..3eb8574 100644
--- a/src/action.c
+++ b/src/action.c
@@ -5731,7 +5731,7 @@ ActionSaveTo (int argc, char **argv, int x, int y)
   if (strcasecmp (function, "LayoutAs") == 0)
     {
       MYFREE (PCB->Filename);
-      PCB->Filename = MyStrdup (name, __FUNCTION__);
+      PCB->Filename = strdup (name);
       SavePCB (PCB->Filename);
       return 0;
     }
@@ -5929,7 +5929,7 @@ ActionNew (int argc, char **argv, int x, int y)
   if (!PCB->Changed || gui->confirm_dialog (_("OK to clear layout data?"), 0))
     {
       if (name)
-	name = MyStrdup (name, "ActionNew");
+	name = strdup (name);
       else
 	name = gui->prompt_for (_("Enter the layout name:"), "");
 
@@ -7238,7 +7238,7 @@ ActionElementSetAttr (int argc, char **argv, int x, int y)
   if (attr && value)
     {
       MYFREE (attr->value);
-      attr->value = MyStrdup (value, "ElementSetAttr");
+      attr->value = strdup (value);
     }
   if (attr && ! value)
     {
diff --git a/src/buffer.c b/src/buffer.c
index 1b2d27c..944a8ed 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -153,7 +153,7 @@ AddLineToBuffer (LayerTypePtr Layer, LineTypePtr Line)
 			       MaskFlags (Line->Flags,
 					  FOUNDFLAG | ExtraFlag));
   if (line && Line->Number)
-    line->Number = MyStrdup (Line->Number, "AddLineToBuffer");
+    line->Number = strdup (Line->Number);
   return (line);
 }
 
@@ -1154,8 +1154,7 @@ ConvertBufferToElement (BufferTypePtr Buffer)
   LINE_LOOP (&Buffer->Data->SILKLAYER);
   {
     if (line->Number && !NAMEONPCB_NAME (Element))
-      NAMEONPCB_NAME (Element) = MyStrdup (line->Number,
-					   "ConvertBufferToElement");
+      NAMEONPCB_NAME (Element) = strdup (line->Number);
     CreateNewLineInElement (Element, line->Point1.X,
 			    line->Point1.Y, line->Point2.X,
 			    line->Point2.Y, line->Thickness);
diff --git a/src/create.c b/src/create.c
index bf3b688..b2b9af4 100644
--- a/src/create.c
+++ b/src/create.c
@@ -213,10 +213,8 @@ CreateNewPCBPost (PCBTypePtr pcb, int use_defaults)
       if (ParseGroupString (Settings.Groups, &pcb->LayerGroups, DEF_LAYER))
 	return 1;
 
-      pcb->Data->Layer[component_silk_layer].Name =
-	MyStrdup ("silk", "CreateNewPCB()");
-      pcb->Data->Layer[solder_silk_layer].Name =
-	MyStrdup ("silk", "CreateNewPCB()");
+      pcb->Data->Layer[component_silk_layer].Name = strdup ("silk");
+      pcb->Data->Layer[solder_silk_layer].Name = strdup ("silk");
     }
   return 0;
 }
@@ -896,8 +894,7 @@ AddTextToElement (TextTypePtr Text, FontTypePtr PCBFont,
   Text->Direction = Direction;
   Text->Flags = Flags;
   Text->Scale = Scale;
-  Text->TextString = (TextString && *TextString) ?
-    MyStrdup (TextString, "AddTextToElement()") : NULL;
+  Text->TextString = (TextString && *TextString) ? strdup (TextString) : NULL;
 
   /* calculate size of the bounding box */
   SetTextBoundingBox (PCBFont, Text);
@@ -976,12 +973,12 @@ CreateNewNet (LibraryTypePtr lib, char *name, char *style)
 
   sprintf (temp, "  %s", name);
   menu = GetLibraryMenuMemory (lib);
-  menu->Name = MyStrdup (temp, "CreateNewNet()");
+  menu->Name = strdup (temp);
   menu->flag = 1;		/* net is enabled by default */
   if (style == NULL || NSTRCMP ("(unknown)", style) == 0)
     menu->Style = NULL;
   else
-    menu->Style = MyStrdup (style, "CreateNewNet()");
+    menu->Style = strdup (style);
   return (menu);
 }
 
diff --git a/src/file.c b/src/file.c
index 6893379..13a8444 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1383,8 +1383,7 @@ ReadLibraryContents (void)
       if (!strncmp (inputline, "TYPE=", 5))
 	{
 	  menu = GetLibraryMenuMemory (&Library);
-	  menu->Name = MyStrdup (UNKNOWN (&inputline[5]),
-				 "ReadLibraryDescription()");
+	  menu->Name = strdup (UNKNOWN (&inputline[5]));
 	  menu->directory = strdup (Settings.LibraryFilename);
 	}
       else
@@ -1393,13 +1392,11 @@ ReadLibraryContents (void)
 	  if (!menu)
 	    {
 	      menu = GetLibraryMenuMemory (&Library);
-	      menu->Name = MyStrdup (UNKNOWN ((char *) NULL),
-				     "ReadLibraryDescription()");
+	      menu->Name = strdup (UNKNOWN ((char *) NULL));
 	      menu->directory = strdup (Settings.LibraryFilename);
 	    }
 	  entry = GetLibraryEntryMemory (menu);
-	  entry->AllocatedMemory = MyStrdup (inputline,
-					     "ReadLibraryDescription()");
+	  entry->AllocatedMemory = strdup (inputline);
 
 	  /* now break the line into pieces separated by colons */
 	  if ((entry->Template = strtok (entry->AllocatedMemory, ":")) !=
@@ -1526,7 +1523,7 @@ ReadNetlist (char *filename)
 	  if (kind == 0)
 	    {
 	      menu = GetLibraryMenuMemory (&PCB->NetlistLib);
-	      menu->Name = MyStrdup (temp, "ReadNetlist()");
+	      menu->Name = strdup (temp);
 	      menu->flag = 1;
 	      kind++;
 	    }
@@ -1535,12 +1532,12 @@ ReadNetlist (char *filename)
 	      if (kind == 1 && strchr (temp, '-') == NULL)
 		{
 		  kind++;
-		  menu->Style = MyStrdup (temp, "ReadNetlist()");
+		  menu->Style = strdup (temp);
 		}
 	      else
 		{
 		  entry = GetLibraryEntryMemory (menu);
-		  entry->ListEntry = MyStrdup (temp, "ReadNetlist()");
+		  entry->ListEntry = strdup (temp);
 		}
 	    }
 	}
diff --git a/src/fontmode.c b/src/fontmode.c
index bd9fcee..4f7f5b8 100644
--- a/src/fontmode.c
+++ b/src/fontmode.c
@@ -94,10 +94,10 @@ FontEdit (int argc, char **argv, int Ux, int Uy)
   PCB->MaxWidth = CELL_SIZE * 18;
   PCB->MaxHeight = CELL_SIZE * ((MAX_FONTPOSITION + 15) / 16 + 2);
   PCB->Grid = 500.0;
-  PCB->Data->Layer[0].Name = MyStrdup ("Font", "FontEdit");
-  PCB->Data->Layer[1].Name = MyStrdup ("OrigFont", "FontEdit");
-  PCB->Data->Layer[2].Name = MyStrdup ("Width", "FontEdit");
-  PCB->Data->Layer[3].Name = MyStrdup ("Grid", "FontEdit");
+  PCB->Data->Layer[0].Name = strdup ("Font");
+  PCB->Data->Layer[1].Name = strdup ("OrigFont");
+  PCB->Data->Layer[2].Name = strdup ("Width");
+  PCB->Data->Layer[3].Name = strdup ("Grid");
   hid_action ("PCBChanged");
   hid_action ("LayersChanged");
 
diff --git a/src/main.c b/src/main.c
index ba8c959..65141bb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -957,7 +957,7 @@ main (int argc, char *argv[])
     {
       char buf[20];
       sprintf (buf, "signal%d", i + 1);
-      Settings.DefaultLayerName[i] = MyStrdup (buf, "DefaultLayerNames");
+      Settings.DefaultLayerName[i] = strdup (buf);
       Settings.LayerColor[i] = "#c49350";
       Settings.LayerSelectedColor[i] = "#00ffff";
     }
@@ -1018,7 +1018,7 @@ main (int argc, char *argv[])
        * file might not exist
        */
       if (LoadPCB (command_line_pcb))
-	PCB->Filename = MyStrdup (command_line_pcb, "main()");
+	PCB->Filename = strdup (command_line_pcb);
     }
 
   if (Settings.InitialLayerStack
diff --git a/src/misc.c b/src/misc.c
index 76b05c0..b87a6a6 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -723,7 +723,7 @@ ParseRouteString (char *s, RouteStyleTypePtr routeStyle, int scale)
       for (i = 0; *s && *s != ','; i++)
         Name[i] = *s++;
       Name[i] = '\0';
-      routeStyle->Name = MyStrdup (Name, "ParseRouteString()");
+      routeStyle->Name = strdup (Name);
       if (!isdigit ((int) *++s))
         goto error;
       GetNum (&s, &routeStyle->Thick);
@@ -943,7 +943,7 @@ EvaluateFilename (char *Template, char *Path, char *Filename, char *Parameter)
   if (Settings.verbose)
     printf ("EvaluateFilename: \033[32m%s\033[0m\n", command.Data);
 
-  return (MyStrdup (command.Data, "EvaluateFilename()"));
+  return strdup (command.Data);
 }
 
 /* ---------------------------------------------------------------------------
diff --git a/src/move.c b/src/move.c
index cce6e19..cbb2e3d 100644
--- a/src/move.c
+++ b/src/move.c
@@ -970,7 +970,7 @@ MoveLayer (int old_index, int new_index)
       max_copper_layer++;
       memset (lp, 0, sizeof (LayerType));
       lp->On = 1;
-      lp->Name = MyStrdup ("New Layer", "MoveLayer");
+      lp->Name = strdup ("New Layer");
       lp->Color = Settings.LayerColor[new_index];
       lp->SelectedColor = Settings.LayerSelectedColor[new_index];
       for (l = 0; l < max_copper_layer; l++)
diff --git a/src/netlist.c b/src/netlist.c
index ade8326..459b08a 100644
--- a/src/netlist.c
+++ b/src/netlist.c
@@ -245,7 +245,7 @@ netlist_style (LibraryMenuType *net, const char *style)
   if (net->Style)
     MYFREE (net->Style);
   if (style)
-    net->Style = MyStrdup ((char *)style, "Netlist(Style)");
+    net->Style = strdup ((char *)style);
 }
 
 /* The primary purpose of this action is to rebuild a netlist from a
-- 
1.7.1


_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user