summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--misc/win32/start.c47
1 files changed, 26 insertions, 21 deletions
diff --git a/misc/win32/start.c b/misc/win32/start.c
index 22820e8e5..eb77d4a4d 100644
--- a/misc/win32/start.c
+++ b/misc/win32/start.c
@@ -45,7 +45,9 @@ static int warrsztoastr(const char * const * *retarr,
apr_size_t totlen;
apr_size_t newlen;
apr_size_t wsize;
- char **newarr;
+ char **env;
+ char *pstrs;
+ char *strs;
int arg;
if (args < 0) {
@@ -55,37 +57,40 @@ static int warrsztoastr(const char * const * *retarr,
}
wsize = 1 + wch - arrsz;
- newarr = apr_malloc_dbg((args + 1) * sizeof(char *),
- __FILE__, __LINE__);
-
- /* This is a safe max allocation, we will realloc after
- * processing and return the excess to the free store.
+ /* This is a safe max allocation, we will alloc each
+ * string exactly after processing and return this
+ * temporary buffer to the free store.
* 3 ucs bytes hold any single wchar_t value (16 bits)
* 4 ucs bytes will hold a wchar_t pair value (20 bits)
*/
newlen = totlen = wsize * 3 + 1;
- newarr[0] = apr_malloc_dbg(newlen * sizeof(char),
- __FILE__, __LINE__);
+ pstrs = strs = apr_malloc_dbg(newlen * sizeof(char),
+ __FILE__, __LINE__);
- (void)apr_conv_ucs2_to_utf8(arrsz, &wsize,
- newarr[0], &newlen);
+ (void)apr_conv_ucs2_to_utf8(arrsz, &wsize, strs, &newlen);
assert(newlen && !wsize);
- /* Return to the free store if the heap realloc is the least bit optimized
- */
- newarr[0] = apr_realloc_dbg(newarr[0], totlen - newlen,
- __FILE__, __LINE__);
- for (arg = 1; arg < args; ++arg) {
- newarr[arg] = newarr[arg - 1] + 2;
- while (*(newarr[arg]++)) {
- /* continue */;
- }
+ *retarr = env = apr_malloc_dbg((args + 1) * sizeof(char*),
+ __FILE__, __LINE__);
+ for (arg = 0; arg < args; ++arg) {
+ char* p = pstrs;
+ int len = 0;
+ while (*p++)
+ ++len;
+ len += 1;
+
+ *env = apr_malloc_dbg(len * sizeof(char),
+ __FILE__, __LINE__);
+ memcpy(*env, pstrs, len * sizeof(char));
+
+ pstrs += len;
+ ++env;
}
- newarr[arg] = NULL;
+ *env = NULL;
+ free(strs);
- *retarr = newarr;
return args;
}
#endif