summaryrefslogtreecommitdiff
path: root/main/strcompat.c
diff options
context:
space:
mode:
authorKinsey Moore <kmoore@digium.com>2012-07-31 20:21:43 +0000
committerKinsey Moore <kmoore@digium.com>2012-07-31 20:21:43 +0000
commit9b16c8b0f6c3b6310e303411421bfcb16b26c3c4 (patch)
tree273c31a834a21bd2239ec6b83cd35c602ea25d26 /main/strcompat.c
parent6c23a60f802e7708389b1a6463a40dc0500512bd (diff)
Clean up and ensure proper usage of alloca()
This replaces all calls to alloca() with ast_alloca() which calls gcc's __builtin_alloca() to avoid BSD semantics and removes all NULL checks on memory allocated via ast_alloca() and ast_strdupa(). (closes issue ASTERISK-20125) Review: https://reviewboard.asterisk.org/r/2032/ Patch-by: Walter Doekes (wdoekes) ........ Merged revisions 370642 from http://svn.asterisk.org/svn/asterisk/branches/1.8 ........ Merged revisions 370643 from http://svn.asterisk.org/svn/asterisk/branches/10 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@370655 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/strcompat.c')
-rw-r--r--main/strcompat.c32
1 files changed, 14 insertions, 18 deletions
diff --git a/main/strcompat.c b/main/strcompat.c
index 03c304144..dcd84d876 100644
--- a/main/strcompat.c
+++ b/main/strcompat.c
@@ -68,12 +68,12 @@ int setenv(const char *name, const char *value, int overwrite)
unsigned char *buf;
int buflen;
- buflen = strlen(name) + strlen(value) + 2;
- buf = alloca(buflen);
-
if (!overwrite && getenv(name))
return 0;
+ buflen = strlen(name) + strlen(value) + 2;
+ buf = ast_alloca(buflen);
+
snprintf(buf, buflen, "%s=%s", name, value);
return putenv(buf);
@@ -105,23 +105,19 @@ static char *upper(const char *orig, char *buf, int bufsize)
char *strcasestr(const char *haystack, const char *needle)
{
char *u1, *u2;
+ char *offset;
int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1;
- u1 = alloca(u1len);
- u2 = alloca(u2len);
- if (u1 && u2) {
- char *offset;
- if (u2len > u1len) {
- /* Needle bigger than haystack */
- return NULL;
- }
- offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
- if (offset) {
- /* Return the offset into the original string */
- return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
- } else {
- return NULL;
- }
+ if (u2len > u1len) {
+ /* Needle bigger than haystack */
+ return NULL;
+ }
+ u1 = ast_alloca(u1len);
+ u2 = ast_alloca(u2len);
+ offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len));
+ if (offset) {
+ /* Return the offset into the original string */
+ return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1)));
} else {
return NULL;
}