From b96113159bdf9be943c99d3241e1e5e5e9f33424 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 2 May 2017 01:14:32 +0000 Subject: [PATCH] fixed the potential overflow issue that would never happen in various binary serach functions --- qse/lib/awk/Makefile.in | 14 +++++++++----- qse/lib/awk/mod-dir.c | 4 ++-- qse/lib/awk/mod-math.c | 4 ++-- qse/lib/awk/mod-mpi.c | 4 ++-- qse/lib/awk/mod-sed.c | 2 +- qse/lib/awk/mod-str.c | 39 +++++++++++++++++++-------------------- qse/lib/awk/mod-sys.c | 4 ++-- qse/lib/awk/mod-uci.c | 2 +- qse/lib/awk/parse.c | 3 ++- qse/lib/cmn/Makefile.in | 14 +++++++++----- qse/lib/cmn/chr.c | 12 ++++++++---- qse/lib/cmn/cp949.h | 6 ++++-- qse/lib/cmn/cp950.h | 6 ++++-- qse/lib/xli/Makefile.in | 14 +++++++++----- qse/lib/xli/read.c | 3 ++- 15 files changed, 76 insertions(+), 55 deletions(-) diff --git a/qse/lib/awk/Makefile.in b/qse/lib/awk/Makefile.in index e08c3ba6..f4054100 100644 --- a/qse/lib/awk/Makefile.in +++ b/qse/lib/awk/Makefile.in @@ -118,11 +118,11 @@ subdir = lib/awk ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \ $(top_srcdir)/m4/ax_cxx_namespace.m4 \ - $(top_srcdir)/m4/ax_numval.m4 $(top_srcdir)/m4/ax_pthread.m4 \ - $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltargz.m4 \ - $(top_srcdir)/m4/ltdl.m4 $(top_srcdir)/m4/ltoptions.m4 \ - $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ - $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/m4/ax_lib_mysql.m4 $(top_srcdir)/m4/ax_numval.m4 \ + $(top_srcdir)/m4/ax_pthread.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltargz.m4 $(top_srcdir)/m4/ltdl.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ $(top_srcdir)/m4/lx_find_mpi.m4 $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) @@ -445,6 +445,10 @@ MKDIR_P = @MKDIR_P@ MPICC = @MPICC@ MPI_CFLAGS = @MPI_CFLAGS@ MPI_CLDFLAGS = @MPI_CLDFLAGS@ +MYSQL_CFLAGS = @MYSQL_CFLAGS@ +MYSQL_CONFIG = @MYSQL_CONFIG@ +MYSQL_LDFLAGS = @MYSQL_LDFLAGS@ +MYSQL_VERSION = @MYSQL_VERSION@ NM = @NM@ NMEDIT = @NMEDIT@ OBJDUMP = @OBJDUMP@ diff --git a/qse/lib/awk/mod-dir.c b/qse/lib/awk/mod-dir.c index edeec6a3..a7ea33f7 100644 --- a/qse/lib/awk/mod-dir.c +++ b/qse/lib/awk/mod-dir.c @@ -570,7 +570,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; @@ -586,7 +586,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs left = 0; right = QSE_COUNTOF(inttab) - 1; while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (inttab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/mod-math.c b/qse/lib/awk/mod-math.c index 85724a53..7d19918b 100644 --- a/qse/lib/awk/mod-math.c +++ b/qse/lib/awk/mod-math.c @@ -619,7 +619,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; @@ -636,7 +636,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs left = 0; right = QSE_COUNTOF(inttab) - 1; while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (inttab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/mod-mpi.c b/qse/lib/awk/mod-mpi.c index d62a6e1a..697ca5b6 100644 --- a/qse/lib/awk/mod-mpi.c +++ b/qse/lib/awk/mod-mpi.c @@ -206,7 +206,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; @@ -222,7 +222,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs left = 0; right = QSE_COUNTOF(inttab) - 1; while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (inttab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/mod-sed.c b/qse/lib/awk/mod-sed.c index 10d60fb6..4ddbbbc3 100644 --- a/qse/lib/awk/mod-sed.c +++ b/qse/lib/awk/mod-sed.c @@ -238,7 +238,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/mod-str.c b/qse/lib/awk/mod-str.c index a232ed37..a074600d 100644 --- a/qse/lib/awk/mod-str.c +++ b/qse/lib/awk/mod-str.c @@ -43,14 +43,13 @@ static int fnc_normspace (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) a0 = qse_awk_rtx_getarg(rtx, 0); path.ptr = qse_awk_rtx_getvalstr (rtx, a0, &path.len); - if (path.ptr) - { - path.len = qse_strxpac (path.ptr, path.len); - retv = qse_awk_rtx_makestrval (rtx, path.ptr, path.len); - qse_awk_rtx_freevalstr (rtx, a0, path.ptr); - if (!retv) return -1; - qse_awk_rtx_setretval (rtx, retv); - } + if (!path.ptr) return -1; + + path.len = qse_strxpac (path.ptr, path.len); + retv = qse_awk_rtx_makestrval (rtx, path.ptr, path.len); + qse_awk_rtx_freevalstr (rtx, a0, path.ptr); + if (!retv) return -1; + qse_awk_rtx_setretval (rtx, retv); return 0; } @@ -65,14 +64,13 @@ static int trim (qse_awk_rtx_t* rtx, int flags) a0 = qse_awk_rtx_getarg(rtx, 0); path.ptr = qse_awk_rtx_getvalstr (rtx, a0, &path.len); - if (path.ptr) - { - npath = qse_strxtrmx (path.ptr, &path.len, flags); - retv = qse_awk_rtx_makestrval (rtx, npath, path.len); - qse_awk_rtx_freevalstr (rtx, a0, path.ptr); - if (!retv) return -1; - qse_awk_rtx_setretval (rtx, retv); - } + if (!path.ptr) return -1; + + npath = qse_strxtrmx (path.ptr, &path.len, flags); + retv = qse_awk_rtx_makestrval (rtx, npath, path.len); + qse_awk_rtx_freevalstr (rtx, a0, path.ptr); + if (!retv) return -1; + qse_awk_rtx_setretval (rtx, retv); return 0; } @@ -100,7 +98,7 @@ static int is_class (qse_awk_rtx_t* rtx, qse_ctype_t ctype) a0 = qse_awk_rtx_getarg (rtx, 0); str0 = qse_awk_rtx_getvalstr (rtx, a0, &len0); - if (str0 == QSE_NULL) return -1; + if (!str0) return -1; if (len0 <= 0) tmp = 0; else @@ -216,6 +214,8 @@ static int fnc_value (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) static int fnc_tonum (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { + /* str::tonum (value) */ + qse_awk_val_t* retv; qse_awk_val_t* a0; qse_awk_int_t lv; @@ -251,7 +251,6 @@ struct fnctab_t qse_awk_mod_sym_fnc_t info; }; - #define A_MAX QSE_TYPE_MAX(int) static fnctab_t fnctab[] = @@ -297,7 +296,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; @@ -314,7 +313,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs left = 0; right = QSE_COUNTOF(inttab) - 1; while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (inttab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/mod-sys.c b/qse/lib/awk/mod-sys.c index 6f1a3992..77fb3e39 100644 --- a/qse/lib/awk/mod-sys.c +++ b/qse/lib/awk/mod-sys.c @@ -966,7 +966,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; @@ -982,7 +982,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs left = 0; right = QSE_COUNTOF(inttab) - 1; while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (inttab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/mod-uci.c b/qse/lib/awk/mod-uci.c index f06efe80..7c35e24b 100644 --- a/qse/lib/awk/mod-uci.c +++ b/qse/lib/awk/mod-uci.c @@ -1342,7 +1342,7 @@ static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qs while (left <= right) { - mid = (left + right) / 2; + mid = left + (right - left) / 2; n = qse_strcmp (fnctab[mid].name, name); if (n > 0) right = mid - 1; diff --git a/qse/lib/awk/parse.c b/qse/lib/awk/parse.c index 6e86757f..076c03e2 100644 --- a/qse/lib/awk/parse.c +++ b/qse/lib/awk/parse.c @@ -6322,7 +6322,8 @@ static int classify_ident (qse_awk_t* awk, const qse_cstr_t* name) int n; kwent_t* kwp; - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; kwp = &kwtab[mid]; n = qse_strxncmp (kwp->name.ptr, kwp->name.len, name->ptr, name->len); diff --git a/qse/lib/cmn/Makefile.in b/qse/lib/cmn/Makefile.in index b2814e13..04013ab4 100644 --- a/qse/lib/cmn/Makefile.in +++ b/qse/lib/cmn/Makefile.in @@ -96,11 +96,11 @@ subdir = lib/cmn ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \ $(top_srcdir)/m4/ax_cxx_namespace.m4 \ - $(top_srcdir)/m4/ax_numval.m4 $(top_srcdir)/m4/ax_pthread.m4 \ - $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltargz.m4 \ - $(top_srcdir)/m4/ltdl.m4 $(top_srcdir)/m4/ltoptions.m4 \ - $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ - $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/m4/ax_lib_mysql.m4 $(top_srcdir)/m4/ax_numval.m4 \ + $(top_srcdir)/m4/ax_pthread.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltargz.m4 $(top_srcdir)/m4/ltdl.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ $(top_srcdir)/m4/lx_find_mpi.m4 $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) @@ -341,6 +341,10 @@ MKDIR_P = @MKDIR_P@ MPICC = @MPICC@ MPI_CFLAGS = @MPI_CFLAGS@ MPI_CLDFLAGS = @MPI_CLDFLAGS@ +MYSQL_CFLAGS = @MYSQL_CFLAGS@ +MYSQL_CONFIG = @MYSQL_CONFIG@ +MYSQL_LDFLAGS = @MYSQL_LDFLAGS@ +MYSQL_VERSION = @MYSQL_VERSION@ NM = @NM@ NMEDIT = @NMEDIT@ OBJDUMP = @OBJDUMP@ diff --git a/qse/lib/cmn/chr.c b/qse/lib/cmn/chr.c index e5501c75..3ee73c4d 100644 --- a/qse/lib/cmn/chr.c +++ b/qse/lib/cmn/chr.c @@ -371,7 +371,8 @@ int qse_wcstoctype (const qse_wchar_t* name, qse_wctype_t* id) int n; struct wtab_t* kwp; - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; kwp = &wtab[mid]; n = qse_wcscmp (name, wtab->name); @@ -402,7 +403,8 @@ int qse_wcsntoctype (const qse_wchar_t* name, qse_size_t len, qse_wctype_t* id) int n; struct wtab_t* kwp; - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; kwp = &wtab[mid]; n = qse_wcsxcmp (name, len, kwp->name); @@ -453,7 +455,8 @@ int qse_mbstoctype (const qse_mchar_t* name, qse_mctype_t* id) int n; struct mtab_t* kwp; - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; kwp = &mtab[mid]; n = qse_mbscmp (name, mtab->name); @@ -484,7 +487,8 @@ int qse_mbsntoctype (const qse_mchar_t* name, qse_size_t len, qse_mctype_t* id) int n; struct mtab_t* kwp; - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; kwp = &mtab[mid]; n = qse_mbsxcmp (name, len, kwp->name); diff --git a/qse/lib/cmn/cp949.h b/qse/lib/cmn/cp949.h index 5a374488..c88fdf0b 100644 --- a/qse/lib/cmn/cp949.h +++ b/qse/lib/cmn/cp949.h @@ -22705,7 +22705,8 @@ static qse_uint16_t mbtowc (qse_uint16_t c) int left = 0, right = QSE_COUNTOF(mbtowc_range) - 1, mid; while (left <= right) { - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; if (c >= mbtowc_range[mid].first && c <= mbtowc_range[mid].last) return mbtowc_range[mid].seg[c - mbtowc_range[mid].first]; else if (c > mbtowc_range[mid].last) @@ -56976,7 +56977,8 @@ static qse_uint16_t wctomb (qse_uint16_t c) int left = 0, right = QSE_COUNTOF(wctomb_range) - 1, mid; while (left <= right) { - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; if (c >= wctomb_range[mid].first && c <= wctomb_range[mid].last) return wctomb_range[mid].seg[c - wctomb_range[mid].first]; else if (c > wctomb_range[mid].last) diff --git a/qse/lib/cmn/cp950.h b/qse/lib/cmn/cp950.h index 7b1f25f8..e47bfccb 100644 --- a/qse/lib/cmn/cp950.h +++ b/qse/lib/cmn/cp950.h @@ -16831,7 +16831,8 @@ static qse_uint16_t mbtowc (qse_uint16_t c) int left = 0, right = QSE_COUNTOF(mbtowc_range) - 1, mid; while (left <= right) { - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; if (c >= mbtowc_range[mid].first && c <= mbtowc_range[mid].last) return mbtowc_range[mid].seg[c - mbtowc_range[mid].first]; else if (c > mbtowc_range[mid].last) @@ -38118,7 +38119,8 @@ static qse_uint16_t wctomb (qse_uint16_t c) int left = 0, right = QSE_COUNTOF(wctomb_range) - 1, mid; while (left <= right) { - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; if (c >= wctomb_range[mid].first && c <= wctomb_range[mid].last) return wctomb_range[mid].seg[c - wctomb_range[mid].first]; else if (c > wctomb_range[mid].last) diff --git a/qse/lib/xli/Makefile.in b/qse/lib/xli/Makefile.in index 893ace49..b34dc5ae 100644 --- a/qse/lib/xli/Makefile.in +++ b/qse/lib/xli/Makefile.in @@ -92,11 +92,11 @@ subdir = lib/xli ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \ $(top_srcdir)/m4/ax_cxx_namespace.m4 \ - $(top_srcdir)/m4/ax_numval.m4 $(top_srcdir)/m4/ax_pthread.m4 \ - $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltargz.m4 \ - $(top_srcdir)/m4/ltdl.m4 $(top_srcdir)/m4/ltoptions.m4 \ - $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ - $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/m4/ax_lib_mysql.m4 $(top_srcdir)/m4/ax_numval.m4 \ + $(top_srcdir)/m4/ax_pthread.m4 $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltargz.m4 $(top_srcdir)/m4/ltdl.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ $(top_srcdir)/m4/lx_find_mpi.m4 $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) @@ -276,6 +276,10 @@ MKDIR_P = @MKDIR_P@ MPICC = @MPICC@ MPI_CFLAGS = @MPI_CFLAGS@ MPI_CLDFLAGS = @MPI_CLDFLAGS@ +MYSQL_CFLAGS = @MYSQL_CFLAGS@ +MYSQL_CONFIG = @MYSQL_CONFIG@ +MYSQL_LDFLAGS = @MYSQL_LDFLAGS@ +MYSQL_VERSION = @MYSQL_VERSION@ NM = @NM@ NMEDIT = @NMEDIT@ OBJDUMP = @OBJDUMP@ diff --git a/qse/lib/xli/read.c b/qse/lib/xli/read.c index 82cae865..e012720a 100644 --- a/qse/lib/xli/read.c +++ b/qse/lib/xli/read.c @@ -267,7 +267,8 @@ static int classify_ident (qse_xli_t* xli, const qse_cstr_t* name) int n; kwent_t* kwp; - mid = (left + right) / 2; + /*mid = (left + right) / 2;*/ + mid = left + (right - left) / 2; kwp = &kwtab[mid]; n = qse_strxncmp (kwp->name.ptr, kwp->name.len, name->ptr, name->len);