- move ast_strdupa from channel.h to utils.h
- attempt to log an error message if the __builtin_alloca inside of ast_strdupa fails. - document the fact that it is known and intended behavior for ast_strdupa to cause Asterisk to crash if the alloca fails - use __builtin_expect when checking for allocation failure in all of the allocation wrappers New Janitor Project! Anywhere that we check for a successful allocation after a call to ast_strdupa is unnecessary and should be removed. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8356 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
parent
cb8bd57a21
commit
1946020208
2 changed files with 31 additions and 16 deletions
|
@ -1174,17 +1174,6 @@ static inline int ast_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds,
|
|||
#endif
|
||||
}
|
||||
|
||||
#if !defined(ast_strdupa) && defined(__GNUC__)
|
||||
# define ast_strdupa(s) \
|
||||
(__extension__ \
|
||||
({ \
|
||||
__const char *__old = (s); \
|
||||
size_t __len = strlen (__old) + 1; \
|
||||
char *__new = (char *) __builtin_alloca (__len); \
|
||||
(char *) memcpy (__new, __old, __len); \
|
||||
}))
|
||||
#endif
|
||||
|
||||
#ifdef DO_CRASH
|
||||
#define CRASH do { fprintf(stderr, "!! Forcing immediate crash a-la abort !!\n"); *((int *)0) = 0; } while(0)
|
||||
#else
|
||||
|
|
|
@ -261,7 +261,7 @@ void *_ast_malloc(size_t len, const char *file, int lineno, const char *func),
|
|||
|
||||
p = malloc(len);
|
||||
|
||||
if (!p)
|
||||
if (__builtin_expect(!p, 0))
|
||||
ast_log(LOG_ERROR, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int)len, func, lineno, file);
|
||||
|
||||
return p;
|
||||
|
@ -286,7 +286,7 @@ void *_ast_calloc(size_t num, size_t len, const char *file, int lineno, const ch
|
|||
|
||||
p = calloc(num, len);
|
||||
|
||||
if (!p)
|
||||
if (__builtin_expect(!p, 0))
|
||||
ast_log(LOG_ERROR, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int)len, func, lineno, file);
|
||||
|
||||
return p;
|
||||
|
@ -311,7 +311,7 @@ void *_ast_realloc(void *p, size_t len, const char *file, int lineno, const char
|
|||
|
||||
newp = realloc(p, len);
|
||||
|
||||
if (!newp)
|
||||
if (__builtin_expect(!newp, 0))
|
||||
ast_log(LOG_ERROR, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int)len, func, lineno, file);
|
||||
|
||||
return newp;
|
||||
|
@ -341,7 +341,7 @@ char *_ast_strdup(const char *str, const char *file, int lineno, const char *fun
|
|||
if (str) {
|
||||
newstr = strdup(str);
|
||||
|
||||
if (!newstr)
|
||||
if (__builtin_expect(!newstr, 0))
|
||||
ast_log(LOG_ERROR, "Memory Allocation Failure - Could not duplicate '%s' in function %s at line %d of %s\n", str, func, lineno, file);
|
||||
}
|
||||
|
||||
|
@ -372,7 +372,7 @@ char *_ast_strndup(const char *str, size_t len, const char *file, int lineno, co
|
|||
if (str) {
|
||||
newstr = strndup(str, len);
|
||||
|
||||
if (!newstr)
|
||||
if (__builtin_expect(!newstr, 0))
|
||||
ast_log(LOG_ERROR, "Memory Allocation Failure - Could not duplicate '%d' bytes of '%s' in function %s at line %d of %s\n", (int)len, str, func, lineno, file);
|
||||
}
|
||||
|
||||
|
@ -380,4 +380,30 @@ char *_ast_strndup(const char *str, size_t len, const char *file, int lineno, co
|
|||
}
|
||||
)
|
||||
|
||||
#if !defined(ast_strdupa) && defined(__GNUC__)
|
||||
/*!
|
||||
\brief duplicate a string in memory from the stack
|
||||
\param s The string to duplicate
|
||||
|
||||
This macro will duplicate the given string. It returns a pointer to the stack
|
||||
allocatted memory for the new string.
|
||||
|
||||
\note If this function fails to allocate memory on the stack, we do not make
|
||||
any effort to prevent Asterisk from crashing. We will attempt to log an
|
||||
error message, but Asterisk will crash shortly after.
|
||||
*/
|
||||
#define ast_strdupa(s) \
|
||||
(__extension__ \
|
||||
({ \
|
||||
const char *__old = (s); \
|
||||
size_t __len = strlen(__old) + 1; \
|
||||
char *__new = __builtin_alloca(__len); \
|
||||
if (__builtin_expect(!__new, 0)) \
|
||||
ast_log(LOG_ERROR, "Stack Allocation Error in" \
|
||||
"function '%s' at line '%d' of '%s'!\n", \
|
||||
__PRETTY_FUNCTION__, __LINE__, __FILE__); \
|
||||
(char *) memcpy (__new, __old, __len); \
|
||||
}))
|
||||
#endif
|
||||
|
||||
#endif /* _ASTERISK_UTILS_H */
|
||||
|
|
Loading…
Reference in a new issue