From 092a8106b2777d4b7ebd889554e9a6dba9a46a24 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 18 Dec 2012 15:12:53 +0000 Subject: [PATCH] enhanced env not to crash if environ is pointing to null --- qse/cmd/awk/awk.c | 2 +- qse/include/qse/types.h | 8 +-- qse/lib/cmn/env.c | 106 +++++++++++++++++++++++----------------- qse/lib/cmn/pio.c | 6 ++- qse/lib/cmn/stdio.c | 10 ++-- 5 files changed, 78 insertions(+), 54 deletions(-) diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index 1d5395df..c0063fd5 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -25,11 +25,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include diff --git a/qse/include/qse/types.h b/qse/include/qse/types.h index 87995c43..462176c9 100644 --- a/qse/include/qse/types.h +++ b/qse/include/qse/types.h @@ -509,9 +509,11 @@ typedef int qse_mcint_t; * The qse_wcint_t type defines a type that can hold a qse_wchar_t value and * #QSE_WCHAR_EOF. */ -#if defined(__cplusplus) && \ - (!(defined(_MSC_VER) || defined(_SCO_DS)) || \ - (defined(_MSC_VER) && defined(_NATIVE_WCHAR_T_DEFINED))) +#if defined(__cplusplus) && !( \ + (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)) || \ + (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) || \ + defined(_SCO_DS) \ + ) /* C++ */ typedef wchar_t qse_wchar_t; diff --git a/qse/lib/cmn/env.c b/qse/lib/cmn/env.c index e8f67186..97c6bd44 100644 --- a/qse/lib/cmn/env.c +++ b/qse/lib/cmn/env.c @@ -421,40 +421,46 @@ static qse_wchar_t* get_env (qse_env_t* env, const qse_wchar_t* name, int* free) extern qse_wchar_t** _wenviron; qse_wchar_t** p = _wenviron; - while (*p) + if (p) { - qse_wchar_t* eq; - eq = qse_wcsbeg (*p, name); - if (eq && *eq == QSE_WT('=')) + while (*p) { - *free = 0; - return eq + 1; + qse_wchar_t* eq; + eq = qse_wcsbeg (*p, name); + if (eq && *eq == QSE_WT('=')) + { + *free = 0; + return eq + 1; + } + p++; } - p++; } */ extern char** environ; qse_mchar_t** p = environ; - while (*p) + if (p) { - qse_wchar_t* dup; - qse_wchar_t* eq; - - dup = qse_mbstowcsdup (*p, env->mmgr); /* TODO: ignroe mbwcerr */ - if (dup == QSE_NULL) return QSE_NULL; - - eq = qse_wcsbeg (dup, name); - if (eq && *eq == QSE_WT('=')) + while (*p) { - *free = 1; - return eq + 1; + qse_wchar_t* dup; + qse_wchar_t* eq; + + dup = qse_mbstowcsdup (*p, env->mmgr); /* TODO: ignroe mbwcerr */ + if (dup == QSE_NULL) return QSE_NULL; + + eq = qse_wcsbeg (dup, name); + if (eq && *eq == QSE_WT('=')) + { + *free = 1; + return eq + 1; + } + + QSE_MMGR_FREE (env->mmgr, dup); + + p++; } - - QSE_MMGR_FREE (env->mmgr, dup); - - p++; } return 0; @@ -467,16 +473,19 @@ static qse_mchar_t* get_env (qse_env_t* env, const qse_mchar_t* name, int* free) extern char** environ; qse_mchar_t** p = environ; - while (*p) + if (p) { - qse_mchar_t* eq; - eq = qse_mbsbeg (*p, name); - if (eq && *eq == QSE_MT('=')) + while (*p) { - *free = 0; - return eq + 1; + qse_mchar_t* eq; + eq = qse_mbsbeg (*p, name); + if (eq && *eq == QSE_MT('=')) + { + *free = 0; + return eq + 1; + } + p++; } - p++; } return 0; @@ -590,28 +599,34 @@ done: extern qse_wchar_t** _wenviron; qse_wchar_t** p = _wenviron; - while (*p) + if (p) { - if (add_envstrw (env, *p) <= -1) return -1; - p++; + while (*p) + { + if (add_envstrw (env, *p) <= -1) return -1; + p++; + } } */ extern char** environ; qse_mchar_t** p = environ; - while (*p) + if (p) { - qse_wchar_t* dup; - int n; + while (*p) + { + qse_wchar_t* dup; + int n; - dup = qse_mbstowcsdup (*p, env->mmgr); /* TODO: ignroe mbwcerr */ - if (dup == QSE_NULL) return -1; - n = add_envstrw (env, dup); - QSE_MMGR_FREE (env->mmgr, dup); - if (n <= -1) return -1; + dup = qse_mbstowcsdup (*p, env->mmgr); /* TODO: ignroe mbwcerr */ + if (dup == QSE_NULL) return -1; + n = add_envstrw (env, dup); + QSE_MMGR_FREE (env->mmgr, dup); + if (n <= -1) return -1; - p++; + p++; + } } @@ -621,10 +636,13 @@ done: extern char** environ; qse_mchar_t** p = environ; - while (*p) + if (p) { - if (add_envstrm (env, *p) <= -1) return -1; - p++; + while (*p) + { + if (add_envstrm (env, *p) <= -1) return -1; + p++; + } } return 0; diff --git a/qse/lib/cmn/pio.c b/qse/lib/cmn/pio.c index b4b87e3b..48d6ea74 100644 --- a/qse/lib/cmn/pio.c +++ b/qse/lib/cmn/pio.c @@ -949,17 +949,19 @@ create_process: (flags & QSE_PIO_ERRTONUL)) { ULONG action_taken; + /* LONGLONG zero; zero.ulLo = 0; zero.ulHi = 0; + */ /* TODO: selective between DosOpenL and DosOpen */ - rc = DosOpenL ( + rc = DosOpen /*DosOpenL*/ ( QSE_MT("NUL"), &os2devnul, &action_taken, - zero, + 0, /*zero,*/ FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW, OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE, diff --git a/qse/lib/cmn/stdio.c b/qse/lib/cmn/stdio.c index 2eafe7ed..296e9fb6 100644 --- a/qse/lib/cmn/stdio.c +++ b/qse/lib/cmn/stdio.c @@ -36,7 +36,9 @@ static qse_char_t* __adjust_format (const qse_char_t* format); int qse_vfprintf (QSE_FILE *stream, const qse_char_t* fmt, va_list ap) { int n; - qse_char_t* nf = __adjust_format (fmt); + qse_char_t* nf; + + nf = __adjust_format (fmt); if (nf == NULL) return -1; #if defined(QSE_CHAR_IS_MCHAR) @@ -70,7 +72,7 @@ int qse_printf (const qse_char_t* fmt, ...) va_list ap; va_start (ap, fmt); - n = qse_vprintf (fmt, ap); + n = qse_vfprintf (QSE_STDOUT, fmt, ap); va_end (ap); return n; } @@ -82,13 +84,13 @@ int qse_vsprintf (qse_char_t* buf, qse_size_t size, const qse_char_t* fmt, va_li if (nf == NULL) return -1; #if defined(QSE_CHAR_IS_MCHAR) - #if defined(_WIN32) && !defined(__WATCOMC__) + #if defined(_MSC_VER) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) n = _vsnprintf (buf, size, nf, ap); #else n = vsnprintf (buf, size, nf, ap); #endif #else - #if defined(_WIN32) && !defined(__WATCOMC__) + #if defined(_MSC_VER) || (defined(__WATCOMC__) && (__WATCOMC__ < 1200)) n = _vsnwprintf (buf, size, nf, ap); #else n = vswprintf (buf, size, nf, ap);