[svn-commits] trunk - r8065 in /trunk: apps/ doc/ include/asterisk/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Fri Jan 13 12:38:58 CST 2006


Author: russell
Date: Fri Jan 13 12:38:55 2006
New Revision: 8065

URL: http://svn.digium.com/view/asterisk?rev=8065&view=rev
Log:
Various cleanups from comments in an email from Luigi Rizzo.  Thank you!
- Use a cleaner syntax for declaring the allocation macros
- Fix return value for ast_strdup/ast_strndup
- remove safe_strdup from app_macro, since ast_strup does the same thing
- fix a place in app_queue where ast_calloc+strncpy was used instead of
  ast_strdup.  If you are helping out with these conversions, please watch out
  for other places where this is done.
- add a note to the coding guidelines about the fix to app_queue

Modified:
    trunk/apps/app_macro.c
    trunk/apps/app_queue.c
    trunk/doc/CODING-GUIDELINES
    trunk/include/asterisk/utils.h

Modified: trunk/apps/app_macro.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_macro.c?rev=8065&r1=8064&r2=8065&view=diff
==============================================================================
--- trunk/apps/app_macro.c (original)
+++ trunk/apps/app_macro.c Fri Jan 13 12:38:55 2006
@@ -88,11 +88,6 @@
 STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
-
-static char *safe_strdup(const char *s)
-{
-	return s ? strdup(s) : NULL;
-}
 
 static int macro_exec(struct ast_channel *chan, void *data)
 {
@@ -169,17 +164,17 @@
 	}
 	argc = 1;
 	/* Save old macro variables */
-	save_macro_exten = safe_strdup(pbx_builtin_getvar_helper(chan, "MACRO_EXTEN"));
+	save_macro_exten = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_EXTEN"));
 	pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
 
-	save_macro_context = safe_strdup(pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT"));
+	save_macro_context = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT"));
 	pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
 
-	save_macro_priority = safe_strdup(pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY"));
+	save_macro_priority = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY"));
 	snprintf(pc, sizeof(pc), "%d", oldpriority);
 	pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
   
-	save_macro_offset = safe_strdup(pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"));
+	save_macro_offset = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"));
 	pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
 
 	/* Setup environment for new run */

Modified: trunk/apps/app_queue.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_queue.c?rev=8065&r1=8064&r2=8065&view=diff
==============================================================================
--- trunk/apps/app_queue.c (original)
+++ trunk/apps/app_queue.c Fri Jan 13 12:38:55 2006
@@ -1709,8 +1709,7 @@
 						if (in->cid.cid_ani) {
 							if (o->chan->cid.cid_ani)
 								free(o->chan->cid.cid_ani);
-							if ((o->chan->cid.cid_ani = ast_calloc(1, strlen(in->cid.cid_ani) + 1)))
-								strncpy(o->chan->cid.cid_ani, in->cid.cid_ani, strlen(in->cid.cid_ani) + 1);
+							o->chan->cid.cid_ani = ast_strdup(in->cid.cid_ani);
 						}
 						if (o->chan->cid.cid_rdnis) 
 							free(o->chan->cid.cid_rdnis);

Modified: trunk/doc/CODING-GUIDELINES
URL: http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES?rev=8065&r1=8064&r2=8065&view=diff
==============================================================================
--- trunk/doc/CODING-GUIDELINES (original)
+++ trunk/doc/CODING-GUIDELINES Fri Jan 13 12:38:55 2006
@@ -386,6 +386,10 @@
 	
 	newstr = ast_strdup(str);
 
+Furthermore, it is unnecessary to have code that malloc/calloc's for the length
+of a string (+1 for the terminating '\0') and then using strncpy to copy the
+copy the string into the resulting buffer.  This is the exact same thing as
+using ast_strdup.
 
 * CLI Commands
 --------------

Modified: trunk/include/asterisk/utils.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/utils.h?rev=8065&r1=8064&r2=8065&view=diff
==============================================================================
--- trunk/include/asterisk/utils.h (original)
+++ trunk/include/asterisk/utils.h Fri Jan 13 12:38:55 2006
@@ -252,9 +252,7 @@
   The argument and return value are the same as malloc()
 */
 #define ast_malloc(len) \
-	({ \
-		(_ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \
-	})
+	_ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
 AST_INLINE_API(
 void *_ast_malloc(size_t len, const char *file, int lineno, const char *func),
@@ -279,9 +277,7 @@
   The arguments and return value are the same as calloc()
 */
 #define ast_calloc(num, len) \
-	({ \
-		(_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \
-	})
+	_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
 AST_INLINE_API(
 void *_ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func),
@@ -306,9 +302,7 @@
   The arguments and return value are the same as realloc()
 */
 #define ast_realloc(p, len) \
-	({ \
-		(_ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \
-	})
+	_ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
 AST_INLINE_API(
 void *_ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func),
@@ -337,12 +331,10 @@
   The argument and return value are the same as strdup()
 */
 #define ast_strdup(str) \
-	({ \
-		(_ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \
-	})
-
-AST_INLINE_API(
-void *_ast_strdup(const char *str, const char *file, int lineno, const char *func),
+	_ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+AST_INLINE_API(
+char *_ast_strdup(const char *str, const char *file, int lineno, const char *func),
 {
 	char *newstr = NULL;
 
@@ -370,12 +362,10 @@
   The arguments and return value are the same as strndup()
 */
 #define ast_strndup(str, len) \
-	({ \
-		(_ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \
-	})
-
-AST_INLINE_API(
-void *_ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),
+	_ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
+
+AST_INLINE_API(
+char *_ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),
 {
 	char *newstr = NULL;
 



More information about the svn-commits mailing list