From b86b5a8fe71a39d6fc69bc635c6b1d920497a87f Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Fri, 8 Dec 2006 05:03:58 +0000 Subject: [PATCH] 2006-12-08 05:55 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/bin/hb-func.sh * harbour/bin/hb-mkslib.sh * harbour/bin/postinst.sh * harbour/config/bsd/gcc.cf * harbour/config/darwin/gcc.cf * harbour/config/dos/owatcom.cf * harbour/config/hpux/gcc.cf * harbour/config/linux/gcc.cf * harbour/config/linux/owatcom.cf * harbour/config/os2/gcc.cf * harbour/config/sunos/gcc.cf * harbour/config/w32/watcom.cf * harbour/contrib/rdd_ads/ads1.c * harbour/contrib/rdd_ads/adsfunc.c * harbour/include/hbapifs.h * harbour/include/hbexprc.c * harbour/include/hbsetup.h * harbour/source/common/expropt2.c * harbour/source/common/hbdate.c * harbour/source/common/hbgete.c * harbour/source/common/hbstr.c * harbour/source/common/hbver.c * harbour/source/common/hbverdsp.c * harbour/source/compiler/cmdcheck.c * harbour/source/compiler/fixflex.c * harbour/source/compiler/gencobj.c * harbour/source/compiler/genobj32.c * harbour/source/compiler/harbour.c * harbour/source/compiler/harbour.l * harbour/source/compiler/harbour.y * harbour/source/compiler/harbour.yyc * harbour/source/compiler/hbfix.c * harbour/source/compiler/hbfunchk.c * harbour/source/pp/ppcore.c * harbour/source/pp/ppgen.c * harbour/source/pp/pplib.c * harbour/source/rdd/dbcmd.c * harbour/source/rdd/workarea.c * harbour/source/rdd/dbfcdx/dbfcdx1.c * harbour/source/rdd/dbffpt/dbffpt1.c * harbour/source/rdd/hsx/hsx.c * harbour/source/rtl/accept.c * harbour/source/rtl/ampm.c * harbour/source/rtl/console.c * harbour/source/rtl/defpath.c * harbour/source/rtl/errorint.c * harbour/source/rtl/filesys.c * harbour/source/rtl/fkmax.c * harbour/source/rtl/fstemp.c * harbour/source/rtl/hbffind.c * harbour/source/rtl/hbgtcore.c * harbour/source/rtl/langapi.c * harbour/source/rtl/seconds.c * harbour/source/rtl/set.c * harbour/source/rtl/tobject.prg * harbour/source/rtl/transfrm.c * harbour/source/rtl/gtcrs/chrmap.c * harbour/source/rtl/gtcrs/gtcrs.c * harbour/source/rtl/gtcrs/gtcrs.h * harbour/source/rtl/gtpca/gtpca.c * harbour/source/rtl/gtwvt/gtwvt.c * harbour/source/rtl/gtxwc/gtxwc.c * harbour/source/vm/classes.c * harbour/source/vm/cmdarg.c * harbour/source/vm/debug.c * harbour/source/vm/estack.c * harbour/source/vm/fm.c * harbour/source/vm/hvm.c * harbour/source/vm/macro.c * harbour/source/vm/proc.c * harbour/source/vm/runner.c * harbour/utils/hbmake/hbmlang.c * harbour/utils/hbpp/hbpp.c * harbour/utils/hbpp/hbppcomp.c * harbour/utils/hbpp/hbppcore.c * harbour/utils/hbpp/hbpplib.c * harbour/utils/hbpp/pragma.c * harbour/utils/hbver/hbverfix.c * general code cleanup: all strcpy() and strcat() replaced hb strn*() and hb_strn*(), sprintf() by snprintf(), etc. It fixed some possible buffer overflow but it's also possible that it will exploit some some hidden so far problems but I strongly prefer to know about them ASAP to fix them before final 1.0 release. ! fixed bug I introduce in codeblock falgs which disabled early macro evaluation in codeblocks. ! added missing protection against execution by bison destructor for used CBSTART token on syntax error. % use PP stringify logik added for FLEX support to remove all redundant spaces in stringified for early macro evaluation codeblocks and add spaces between operators if after preprocessing they were removed. Now we are supporting also some code which cannot be compiled by Clipper. % cleaned and improved the speed of printer redirecting in console code. Chen please check if CG still reports problems and if not ubdate TODO/TOFIX or inform me about them. * some cleanups in building process for different *nixes: MacOSX, SunOS, *BSD, etc. * removed not longer necessary (I hope) compiler flag to force default 'char' type as signed. Now Harbour should work with signed and unsigned char without any problems. To other developers: please remember that when you are using 'char' as 8 bytes signed integer then it's a bug. Always explicitly use: 'signed char' in such case. On some platforms it's not possible to set default type for 'char' so if you hardcode that 'char' is signed or unsigned somewhere then code will not be portable. Chen if you will find a while to also rebuild Harbour with BCC and -K switch to make some tests with CG then I'll be thankful. --- harbour/ChangeLog | 108 +++++++++++++++++++++++++++ harbour/bin/hb-func.sh | 2 + harbour/bin/hb-mkslib.sh | 1 + harbour/bin/postinst.sh | 4 +- harbour/config/bsd/gcc.cf | 2 +- harbour/config/darwin/gcc.cf | 3 +- harbour/config/dos/owatcom.cf | 3 +- harbour/config/hpux/gcc.cf | 2 +- harbour/config/linux/gcc.cf | 2 +- harbour/config/linux/owatcom.cf | 3 +- harbour/config/os2/gcc.cf | 2 +- harbour/config/sunos/gcc.cf | 2 +- harbour/config/w32/watcom.cf | 3 +- harbour/contrib/rdd_ads/ads1.c | 108 +++++++++++++-------------- harbour/contrib/rdd_ads/adsfunc.c | 4 +- harbour/include/hbapifs.h | 4 +- harbour/include/hbexprc.c | 4 +- harbour/include/hbsetup.h | 26 ++++++- harbour/source/common/expropt2.c | 10 +-- harbour/source/common/hbdate.c | 4 +- harbour/source/common/hbgete.c | 6 +- harbour/source/common/hbstr.c | 61 ++++++++------- harbour/source/common/hbver.c | 97 ++++++++++++------------ harbour/source/common/hbverdsp.c | 2 +- harbour/source/compiler/cmdcheck.c | 32 ++++---- harbour/source/compiler/fixflex.c | 20 ++--- harbour/source/compiler/gencobj.c | 38 +++++----- harbour/source/compiler/genobj32.c | 10 +-- harbour/source/compiler/harbour.c | 21 ++++-- harbour/source/compiler/harbour.l | 8 +- harbour/source/compiler/harbour.y | 2 +- harbour/source/compiler/harbour.yyc | 2 +- harbour/source/compiler/hbfix.c | 12 +-- harbour/source/compiler/hbfunchk.c | 6 +- harbour/source/pp/ppcore.c | 32 ++++---- harbour/source/pp/ppgen.c | 12 +-- harbour/source/pp/pplib.c | 3 +- harbour/source/rdd/dbcmd.c | 14 ++-- harbour/source/rdd/dbfcdx/dbfcdx1.c | 6 +- harbour/source/rdd/dbffpt/dbffpt1.c | 6 +- harbour/source/rdd/hsx/hsx.c | 2 +- harbour/source/rdd/workarea.c | 2 +- harbour/source/rtl/accept.c | 2 +- harbour/source/rtl/ampm.c | 11 ++- harbour/source/rtl/console.c | 98 +++++++++++++++++-------- harbour/source/rtl/defpath.c | 4 +- harbour/source/rtl/errorint.c | 8 +- harbour/source/rtl/filesys.c | 110 ++++++++++++---------------- harbour/source/rtl/fkmax.c | 3 +- harbour/source/rtl/fstemp.c | 10 +-- harbour/source/rtl/gtcrs/chrmap.c | 4 +- harbour/source/rtl/gtcrs/gtcrs.c | 2 +- harbour/source/rtl/gtcrs/gtcrs.h | 7 +- harbour/source/rtl/gtpca/gtpca.c | 2 +- harbour/source/rtl/gtwvt/gtwvt.c | 4 +- harbour/source/rtl/gtxwc/gtxwc.c | 2 +- harbour/source/rtl/hbffind.c | 24 +++--- harbour/source/rtl/hbgtcore.c | 3 +- harbour/source/rtl/langapi.c | 9 ++- harbour/source/rtl/seconds.c | 10 +-- harbour/source/rtl/set.c | 8 +- harbour/source/rtl/tobject.prg | 16 ++-- harbour/source/rtl/transfrm.c | 8 +- harbour/source/vm/classes.c | 58 ++++++++------- harbour/source/vm/cmdarg.c | 4 +- harbour/source/vm/debug.c | 4 +- harbour/source/vm/estack.c | 32 ++++---- harbour/source/vm/fm.c | 8 +- harbour/source/vm/hvm.c | 7 +- harbour/source/vm/macro.c | 6 +- harbour/source/vm/proc.c | 32 ++++---- harbour/source/vm/runner.c | 1 - harbour/utils/hbmake/hbmlang.c | 3 +- harbour/utils/hbpp/hbpp.c | 3 +- harbour/utils/hbpp/hbppcomp.c | 19 ++--- harbour/utils/hbpp/hbppcore.c | 16 ++-- harbour/utils/hbpp/hbpplib.c | 2 +- harbour/utils/hbpp/pragma.c | 8 +- harbour/utils/hbver/hbverfix.c | 18 ++--- 79 files changed, 705 insertions(+), 552 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index a92e13d327..616b20df33 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,114 @@ 2002-12-01 13:30 UTC+0100 Foo Bar */ +2006-12-08 05:55 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/bin/hb-func.sh + * harbour/bin/hb-mkslib.sh + * harbour/bin/postinst.sh + * harbour/config/bsd/gcc.cf + * harbour/config/darwin/gcc.cf + * harbour/config/dos/owatcom.cf + * harbour/config/hpux/gcc.cf + * harbour/config/linux/gcc.cf + * harbour/config/linux/owatcom.cf + * harbour/config/os2/gcc.cf + * harbour/config/sunos/gcc.cf + * harbour/config/w32/watcom.cf + * harbour/contrib/rdd_ads/ads1.c + * harbour/contrib/rdd_ads/adsfunc.c + * harbour/include/hbapifs.h + * harbour/include/hbexprc.c + * harbour/include/hbsetup.h + * harbour/source/common/expropt2.c + * harbour/source/common/hbdate.c + * harbour/source/common/hbgete.c + * harbour/source/common/hbstr.c + * harbour/source/common/hbver.c + * harbour/source/common/hbverdsp.c + * harbour/source/compiler/cmdcheck.c + * harbour/source/compiler/fixflex.c + * harbour/source/compiler/gencobj.c + * harbour/source/compiler/genobj32.c + * harbour/source/compiler/harbour.c + * harbour/source/compiler/harbour.l + * harbour/source/compiler/harbour.y + * harbour/source/compiler/harbour.yyc + * harbour/source/compiler/hbfix.c + * harbour/source/compiler/hbfunchk.c + * harbour/source/pp/ppcore.c + * harbour/source/pp/ppgen.c + * harbour/source/pp/pplib.c + * harbour/source/rdd/dbcmd.c + * harbour/source/rdd/workarea.c + * harbour/source/rdd/dbfcdx/dbfcdx1.c + * harbour/source/rdd/dbffpt/dbffpt1.c + * harbour/source/rdd/hsx/hsx.c + * harbour/source/rtl/accept.c + * harbour/source/rtl/ampm.c + * harbour/source/rtl/console.c + * harbour/source/rtl/defpath.c + * harbour/source/rtl/errorint.c + * harbour/source/rtl/filesys.c + * harbour/source/rtl/fkmax.c + * harbour/source/rtl/fstemp.c + * harbour/source/rtl/hbffind.c + * harbour/source/rtl/hbgtcore.c + * harbour/source/rtl/langapi.c + * harbour/source/rtl/seconds.c + * harbour/source/rtl/set.c + * harbour/source/rtl/tobject.prg + * harbour/source/rtl/transfrm.c + * harbour/source/rtl/gtcrs/chrmap.c + * harbour/source/rtl/gtcrs/gtcrs.c + * harbour/source/rtl/gtcrs/gtcrs.h + * harbour/source/rtl/gtpca/gtpca.c + * harbour/source/rtl/gtwvt/gtwvt.c + * harbour/source/rtl/gtxwc/gtxwc.c + * harbour/source/vm/classes.c + * harbour/source/vm/cmdarg.c + * harbour/source/vm/debug.c + * harbour/source/vm/estack.c + * harbour/source/vm/fm.c + * harbour/source/vm/hvm.c + * harbour/source/vm/macro.c + * harbour/source/vm/proc.c + * harbour/source/vm/runner.c + * harbour/utils/hbmake/hbmlang.c + * harbour/utils/hbpp/hbpp.c + * harbour/utils/hbpp/hbppcomp.c + * harbour/utils/hbpp/hbppcore.c + * harbour/utils/hbpp/hbpplib.c + * harbour/utils/hbpp/pragma.c + * harbour/utils/hbver/hbverfix.c + * general code cleanup: all strcpy() and strcat() replaced hb strn*() + and hb_strn*(), sprintf() by snprintf(), etc. + It fixed some possible buffer overflow but it's also possible that + it will exploit some some hidden so far problems but I strongly + prefer to know about them ASAP to fix them before final 1.0 release. + ! fixed bug I introduce in codeblock falgs which disabled early macro + evaluation in codeblocks. + ! added missing protection against execution by bison destructor for + used CBSTART token on syntax error. + % use PP stringify logik added for FLEX support to remove all redundant + spaces in stringified for early macro evaluation codeblocks and add + spaces between operators if after preprocessing they were removed. + Now we are supporting also some code which cannot be compiled by + Clipper. + % cleaned and improved the speed of printer redirecting in console code. + Chen please check if CG still reports problems and if not ubdate + TODO/TOFIX or inform me about them. + * some cleanups in building process for different *nixes: MacOSX, SunOS, + *BSD, etc. + * removed not longer necessary (I hope) compiler flag to force default + 'char' type as signed. Now Harbour should work with signed and unsigned + char without any problems. To other developers: please remember that + when you are using 'char' as 8 bytes signed integer then it's a bug. + Always explicitly use: 'signed char' in such case. On some platforms + it's not possible to set default type for 'char' so if you hardcode + that 'char' is signed or unsigned somewhere then code will not be + portable. Chen if you will find a while to also rebuild Harbour with + BCC and -K switch to make some tests with CG then I'll be thankful. + 2006-12-04 19:20 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/source/macro/macro.y * harbour/source/macro/macro.yyc diff --git a/harbour/bin/hb-func.sh b/harbour/bin/hb-func.sh index 71229aa114..9e441c95ec 100644 --- a/harbour/bin/hb-func.sh +++ b/harbour/bin/hb-func.sh @@ -125,6 +125,8 @@ mk_hbtools() if [ "${HB_ARCHITECTURE}" = "sunos" ]; then HB_SYS_LIBS="${HB_SYS_LIBS} -lrt" HB_CRS_LIB="curses" + elif [ -n "${HB_CURSES_VER}" ]; then + HB_CRS_LIB="${HB_CURSES_VER}" elif [ "${HB_NCURSES_194}" = "yes" ]; then HB_CRS_LIB="ncur194" else diff --git a/harbour/bin/hb-mkslib.sh b/harbour/bin/hb-mkslib.sh index d66438c3f3..651cf504b1 100644 --- a/harbour/bin/hb-mkslib.sh +++ b/harbour/bin/hb-mkslib.sh @@ -123,6 +123,7 @@ elif [ "${SLIB_EXT}" = ".dll" ]; then SYSLIBS="${SYSLIBS} -loleaut32 -luuid -lmpr -lwsock32 -lws2_32 -lmapi32" ${CCPREFIX}gcc -shared -o "${FULLNAME}" $OBJLST ${linker_options} ${SYSLIBS} ${HB_DLLIBS} && \ cd "${dir}" && \ + rm -f "${DSTDIR}${FULLNAME}" && \ mv -f "${OTMPDIR}/${FULLNAME}" "${DSTDIR}${FULLNAME}" else #FULLNAME="${BASE}-${VERSION}${SLIB_EXT}" diff --git a/harbour/bin/postinst.sh b/harbour/bin/postinst.sh index 7225861e81..9cdfdab75e 100644 --- a/harbour/bin/postinst.sh +++ b/harbour/bin/postinst.sh @@ -48,7 +48,9 @@ then AR="${CCPREFIX}ar -crs" fi if [ "${HB_ARCHITECTURE}" = "sunos" ]; then - install -m 755 -f "${HB_BIN_INSTALL}" "${hb_root}/bin/hb-mkslib.sh" + rm -f "${HB_BIN_INSTALL}/hb-mkslib" + cp "${hb_root}/bin/hb-mkslib.sh" "${HB_BIN_INSTALL}/hb-mkslib" && \ + chmod 755 "${HB_BIN_INSTALL}/hb-mkslib" elif [ "${HB_ARCHITECTURE}" != "dos" ]; then # Without -c some OSes _move_ the file instead of copying it! install -c -m 755 "${hb_root}/bin/hb-mkslib.sh" "${HB_BIN_INSTALL}/hb-mkslib" diff --git a/harbour/config/bsd/gcc.cf b/harbour/config/bsd/gcc.cf index 49563bdf2e..f57a606b30 100644 --- a/harbour/config/bsd/gcc.cf +++ b/harbour/config/bsd/gcc.cf @@ -13,7 +13,7 @@ CC = gcc CC_IN = -c CC_OUT = -o CPPFLAGS = -I. -I$(HB_INC_COMPILE) -CFLAGS = -Wall -g -DHB_OS_BSD +CFLAGS = -W -Wall -DHB_OS_BSD LD = gcc LD_OUT = -o diff --git a/harbour/config/darwin/gcc.cf b/harbour/config/darwin/gcc.cf index a9e583b678..a6a90e3f8c 100644 --- a/harbour/config/darwin/gcc.cf +++ b/harbour/config/darwin/gcc.cf @@ -19,7 +19,7 @@ CC_OUT = -o CPPFLAGS = -no-cpp-precomp -I. -I$(TOP)include -I$(HB_INC_COMPILE) # -fno-common enables building .dylib files -CFLAGS = -fno-common -fsigned-char -Wall -W +CFLAGS = -fno-common -Wall -W # It's to avoid warning message generated when 'long double' is used # remove it if you have newer compiler version @@ -105,4 +105,5 @@ LIBTOOL = libtool LIBTOOLFLAGS = $(LIBTOOL_USR) AR_RULE = $(LIBTOOL) -static $(LIBTOOLFLAGS) -o $@ $(^F) || $(RM) $@ RANLIB = ranlib + include $(TOP)$(ROOT)config/rules.cf diff --git a/harbour/config/dos/owatcom.cf b/harbour/config/dos/owatcom.cf index 9452847311..d75b513ab6 100644 --- a/harbour/config/dos/owatcom.cf +++ b/harbour/config/dos/owatcom.cf @@ -17,9 +17,8 @@ CC = wpp386 CC_IN = CC_OUT = -fo= -# important: -j is necessary for proper build #CPPFLAGS = -j -w3 -d1 -5s -5r -fp5 -oehtz -s -zq -zt0 -bt=DOS -CPPFLAGS = -j -w2 -d1 -zq -bt=DOS +CPPFLAGS = -w2 -d1 -zq -bt=DOS #architecture flags CPPFLAGS += -5 -fp5 diff --git a/harbour/config/hpux/gcc.cf b/harbour/config/hpux/gcc.cf index 8dfb342722..5e06d83439 100644 --- a/harbour/config/hpux/gcc.cf +++ b/harbour/config/hpux/gcc.cf @@ -14,7 +14,7 @@ CC_IN = -c CC_OUT = -o CPPFLAGS = -I. -I$(HB_INC_COMPILE) -CFLAGS = -DHB_OS_HPUX -Wall -W -fsigned-char +CFLAGS = -DHB_OS_HPUX -Wall -W # uncomment this if you want to farce relocateable code for .so libs # it's necessary on some platforms but can reduce performance diff --git a/harbour/config/linux/gcc.cf b/harbour/config/linux/gcc.cf index 0de0c2404d..2eff9cf10e 100644 --- a/harbour/config/linux/gcc.cf +++ b/harbour/config/linux/gcc.cf @@ -21,7 +21,7 @@ CC_OUT = -o CPPFLAGS = -I. -I$(HB_INC_COMPILE) # We are under linux -CFLAGS = -DHB_OS_LINUX -Wall -W -fsigned-char +CFLAGS = -DHB_OS_LINUX -Wall -W # uncomment this if you want to force relocateable code for .so libs # it's necessary on some platforms but can reduce performance diff --git a/harbour/config/linux/owatcom.cf b/harbour/config/linux/owatcom.cf index c51adaf29a..27e18b2e65 100644 --- a/harbour/config/linux/owatcom.cf +++ b/harbour/config/linux/owatcom.cf @@ -15,9 +15,8 @@ CC = wpp386 CC_IN = CC_OUT = -fo= -# important: -j is necessary for proper build #CPPFLAGS = -j -w3 -d1 -5s -5r -fp5 -oehtz -s -zq -zt0 -bt=linux -CPPFLAGS = -j -w2 -d1 -zq -bt=linux +CPPFLAGS = -w2 -d1 -zq -bt=linux #architecture flags CPPFLAGS += -5 -fp5 diff --git a/harbour/config/os2/gcc.cf b/harbour/config/os2/gcc.cf index d4f32c7e0a..32f905533c 100644 --- a/harbour/config/os2/gcc.cf +++ b/harbour/config/os2/gcc.cf @@ -13,7 +13,7 @@ CC = gcc CC_IN = -c CC_OUT = -o CPPFLAGS = -I. -I$(TOP)include -I$(HB_INC_COMPILE) -CFLAGS = -W -Wall -O2 -DHB_OS_OS2 -fsigned-char +CFLAGS = -W -Wall -DHB_OS_OS2 -O2 # 26/03/2004 - # added -DTCPV40HDRS to compile with GCC 3.2.2 on OS/2 v4.0 with mixed 16/32 bit tcp/ip stack diff --git a/harbour/config/sunos/gcc.cf b/harbour/config/sunos/gcc.cf index c18d09a79f..993e5e52ba 100644 --- a/harbour/config/sunos/gcc.cf +++ b/harbour/config/sunos/gcc.cf @@ -14,7 +14,7 @@ CC_IN = -c CC_OUT = -o CPPFLAGS = -I. -I$(HB_INC_COMPILE) -CFLAGS = -DHB_OS_SUNOS -Wall -W -fsigned-char +CFLAGS = -DHB_OS_SUNOS -Wall -W # uncomment this if you want to farce relocateable code for .so libs # it's necessary on some platforms but can reduce performance diff --git a/harbour/config/w32/watcom.cf b/harbour/config/w32/watcom.cf index 2dfb3a9134..586b1de68d 100644 --- a/harbour/config/w32/watcom.cf +++ b/harbour/config/w32/watcom.cf @@ -17,8 +17,7 @@ CC = wpp386 CC_IN = CC_OUT = -fo= -# important: -j is necessary for proper build -CPPFLAGS = -j -w3 -d2 -5s -5r -fp5 -oxehtz -zq -zt0 -mf -bt=NT +CPPFLAGS = -w3 -d2 -5s -5r -fp5 -oxehtz -zq -zt0 -mf -bt=NT CFLAGS := -i$(subst /,\,$(HB_INC_COMPILE)) -i. # work arround to DOS command line size limit diff --git a/harbour/contrib/rdd_ads/ads1.c b/harbour/contrib/rdd_ads/ads1.c index 1aed914734..d4fb3d3721 100644 --- a/harbour/contrib/rdd_ads/ads1.c +++ b/harbour/contrib/rdd_ads/ads1.c @@ -2342,11 +2342,11 @@ static ERRCODE adsCreate( ADSAREAP pArea, LPDBOPENINFO pCreateInfo ) { ADSHANDLE hTable, hConnection; UNSIGNED32 uRetVal, u32Length, uiFldLen, uiLen; - UNSIGNED8 *ucfieldDefs, *ucfieldPtr ; - UNSIGNED8 ucBuffer[MAX_STR_LEN + 1], ucField[ADS_MAX_FIELD_NAME + 1]; + UNSIGNED8 *ucfieldDefs, *ucfieldPtr; + UNSIGNED8 ucBuffer[MAX_STR_LEN + 1]; USHORT uiCount; LPFIELD pField; - char cType[8]; + char * cType; HB_TRACE(HB_TR_DEBUG, ("adsCreate(%p, %p)", pArea, pCreateInfo)); @@ -2388,47 +2388,48 @@ static ERRCODE adsCreate( ADSAREAP pArea, LPDBOPENINFO pCreateInfo ) pArea->maxFieldLen = pField->uiLen; } + cType = NULL; switch( pField->uiType ) { case HB_IT_DATE: if( pField->uiTypeExtended == 0 ) { - strcpy( cType, "D" ); + cType = "D"; } else { - strcpy( cType, "ShortD" ); + cType = "ShortD"; } break; case HB_IT_LOGICAL: - strcpy( cType, "L" ); + cType = "L"; break; case HB_IT_MEMO: if( pField->uiTypeExtended == 0 ) { - strcpy( cType, "M" ); + cType = "M"; } else if( pField->uiTypeExtended == ADS_VARCHAR ) { - strcpy( cType, "VarC" ); + cType = "VarC"; } else if( pField->uiTypeExtended == ADS_BINARY ) { - strcpy( cType, "Binary" ); + cType = "Binary"; } else if( pField->uiTypeExtended == ADS_IMAGE ) { - strcpy( cType, "Image" ); + cType = "Image"; } break; case HB_IT_STRING: if( pField->uiTypeExtended == 0 ) { - strcpy( cType, "C" ); + cType = "C"; } else if( pField->uiTypeExtended == ADS_RAW ) { - strcpy( cType, "Raw" ); + cType = "Raw"; } break; case HB_IT_INTEGER: @@ -2436,39 +2437,45 @@ static ERRCODE adsCreate( ADSAREAP pArea, LPDBOPENINFO pCreateInfo ) case HB_IT_DOUBLE: if( pField->uiTypeExtended == 0 ) { - strcpy( cType, "N" ); + cType = "N"; } else if( pField->uiTypeExtended == ADS_INTEGER ) { - strcpy( cType, "Int" ); + cType = "Int"; } else if( pField->uiTypeExtended == ADS_SHORTINT ) { - strcpy( cType, "ShortI" ); + cType = "ShortI"; } else if( pField->uiTypeExtended == ADS_DOUBLE ) { - strcpy( cType, "Double" ); + cType = "Double"; } else if( pField->uiTypeExtended == ADS_TIME ) { - strcpy( cType, "Time" ); + cType = "Time"; } else if( pField->uiTypeExtended == ADS_TIMESTAMP ) { - strcpy( cType, "TimeSt" ); + cType = "TimeSt"; } else if( pField->uiTypeExtended == ADS_AUTOINC ) { - strcpy( cType, "Auto" ); + cType = "Auto"; } else if( pField->uiTypeExtended == ADS_CURDOUBLE ) { - strcpy( cType, "CurD" ); + cType = "CurD"; } break; } + if( cType == NULL ) + { + /* RT_ERROR */ + return FAILURE; + } + switch( pField->uiType ) { case HB_IT_DATE: @@ -2476,39 +2483,23 @@ static ERRCODE adsCreate( ADSAREAP pArea, LPDBOPENINFO pCreateInfo ) case HB_IT_MEMO: if( pField->uiTypeExtended != ADS_VARCHAR ) { - char * szName = hb_dynsymName( ( PHB_DYNS ) pField->sym ); - if( strlen( szName ) > (unsigned int) pArea->uiMaxFieldNameLength ) - { - strncpy( (char*)ucField, szName, pArea->uiMaxFieldNameLength ); - uiFldLen = sprintf( (char*)ucBuffer, "%s,%s;", ucField, cType ); - } - else - { - uiFldLen = sprintf( (char*)ucBuffer, "%s,%s;", szName, cType ); - } + uiFldLen = snprintf( ( char * ) ucBuffer, MAX_STR_LEN, "%.*s,%s;", + ( int ) pArea->uiMaxFieldNameLength, + hb_dynsymName( ( PHB_DYNS ) pField->sym ), + cType ); break; } - default: - { - char * szName ; - szName = hb_dynsymName( ( PHB_DYNS ) pField->sym ); - if( strlen( szName) > (unsigned int) pArea->uiMaxFieldNameLength ) - { - strncpy( (char*)ucField, szName, pArea->uiMaxFieldNameLength ); - uiFldLen = sprintf( (char*)ucBuffer, "%s,%s,%d,%d;", ucField, cType, pField->uiLen, pField->uiDec ); - } - else - { - uiFldLen = sprintf( (char*)ucBuffer, "%s,%s,%d,%d;", szName, cType, pField->uiLen, pField->uiDec ); - } + uiFldLen = snprintf( ( char * ) ucBuffer, MAX_STR_LEN, "%.*s,%s,%d,%d;", + ( int ) pArea->uiMaxFieldNameLength, + hb_dynsymName( ( PHB_DYNS ) pField->sym ), + cType, pField->uiLen, pField->uiDec ); break; - } } if( uiFldLen == 0 ) { - uiFldLen = strlen( (const char *) ucBuffer ); // should have been set by sprintf above. + uiFldLen = strlen( ( char * ) ucBuffer ); /* should have been set by snprintf above. */ } if( uiFldLen >= uiLen ) { @@ -2682,8 +2673,7 @@ static ERRCODE adsInfo( ADSAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) AdsGetVersion( &ulMajor, &ulMinor, &ucLetter, ucDesc, &usDescLen); - sprintf( ( char *) ucVersion, "%s, v%ld.%ld%c", ucDesc, ulMajor, ulMinor, ucLetter ); - + snprintf( ( char *) ucVersion, sizeof( ucVersion ), "%s, v%ld.%ld%c", ucDesc, ulMajor, ulMinor, ucLetter ); hb_itemPutC( pItem, ( char *) ucVersion ); break; } @@ -3000,13 +2990,13 @@ static ERRCODE adsSysName( ADSAREAP pArea, BYTE * pBuffer ) switch( u16TableType ) { case ADS_NTX: - strcpy( (char *) pBuffer, "ADSNTX" ); + hb_strncpy( ( char * ) pBuffer, "ADSNTX", HARBOUR_MAX_RDD_DRIVERNAME_LENGTH ); break; case ADS_CDX: - strcpy( (char *) pBuffer, "ADSCDX" ); + hb_strncpy( ( char * ) pBuffer, "ADSCDX", HARBOUR_MAX_RDD_DRIVERNAME_LENGTH ); break; case ADS_ADT: - strcpy( (char *) pBuffer, "ADSADT" ); + hb_strncpy( ( char * ) pBuffer, "ADSADT", HARBOUR_MAX_RDD_DRIVERNAME_LENGTH ); break; } @@ -3370,14 +3360,14 @@ static ERRCODE adsOrderCreate( ADSAREAP pArea, LPDBORDERCREATEINFO pOrderInfo ) pucWhile[ u16Len ] = 0; if( u16 == ADS_STRING ) /* add quotation marks around the key */ { - strcat( (char * ) pucWhile, "<=\""); - strcat( (char * ) pucWhile, (char * ) pucScope ); - strcat( (char * ) pucWhile, "\"" ); + hb_strncat( ( char * ) pucWhile, "<=\"", sizeof( pucWhile ) - 1 ); + hb_strncat( ( char * ) pucWhile, (char * ) pucScope, sizeof( pucWhile ) - 1 ); + hb_strncat( ( char * ) pucWhile, "\"", sizeof( pucWhile ) - 1 ); } else { - strcat( (char * ) pucWhile, "<="); - strcat( (char * ) pucWhile, (char * ) pucScope ); + hb_strncat( ( char * ) pucWhile, "<=", sizeof( pucWhile ) - 1 ); + hb_strncat( ( char * ) pucWhile, ( char * ) pucScope, sizeof( pucWhile ) - 1 ); } } hTableOrIndex = pArea->hOrdCurrent; @@ -3390,13 +3380,13 @@ static ERRCODE adsOrderCreate( ADSAREAP pArea, LPDBORDERCREATEINFO pOrderInfo ) { if( pucWhile[0] ) { - strcat( (char * ) pucWhile, ".AND.("); - strcat( (char * ) pucWhile, (char * ) pArea->lpdbOrdCondInfo->abWhile ); - strcat( (char * ) pucWhile, ")"); + hb_strncat( ( char * ) pucWhile, ".AND.(", sizeof( pucWhile ) - 1 ); + hb_strncat( ( char * ) pucWhile, ( char * ) pArea->lpdbOrdCondInfo->abWhile, sizeof( pucWhile ) - 1 ); + hb_strncat( ( char * ) pucWhile, ")", sizeof( pucWhile ) - 1 ); } else { - strcat( (char * ) pucWhile, (char * ) pArea->lpdbOrdCondInfo->abWhile ); + hb_strncat( ( char * ) pucWhile, ( char * ) pArea->lpdbOrdCondInfo->abWhile, sizeof( pucWhile ) - 1 ); } if( pArea->hOrdCurrent ) { diff --git a/harbour/contrib/rdd_ads/adsfunc.c b/harbour/contrib/rdd_ads/adsfunc.c index 53b4189c55..879f0aaa6a 100644 --- a/harbour/contrib/rdd_ads/adsfunc.c +++ b/harbour/contrib/rdd_ads/adsfunc.c @@ -1962,10 +1962,10 @@ HB_FUNC( ADSVERSION ) switch( iVersionType ) { case 0: - sprintf( ucVersion, "%ld.%ld%c", ulMajor, ulMinor, ucLetter ); + snprintf( ucVersion, sizeof( ucVersion ), "%ld.%ld%c", ulMajor, ulMinor, ucLetter ); break; case 3: - sprintf( ucVersion, "%s, v%ld.%ld%c", (char *) ucDesc, ulMajor, ulMinor, ucLetter ); + snprintf( ucVersion, sizeof( ucVersion ), "%s, v%ld.%ld%c", (char *) ucDesc, ulMajor, ulMinor, ucLetter ); break; default: ucVersion[0] = 0; diff --git a/harbour/include/hbapifs.h b/harbour/include/hbapifs.h index 6f69574742..0ca4dfd8a0 100644 --- a/harbour/include/hbapifs.h +++ b/harbour/include/hbapifs.h @@ -140,8 +140,8 @@ extern HB_EXPORT ULONG hb_fsTell ( FHANDLE hFileHandle ); /* retrieve t extern HB_EXPORT BOOL hb_fsSetDevMode ( FHANDLE hFileHandle, USHORT uiDevMode ); /* change the device mode of a file (text/binary) */ extern HB_EXPORT void hb_fsSetError ( USHORT uiError ); /* set the file system DOS error number */ extern HB_EXPORT void hb_fsSetIOError ( BOOL fResult, USHORT uiOperation ); /* set the file system error number after IO operation */ -extern HB_EXPORT USHORT hb_fsWrite ( FHANDLE hFileHandle, BYTE * pBuff, USHORT ulCount ); /* write to an open file from a buffer (<=64K) */ -extern HB_EXPORT ULONG hb_fsWriteLarge ( FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCount ); /* write to an open file from a buffer (>64K) */ +extern HB_EXPORT USHORT hb_fsWrite ( FHANDLE hFileHandle, const BYTE * pBuff, USHORT ulCount ); /* write to an open file from a buffer (<=64K) */ +extern HB_EXPORT ULONG hb_fsWriteLarge ( FHANDLE hFileHandle, const BYTE * pBuff, ULONG ulCount ); /* write to an open file from a buffer (>64K) */ extern HB_EXPORT FHANDLE hb_fsPOpen( BYTE * pFilename, BYTE * pMode ); extern HB_EXPORT FHANDLE hb_fsGetOsHandle( FHANDLE hFileHandle ); diff --git a/harbour/include/hbexprc.c b/harbour/include/hbexprc.c index 5f55d6c537..2d158e06a9 100644 --- a/harbour/include/hbexprc.c +++ b/harbour/include/hbexprc.c @@ -748,7 +748,6 @@ HB_EXPR_PTR hb_compExprReducePlusStrings( HB_EXPR_PTR pLeft, HB_EXPR_PTR pRight, pRight->value.asString.string, pRight->ulLength ); pLeft->ulLength += pRight->ulLength; pLeft->value.asString.string[ pLeft->ulLength ] = '\0'; - hb_compExprFree( pRight, HB_COMP_PARAM ); } else { @@ -760,9 +759,8 @@ HB_EXPR_PTR hb_compExprReducePlusStrings( HB_EXPR_PTR pLeft, HB_EXPR_PTR pRight, szString[ pLeft->ulLength ] = '\0'; pLeft->value.asString.string = szString; pLeft->value.asString.dealloc = TRUE; - hb_compExprFree( pRight, HB_COMP_PARAM ); } - HB_SYMBOL_UNUSED( HB_COMP_PARAM ); /* to suppress BCC warning */ + hb_compExprFree( pRight, HB_COMP_PARAM ); return pLeft; } diff --git a/harbour/include/hbsetup.h b/harbour/include/hbsetup.h index edb98ec41a..e3f7ea2d2a 100644 --- a/harbour/include/hbsetup.h +++ b/harbour/include/hbsetup.h @@ -345,14 +345,34 @@ #endif #endif -#ifdef HB_OS_DARWIN - #if ! defined(HB_OS_BSD) +#ifndef HB_OS_LINUX + #if defined(linux) || defined(__linux) || defined(__linux__) + #define HB_OS_LINUX + #endif +#endif + +#ifndef HB_OS_SUNOS + #if defined(sun) || defined(__sun) + #define HB_OS_SUNOS + #endif +#endif + +#ifndef HB_OS_HPUX + #if defined(__hpux) + #define HB_OS_HPUX + #endif +#endif + +#ifndef HB_OS_BSD + #if defined( __FreeBSD__ ) || defined( __NetBSD__ ) || defined( __OpenBSD__ ) || \ + defined( __APPLE__ ) || defined( HB_OS_DARWIN ) #define HB_OS_BSD #endif #endif #ifndef HB_OS_UNIX - #if defined(OS_UNIX_COMPATIBLE) || defined(HB_OS_LINUX) || defined(HB_OS_BSD) || defined(HB_OS_SUNOS) || defined(HB_OS_HPUX) + #if defined(OS_UNIX_COMPATIBLE) || defined(HB_OS_LINUX) || \ + defined(HB_OS_BSD) || defined(HB_OS_SUNOS) || defined(HB_OS_HPUX) #define HB_OS_UNIX #endif #endif diff --git a/harbour/source/common/expropt2.c b/harbour/source/common/expropt2.c index 225af828db..a61f26b695 100644 --- a/harbour/source/common/expropt2.c +++ b/harbour/source/common/expropt2.c @@ -1218,10 +1218,8 @@ BOOL hb_compExprReduceCHR( HB_EXPR_PTR pSelf, HB_COMP_DECL ) pExpr->ValType = HB_EV_STRING; if( pArg->value.asNum.NumType == HB_ET_LONG ) { - BYTE bVal; - bVal = ( pArg->value.asNum.val.l % 256 ); - - if( bVal == 0 && pArg->value.asNum.val.l != 0 ) + if( ( pArg->value.asNum.val.l & 0xff ) == 0 && + pArg->value.asNum.val.l != 0 ) { pExpr->value.asString.string = ""; pExpr->value.asString.dealloc = FALSE; @@ -1230,7 +1228,7 @@ BOOL hb_compExprReduceCHR( HB_EXPR_PTR pSelf, HB_COMP_DECL ) else { pExpr->value.asString.string = ( char * ) hb_xgrab( 2 ); - pExpr->value.asString.string[ 0 ] = bVal; + pExpr->value.asString.string[ 0 ] = ( char ) pArg->value.asNum.val.l; pExpr->value.asString.string[ 1 ] = '\0'; pExpr->value.asString.dealloc = TRUE; pExpr->ulLength = 1; @@ -1239,7 +1237,7 @@ BOOL hb_compExprReduceCHR( HB_EXPR_PTR pSelf, HB_COMP_DECL ) else { pExpr->value.asString.string = ( char * ) hb_xgrab( 2 ); - pExpr->value.asString.string[ 0 ] = ( ( unsigned int ) pArg->value.asNum.val.d % 256 ); + pExpr->value.asString.string[ 0 ] = ( char ) ( ( unsigned int ) pArg->value.asNum.val.d & 0xff ); pExpr->value.asString.string[ 1 ] = '\0'; pExpr->value.asString.dealloc = TRUE; pExpr->ulLength = 1; diff --git a/harbour/source/common/hbdate.c b/harbour/source/common/hbdate.c index abbd7028fa..f294b9d75e 100644 --- a/harbour/source/common/hbdate.c +++ b/harbour/source/common/hbdate.c @@ -279,7 +279,7 @@ HB_EXPORT void hb_dateTimeStr( char * pszTime ) { SYSTEMTIME st; GetLocalTime( &st ); - sprintf( pszTime, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond ); + snprintf( pszTime, 9, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond ); } #else { @@ -288,7 +288,7 @@ HB_EXPORT void hb_dateTimeStr( char * pszTime ) time( &t ); oTime = localtime( &t ); - sprintf( pszTime, "%02d:%02d:%02d", oTime->tm_hour, oTime->tm_min, oTime->tm_sec ); + snprintf( pszTime, 9, "%02d:%02d:%02d", oTime->tm_hour, oTime->tm_min, oTime->tm_sec ); } #endif } diff --git a/harbour/source/common/hbgete.c b/harbour/source/common/hbgete.c index 46d30684c3..35b5cc90b6 100644 --- a/harbour/source/common/hbgete.c +++ b/harbour/source/common/hbgete.c @@ -92,8 +92,7 @@ char * hb_getenv( const char * szName ) if( DosScanEnv( szName, &EnvValue ) == NO_ERROR ) { - pszBuffer = ( char * ) hb_xgrab( strlen( EnvValue ) + 1 ); - strcpy( pszBuffer, ( char * ) EnvValue ); + pszBuffer = hb_strdup( EnvValue ); } } @@ -104,8 +103,7 @@ char * hb_getenv( const char * szName ) if( pszTemp != NULL ) { - pszBuffer = ( char * ) hb_xgrab( strlen( pszTemp ) + 1 ); - strcpy( pszBuffer, pszTemp ); + pszBuffer = hb_strdup( pszTemp ); } } diff --git a/harbour/source/common/hbstr.c b/harbour/source/common/hbstr.c index 5928dd4644..eae139a4bc 100644 --- a/harbour/source/common/hbstr.c +++ b/harbour/source/common/hbstr.c @@ -214,9 +214,9 @@ AJ: 2004-02-23 Concatenates multiple strings into a single result. Eg. hb_xstrcat (buffer, "A", "B", NULL) stores "AB" in buffer. */ -HB_EXPORT char * hb_xstrcat ( char *szDest, const char *szSrc, ... ) +HB_EXPORT char * hb_xstrcat( char * szDest, const char * szSrc, ... ) { - char *szResult = szDest; + char * szResult = szDest; va_list va; HB_TRACE(HB_TR_DEBUG, ("hb_xstrcat(%p, %p, ...)", szDest, szSrc)); @@ -224,18 +224,17 @@ HB_EXPORT char * hb_xstrcat ( char *szDest, const char *szSrc, ... ) while( *szDest ) szDest++; - va_start(va, szSrc); - + va_start( va, szSrc ); while( szSrc ) { while ( *szSrc ) *szDest++ = *szSrc++; - szSrc = va_arg ( va, char* ); + szSrc = va_arg( va, char * ); } - *szDest = '\0'; - va_end ( va ); - return ( szResult ); + va_end( va ); + + return szResult; } /* @@ -248,39 +247,39 @@ allocates a new buffer with the required length and returns that. The buffer is allocated using hb_xgrab(), and should eventually be freed using hb_xfree(). */ -HB_EXPORT char * hb_xstrcpy ( char *szDest, const char *szSrc, ...) +HB_EXPORT char * hb_xstrcpy( char * szDest, const char * szSrc, ... ) { - const char *szSrc_Ptr; + char * szResult; va_list va; - size_t dest_size; HB_TRACE(HB_TR_DEBUG, ("hb_xstrcpy(%p, %p, ...)", szDest, szSrc)); - if (szDest == NULL) + if( szDest == NULL ) { - va_start (va, szSrc); - szSrc_Ptr = szSrc; - dest_size = 1; - while (szSrc_Ptr) - { - dest_size += strlen (szSrc_Ptr); - szSrc_Ptr = va_arg (va, char *); - } - va_end (va); - - szDest = (char *) hb_xgrab( dest_size ); + const char * szSrcPtr = szSrc; + ULONG ulSize = 1; + va_start( va, szSrc ); + while( szSrcPtr ) + { + ulSize += strlen( szSrcPtr ); + szSrcPtr = va_arg( va, char * ); + } + va_end( va ); + szDest = ( char * ) hb_xgrab( ulSize ); } + szResult = szDest; - va_start (va, szSrc); - szSrc_Ptr = szSrc; - szDest [0] = '\0'; - while (szSrc_Ptr) + va_start( va, szSrc ); + while( szSrc ) { - hb_xstrcat (szDest, szSrc_Ptr, NULL ); - szSrc_Ptr = va_arg (va, char *); + while ( *szSrc ) + *szDest++ = *szSrc++; + szSrc = va_arg( va, char * ); } - va_end (va); - return (szDest); + *szDest = '\0'; + va_end( va ); + + return szResult; } static double hb_numPow10( int nPrecision ) diff --git a/harbour/source/common/hbver.c b/harbour/source/common/hbver.c index bf8c463dfb..fa9af65dea 100644 --- a/harbour/source/common/hbver.c +++ b/harbour/source/common/hbver.c @@ -119,7 +119,7 @@ char * hb_verPlatform( void ) regs.h.ah = 0x30; HB_DOS_INT86( 0x21, ®s, ®s ); - sprintf( pszPlatform, "DOS %d.%02d", regs.h.al, regs.h.ah ); + snprintf( pszPlatform, 256, "DOS %d.%02d", regs.h.al, regs.h.ah ); /* Host OS detection: Windows 2.x, 3.x, 95/98 */ @@ -132,11 +132,11 @@ char * hb_verPlatform( void ) char szHost[ 128 ]; if( regs.h.al == 0x01 || regs.h.al == 0xFF ) - sprintf( szHost, " (Windows 2.x)" ); + snprintf( szHost, sizeof( szHost ), " (Windows 2.x)" ); else - sprintf( szHost, " (Windows %d.%02d)", regs.h.al, regs.h.ah ); + snprintf( szHost, sizeof( szHost ), " (Windows %d.%02d)", regs.h.al, regs.h.ah ); - strcat( pszPlatform, szHost ); + hb_strncat( pszPlatform, szHost, 255 ); } } @@ -147,7 +147,7 @@ char * hb_verPlatform( void ) HB_DOS_INT86( 0x21, ®s, ®s ); if( regs.HB_XREGS.bx == 0x3205 ) - strcat( pszPlatform, " (Windows NT/2000)" ); + hb_strncat( pszPlatform, " (Windows NT/2000)", 255 ); } /* Host OS detection: OS/2 */ @@ -161,11 +161,11 @@ char * hb_verPlatform( void ) char szHost[ 128 ]; if( regs.h.al == 20 && regs.h.ah > 20 ) - sprintf( szHost, " (OS/2 %d.%02d)", regs.h.ah / 10, regs.h.ah % 10 ); + snprintf( szHost, sizeof( szHost ), " (OS/2 %d.%02d)", regs.h.ah / 10, regs.h.ah % 10 ); else - sprintf( szHost, " (OS/2 %d.%02d)", regs.h.al / 10, regs.h.ah ); + snprintf( szHost, sizeof( szHost ), " (OS/2 %d.%02d)", regs.h.al / 10, regs.h.ah ); - strcat( pszPlatform, szHost ); + hb_strncat( pszPlatform, szHost, 255 ); } } } @@ -183,16 +183,16 @@ char * hb_verPlatform( void ) /* is this OS/2 2.x ? */ if( aulQSV[ QSV_VERSION_MINOR - 1 ] < 30 ) { - sprintf( pszPlatform, "OS/2 %ld.%02ld", - aulQSV[ QSV_VERSION_MAJOR - 1 ] / 10, - aulQSV[ QSV_VERSION_MINOR - 1 ] ); + snprintf( pszPlatform, 256, "OS/2 %ld.%02ld", + aulQSV[ QSV_VERSION_MAJOR - 1 ] / 10, + aulQSV[ QSV_VERSION_MINOR - 1 ] ); } else - sprintf( pszPlatform, "OS/2 %2.2f", - ( float ) aulQSV[ QSV_VERSION_MINOR - 1 ] / 10 ); + snprintf( pszPlatform, 256, "OS/2 %2.2f", + ( float ) aulQSV[ QSV_VERSION_MINOR - 1 ] / 10 ); } else - sprintf( pszPlatform, "OS/2" ); + snprintf( pszPlatform, 256, "OS/2" ); } #elif defined(HB_OS_WIN_32) @@ -239,11 +239,11 @@ char * hb_verPlatform( void ) break; } - sprintf( pszPlatform, "%s %lu.%lu.%04d", - pszName, - ( ULONG ) osVer.dwMajorVersion, - ( ULONG ) osVer.dwMinorVersion, - ( USHORT ) LOWORD( osVer.dwBuildNumber ) ); + snprintf( pszPlatform, 256, "%s %lu.%lu.%04d", + pszName, + ( ULONG ) osVer.dwMajorVersion, + ( ULONG ) osVer.dwMinorVersion, + ( USHORT ) LOWORD( osVer.dwBuildNumber ) ); /* Add service pack/other info */ @@ -256,13 +256,13 @@ char * hb_verPlatform( void ) if( osVer.szCSDVersion[ i ] != '\0' ) { - strcat( pszPlatform, " " ); - strcat( pszPlatform, osVer.szCSDVersion + i ); + hb_strncat( pszPlatform, " ", 255 ); + hb_strncat( pszPlatform, osVer.szCSDVersion + i, 255 ); } } } else - sprintf( pszPlatform, "Windows" ); + snprintf( pszPlatform, 256, "Windows" ); } #elif defined(HB_OS_UNIX) @@ -271,19 +271,19 @@ char * hb_verPlatform( void ) struct utsname un; uname( &un ); - sprintf( pszPlatform, "%s %s %s", un.sysname, un.release, un.machine ); + snprintf( pszPlatform, 256, "%s %s %s", un.sysname, un.release, un.machine ); } #elif defined(HB_OS_MAC) { - strcpy( pszPlatform, "MacOS compatible" ); + hb_strncpy( pszPlatform, "MacOS compatible", 255 ); } #else { - strcpy( pszPlatform, "(unknown)" ); + hb_strncpy( pszPlatform, "(unknown)", 255 ); } #endif @@ -291,16 +291,23 @@ char * hb_verPlatform( void ) return pszPlatform; } -HB_EXPORT BOOL hb_iswinnt(void) +HB_EXPORT BOOL hb_iswinnt( void ) { #if defined(HB_OS_WIN_32) + static BOOL s_fWinNT = FALSE; + static BOOL s_fInited = FALSE; - OSVERSIONINFO osvi ; - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx (&osvi); - return(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT); /* && osvi.dwMajorVersion >= 4); */ + if( ! s_fInited ) + { + OSVERSIONINFO osvi ; + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + GetVersionEx (&osvi); + s_fWinNT = osvi.dwPlatformId == VER_PLATFORM_WIN32_NT; /* && osvi.dwMajorVersion >= 4); */ + s_fInited = TRUE; + } + return s_fWinNT; #else - return FALSE ; + return FALSE ; #endif } @@ -374,7 +381,7 @@ char * hb_verCompiler( void ) #endif #if defined(__cplusplus) - strcpy( szSub, "++" ); + hb_strncpy( szSub, "++", sizeof( szSub ) - 1 ); #endif iVerMajor = _MSC_VER / 100; @@ -426,7 +433,7 @@ char * hb_verCompiler( void ) #endif #if defined(__cplusplus) - strcpy( szSub, "++" ); + hb_strncpy( szSub, "++", sizeof( szSub ) - 1 ); #endif iVerMajor = __WATCOMC__ / 100; @@ -457,7 +464,7 @@ char * hb_verCompiler( void ) #endif #if defined(__cplusplus) - strcpy( szSub, "++" ); + hb_strncpy( szSub, "++", sizeof( szSub ) - 1 ); #endif iVerMajor = __GNUC__; @@ -477,28 +484,28 @@ char * hb_verCompiler( void ) if( pszName ) { if( iVerPatch != 0 ) - sprintf( pszCompiler, "%s%s %hd.%hd.%hd", pszName, szSub, iVerMajor, iVerMinor, iVerPatch ); + snprintf( pszCompiler, 80, "%s%s %hd.%hd.%hd", pszName, szSub, iVerMajor, iVerMinor, iVerPatch ); else if( iVerMajor != 0 || iVerMinor != 0 ) - sprintf( pszCompiler, "%s%s %hd.%hd", pszName, szSub, iVerMajor, iVerMinor ); + snprintf( pszCompiler, 80, "%s%s %hd.%hd", pszName, szSub, iVerMajor, iVerMinor ); else - sprintf( pszCompiler, "%s%s", pszName, szSub ); + snprintf( pszCompiler, 80, "%s%s", pszName, szSub ); } else - strcpy( pszCompiler, "(unknown)" ); + hb_strncpy( pszCompiler, "(unknown)", 79 ); #if defined(__DJGPP__) - sprintf( szSub, " (DJGPP %i.%02i)", ( int ) __DJGPP__, ( int ) __DJGPP_MINOR__ ); - strcat( pszCompiler, szSub ); + snprintf( szSub, sizeof( szSub ), " (DJGPP %i.%02i)", ( int ) __DJGPP__, ( int ) __DJGPP_MINOR__ ); + hb_strncat( pszCompiler, szSub, 79 ); #elif defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__GNUC__) #if defined( HB_ARCH_16BIT ) - strcat( pszCompiler, " (16 bit)" ); + hb_strncat( pszCompiler, " (16 bit)", 79 ); #elif defined( HB_ARCH_32BIT ) - strcat( pszCompiler, " (32 bit)" ); + hb_strncat( pszCompiler, " (32 bit)", 79 ); #elif defined( HB_ARCH_64BIT ) - strcat( pszCompiler, " (64 bit)" ); + hb_strncat( pszCompiler, " (64 bit)", 79 ); #endif #endif @@ -516,8 +523,8 @@ char * hb_verHarbour( void ) pszVersion = ( char * ) hb_xgrab( 80 ); - sprintf( pszVersion, "Harbour Alpha build %d.%d Intl.", - HB_VER_MINOR, HB_VER_REVISION ); + snprintf( pszVersion, 80, "Harbour Alpha build %d.%d Intl.", + HB_VER_MINOR, HB_VER_REVISION ); return pszVersion; } diff --git a/harbour/source/common/hbverdsp.c b/harbour/source/common/hbverdsp.c index 7f7b683f05..249c233d42 100644 --- a/harbour/source/common/hbverdsp.c +++ b/harbour/source/common/hbverdsp.c @@ -255,7 +255,7 @@ void hb_verBuildInfo( void ) { char buffer[ 64 ]; - sprintf( buffer, "Maximum symbol name length: %i", HB_SYMBOL_NAME_LEN ); + snprintf( buffer, sizeof( buffer ), "Maximum symbol name length: %i", HB_SYMBOL_NAME_LEN ); hb_conOutErr( buffer, 0 ); hb_conOutErr( hb_conNewLine(), 0 ); } diff --git a/harbour/source/compiler/cmdcheck.c b/harbour/source/compiler/cmdcheck.c index 0048961708..9484bcb14d 100644 --- a/harbour/source/compiler/cmdcheck.c +++ b/harbour/source/compiler/cmdcheck.c @@ -488,20 +488,24 @@ static void hb_compChkEnvironVar( HB_COMP_DECL, char *szSwitch ) case 'x': case 'X': { - unsigned int i = 0; - char *szPrefix = hb_strdup( s + 1 ); - - while( i < strlen( szPrefix ) && !HB_ISOPTSEP( szPrefix[i] ) ) - i++; - szPrefix[i] = '\0'; - - if( strlen( szPrefix ) == 0 ) - sprintf( szPrefix, "%08lX_", PackDateTime() ); - - strncpy( HB_COMP_PARAM->szPrefix, szPrefix, 16 ); - HB_COMP_PARAM->szPrefix[ 16 ] = '\0'; - strcat( HB_COMP_PARAM->szPrefix, "_" ); - hb_xfree( szPrefix ); + unsigned int i = 1; + while( s[i] && !HB_ISOPTSEP( s[i] ) && + i < sizeof( HB_COMP_PARAM->szPrefix ) - 1 ) + { + ++i; + } + if( i > 1 ) + { + memcpy( HB_COMP_PARAM->szPrefix, s + 1, i - 1 ); + HB_COMP_PARAM->szPrefix[ i - 1 ] = '_'; + HB_COMP_PARAM->szPrefix[ i ] = '\0'; + } + else + { + snprintf( HB_COMP_PARAM->szPrefix, + sizeof( HB_COMP_PARAM->szPrefix ), + "%08lX_", PackDateTime() ); + } break; } diff --git a/harbour/source/compiler/fixflex.c b/harbour/source/compiler/fixflex.c index 8f91d0d546..8da2494903 100644 --- a/harbour/source/compiler/fixflex.c +++ b/harbour/source/compiler/fixflex.c @@ -90,17 +90,17 @@ void fixup( char * inbuf, char * outbuf, int c_plus_plus ) if( c_plus_plus ) { /* If compiling for C++, the arrays need to be extern "C" in both modules */ - static char tempbuf[ BUF_SIZE ]; - strcpy( tempbuf, "extern \"C\" " ); - strcpy( outbuf, tempbuf ); - strcat( outbuf, inbuf + 7 ); - strcat( tempbuf, inbuf + 7 ); - strcpy( inbuf, tempbuf ); + static char tempbuf[ BUF_SIZE + 1 ]; + hb_strncpy( tempbuf, "extern \"C\" ", BUF_SIZE ); + hb_strncpy( outbuf, tempbuf, BUF_SIZE ); + hb_strncat( outbuf, inbuf + 7, BUF_SIZE ); + hb_strncat( tempbuf, inbuf + 7, BUF_SIZE ); + hb_strncpy( inbuf, tempbuf, BUF_SIZE ); } else { /* if compiling for C, the arrays only need to be extern in lexyy.c */ - strcpy( outbuf, inbuf + 7 ); + hb_strncpy( outbuf, inbuf + 7, BUF_SIZE ); memcpy( inbuf, "extern", 6 ); } ptr = strchr( inbuf, '=' ); @@ -133,10 +133,10 @@ int main( int argc, char * argv [] ) if( strcmp( argv[ i ], "-P-" ) == 0 ) c_plus_plus = 0; } /* Rename source to backup. */ - strcpy( backup, argv[ 1 ] ); + hb_strncpy( backup, argv[ 1 ], sizeof( backup ) - 1 ); len = strlen( backup ); for( i = 1; i < 4; i++ ) if( backup[ len - i ] == '.' ) backup[ len - i ] = 0; - strcat( backup, ".bak" ); + hb_strncat( backup, ".bak", sizeof( backup ) - 1 ); if( rename( argv[ 1 ], backup ) ) { rc = 10; @@ -205,7 +205,7 @@ int main( int argc, char * argv [] ) else { char * ptr; - strcpy( outbuf, inbuf ); + hb_strncpy( outbuf, inbuf, BUF_SIZE ); /* Check for stuff to copy or move to dest. */ if( check_count > 0 && !move1 && !move2 && !move3 && !copy ) diff --git a/harbour/source/compiler/gencobj.c b/harbour/source/compiler/gencobj.c index b48c481ebc..13832b25d3 100644 --- a/harbour/source/compiler/gencobj.c +++ b/harbour/source/compiler/gencobj.c @@ -35,21 +35,21 @@ static char * hb_searchpath( const char *, char *, char * ); #define HB_CFG_FILENAME "harbour.cfg" /* QUESTION: Allocate buffer dynamically ? */ -#define HB_CFG_LINE_LEN 100 +#define HB_CFG_LINE_LEN ( _POSIX_PATH_MAX ) /*--------------------------------------------------------------------------*/ /* Builds platform dependant object module from Harbour C output */ void hb_compGenCObj( HB_COMP_DECL, PHB_FNAME pFileName ) { - char szFileName[ _POSIX_PATH_MAX ]; + char szFileName[ _POSIX_PATH_MAX + 1 ]; char szLine[ HB_CFG_LINE_LEN ]; - char szCompiler[ HB_CFG_LINE_LEN ] = ""; - char szOptions[ HB_CFG_LINE_LEN ] = ""; - char szCommandLine[ HB_CFG_LINE_LEN * 2 ]; - char szOutPath[ _POSIX_PATH_MAX ] = "\0"; + char szCompiler[ HB_CFG_LINE_LEN + 1 ] = ""; + char szOptions[ HB_CFG_LINE_LEN + 1 ] = ""; + char szCommandLine[ HB_CFG_LINE_LEN * 2 + 1 ]; + char szOutPath[ _POSIX_PATH_MAX + 1 ] = "\0"; #if defined( HOST_OS_UNIX_COMPATIBLE ) - char szDefaultUnixPath[ _POSIX_PATH_MAX ] = "/etc:/usr/local/etc"; + char szDefaultUnixPath[ _POSIX_PATH_MAX + 1 ] = "/etc:/usr/local/etc"; char * pszEnv = szDefaultUnixPath; #define HB_NULL_STR " > /dev/null" #define HB_ACCESS_FLAG F_OK @@ -80,7 +80,7 @@ void hb_compGenCObj( HB_COMP_DECL, PHB_FNAME pFileName ) /* Set up things */ /* Grab space */ - pszCfg = ( char * ) hb_xgrab( /*strlen( pszEnv )*/ _POSIX_PATH_MAX ); + pszCfg = ( char * ) hb_xgrab( _POSIX_PATH_MAX + 1 ); if( pszEnv && pszEnv[ 0 ] != '\0' && *hb_searchpath( HB_CFG_FILENAME, pszEnv, pszCfg ) ) { @@ -128,12 +128,12 @@ void hb_compGenCObj( HB_COMP_DECL, PHB_FNAME pFileName ) /* Checks compiler name */ if( ! hb_stricmp( szStr, "CC" ) ) { - sprintf( szCompiler, "%s", szToken ); + snprintf( szCompiler, sizeof( szCompiler ), "%s", szToken ); } /* Checks optional switches */ else if( ! hb_stricmp( szStr, "CFLAGS" ) ) { - sprintf( szOptions, "%s", szToken ); + snprintf( szOptions, sizeof( szCompiler ), "%s", szToken ); } /* Wanna see C compiler output ? */ else if( ! hb_stricmp( szStr, "VERBOSE" ) ) @@ -186,21 +186,21 @@ void hb_compGenCObj( HB_COMP_DECL, PHB_FNAME pFileName ) hb_fsFNameMerge( pszTemp, pOut ); #if defined(_MSC_VER) - strcat( szOutPath, "-Fo" ); + hb_strncat( szOutPath, "-Fo", sizeof( szOutPath ) - 1 ); #elif defined(__WATCOMC__) - strcat( szOutPath, "-fo=" ); + hb_strncat( szOutPath, "-fo=", sizeof( szOutPath ) - 1 ); #else - strcat( szOutPath, "-o" ); + hb_strncat( szOutPath, "-o", sizeof( szOutPath ) - 1 ); #endif - strcat( szOutPath, pszTemp ); + hb_strncat( szOutPath, pszTemp, sizeof( szOutPath ) - 1 ); hb_xfree( pOut ); } if( *szCompiler ) { - sprintf( szCommandLine, "%s %s %s %s", szCompiler, szOptions, szOutPath, szFileName ); + snprintf( szCommandLine, sizeof( szCommandLine ), "%s %s %s %s", szCompiler, szOptions, szOutPath, szFileName ); if( bVerbose ) { @@ -208,7 +208,7 @@ void hb_compGenCObj( HB_COMP_DECL, PHB_FNAME pFileName ) } else { - strcat( szCommandLine, HB_NULL_STR ); + hb_strncat( szCommandLine, HB_NULL_STR, sizeof( szCommandLine ) ); } /* Compile it! */ @@ -245,7 +245,7 @@ static char * hb_searchpath( const char * pszFile, char * pszEnv, char * pszCfg /* Check current dir first */ if( access( ( const char * ) pszFile, HB_ACCESS_FLAG ) == 0 ) { - sprintf( pszCfg, "%s", pszFile ); + snprintf( pszCfg, _POSIX_PATH_MAX + 1, "%s", pszFile ); return ( char * ) pszFile; } else @@ -256,7 +256,7 @@ static char * hb_searchpath( const char * pszFile, char * pszEnv, char * pszCfg { while( pszPath ) { - sprintf( pszCfg, "%s%c%s", pszPath, OS_PATH_DELIMITER, pszFile ); + snprintf( pszCfg, _POSIX_PATH_MAX + 1, "%s%c%s", pszPath, OS_PATH_DELIMITER, pszFile ); if( access( ( const char * ) pszCfg, HB_ACCESS_FLAG ) == 0 ) { bFound = TRUE; @@ -270,7 +270,7 @@ static char * hb_searchpath( const char * pszFile, char * pszEnv, char * pszCfg /* If not found, make sure to return a NULL string */ if( ! bFound ) - sprintf( pszCfg, "%s", "" ); + snprintf( pszCfg, _POSIX_PATH_MAX + 1, "%s", "" ); return ( char * ) pszCfg; } diff --git a/harbour/source/compiler/genobj32.c b/harbour/source/compiler/genobj32.c index 06a2929ef4..3f2842c977 100644 --- a/harbour/source/compiler/genobj32.c +++ b/harbour/source/compiler/genobj32.c @@ -66,7 +66,7 @@ void hb_compGenObj32( HB_COMP_DECL, PHB_FNAME pFileName ) { char szFileName[ _POSIX_PATH_MAX ]; FILE * hObjFile; /* file handle for OBJ output */ - char compiler[ 70 ], * szVer; + char * szVer; if( ! pFileName->szExtension ) pFileName->szExtension = ".obj"; @@ -84,12 +84,10 @@ void hb_compGenObj32( HB_COMP_DECL, PHB_FNAME pFileName ) fflush( stdout ); } - szVer = hb_verHarbour(); - sprintf( compiler, "%s\n", szVer ); - hb_xfree( szVer ); - CompiledFileName( hObjFile, szFileName ); - CompilerVersion( hObjFile, compiler ); + szVer = hb_verHarbour(); + CompilerVersion( hObjFile, szVer ); + hb_xfree( szVer ); GenerateLocalNames( hObjFile ); GenerateExternals( HB_COMP_PARAM, hObjFile ); GenerateCodeSegment( HB_COMP_PARAM, hObjFile ); diff --git a/harbour/source/compiler/harbour.c b/harbour/source/compiler/harbour.c index acb3f3615d..f647b948be 100644 --- a/harbour/source/compiler/harbour.c +++ b/harbour/source/compiler/harbour.c @@ -313,6 +313,10 @@ int isatty( int handle ) #define HB_MEMINFO_SIGNATURE 0xDEADBEAF #define HB_MEMSTR_BLOCK_MAX 256 +#ifndef HB_MEMFILER +# define HB_MEMFILER 0xff +#endif + typedef struct _HB_MEMINFO { struct _HB_MEMINFO * pPrevBlock; @@ -363,6 +367,9 @@ void * hb_xgrab( ULONG ulSize ) /* allocates fixed memory, exits on failu if( s_ulMemoryMaxBlocks < s_ulMemoryBlocks ) s_ulMemoryMaxBlocks = s_ulMemoryBlocks; pMem = ( BYTE * ) pMem + HB_MEMINFO_SIZE; +#ifdef HB_PARANOID_MEM_CHECK + memset( HB_MEM_PTR( pMem ), HB_MEMFILER, ulSize ); +#endif } else #else @@ -539,13 +546,13 @@ void hb_xexit( void ) hb_conOutErr( hb_conNewLine(), 0 ); hb_conOutErr( "----------------------------------------", 0 ); hb_conOutErr( hb_conNewLine(), 0 ); - sprintf( szBuffer, "Total memory allocated: %lu bytes (%lu blocks)", s_ulMemoryMaxConsumed, s_ulMemoryMaxBlocks ); + snprintf( szBuffer, sizeof( szBuffer ), "Total memory allocated: %lu bytes (%lu blocks)", s_ulMemoryMaxConsumed, s_ulMemoryMaxBlocks ); hb_conOutErr( szBuffer, 0 ); if( s_ulMemoryBlocks ) { hb_conOutErr( hb_conNewLine(), 0 ); - sprintf( szBuffer, "WARNING! Memory allocated but not released: %lu bytes (%lu blocks)", s_ulMemoryConsumed, s_ulMemoryBlocks ); + snprintf( szBuffer, sizeof( szBuffer ), "WARNING! Memory allocated but not released: %lu bytes (%lu blocks)", s_ulMemoryConsumed, s_ulMemoryBlocks ); hb_conOutErr( szBuffer, 0 ); } @@ -582,11 +589,11 @@ void hb_errInternal( ULONG ulIntCode, const char * szText, const char * szPar1, HB_TRACE(HB_TR_DEBUG, ("hb_errInternal(%lu, %s, %s, %s)", ulIntCode, szText, szPar1, szPar2)); hb_conOutErr( hb_conNewLine(), 0 ); - sprintf( buffer, "Unrecoverable error %lu: ", ulIntCode ); + snprintf( buffer, sizeof( buffer ), "Unrecoverable error %lu: ", ulIntCode ); hb_conOutErr( buffer, 0 ); if( szText ) { - sprintf( buffer, szText, szPar1, szPar2 ); + snprintf( buffer, sizeof( buffer ), szText, szPar1, szPar2 ); hb_conOutErr( buffer, 0 ); } hb_conOutErr( hb_conNewLine(), 0 ); @@ -3408,7 +3415,7 @@ void hb_compFinalizeFunction( HB_COMP_DECL ) /* fixes all last defined function if( pVar->szName && pFunc->szName && pFunc->szName[0] && (! ( pVar->iUsed & VU_USED )) ) { char szFun[ 256 ]; - sprintf( szFun, "%s(%i)", pFunc->szName, pVar->iDeclLine ); + snprintf( szFun, sizeof( szFun ), "%s(%i)", pFunc->szName, pVar->iDeclLine ); hb_compGenWarning( HB_COMP_PARAM, hb_comp_szWarnings, 'W', HB_COMP_WARN_VAR_NOT_USED, pVar->szName, szFun ); } @@ -3421,7 +3428,7 @@ void hb_compFinalizeFunction( HB_COMP_DECL ) /* fixes all last defined function if( pVar->szName && pFunc->szName && pFunc->szName[0] && ! ( pVar->iUsed & VU_USED ) ) { char szFun[ 256 ]; - sprintf( szFun, "%s(%i)", pFunc->szName, pVar->iDeclLine ); + snprintf( szFun, sizeof( szFun ), "%s(%i)", pFunc->szName, pVar->iDeclLine ); hb_compGenWarning( HB_COMP_PARAM, hb_comp_szWarnings, 'W', HB_COMP_WARN_VAR_NOT_USED, pVar->szName, szFun ); } @@ -4528,7 +4535,7 @@ static int hb_compCompile( HB_COMP_DECL, char * szPrg, BOOL bSingleFile ) HB_COMP_PARAM->pInitFunc->pCode[ 4 ] = HB_HIBYTE( HB_COMP_PARAM->iStaticCnt ); HB_COMP_PARAM->pInitFunc->iStaticsBase = HB_COMP_PARAM->iStaticCnt; /* Update pseudo function name */ - sprintf( szNewName, "(_INITSTATICS%05d)", HB_COMP_PARAM->iStaticCnt ); + snprintf( szNewName, sizeof( szNewName ), "(_INITSTATICS%05d)", HB_COMP_PARAM->iStaticCnt ); HB_COMP_PARAM->pInitFunc->szName = hb_compIdentifierNew( HB_COMP_PARAM, szNewName, HB_IDENT_COPY ); pSym = hb_compSymbolAdd( HB_COMP_PARAM, HB_COMP_PARAM->pInitFunc->szName, NULL, HB_SYM_FUNCNAME ); diff --git a/harbour/source/compiler/harbour.l b/harbour/source/compiler/harbour.l index 6215cdf2b2..0e03f4eb66 100644 --- a/harbour/source/compiler/harbour.l +++ b/harbour/source/compiler/harbour.l @@ -1007,15 +1007,15 @@ Separator {SpaceTab} SaveInline: + iSize = strlen( (char*) sBuffer ); if( pInline->pCode == NULL ) { - pInline->pCode = (BYTE *) hb_xgrab( ( iSize = strlen( (char*) sBuffer ) ) + 1 ); - strcpy( (char *) pInline->pCode, (char*) sBuffer ); + pInline->pCode = (BYTE *) hb_strdup( sBuffer ); } else { - pInline->pCode = (BYTE *) hb_xrealloc( pInline->pCode, pInline->lPCodeSize + ( iSize = strlen( (char*) sBuffer ) ) + 1 ); - strcpy( (char *) (pInline->pCode + pInline->lPCodeSize), (char*) sBuffer ); + pInline->pCode = (BYTE *) hb_xrealloc( pInline->pCode, pInline->lPCodeSize + iSize + 1 ); + memcpy( (char *) (pInline->pCode + pInline->lPCodeSize), sBuffer, iSize + 1 ); } pInline->lPCodeSize += iSize; diff --git a/harbour/source/compiler/harbour.y b/harbour/source/compiler/harbour.y index 6ab7df754f..226e9e1bb7 100644 --- a/harbour/source/compiler/harbour.y +++ b/harbour/source/compiler/harbour.y @@ -1119,7 +1119,7 @@ ElemList : EmptyExpression { $$ = hb_compExprNewList( $1, HB_COMP CodeBlock : CBSTART { $$ = hb_compExprNewCodeBlock( $1.string, $1.length, $1.flags, HB_COMP_PARAM ); $1.string = NULL; } BlockNoVar '|' BlockExpList '}' { $$ = $2; } - | CBSTART { $$ = hb_compExprNewCodeBlock( $1.string, $1.length, $1.flags, HB_COMP_PARAM ); } BlockVarList + | CBSTART { $$ = hb_compExprNewCodeBlock( $1.string, $1.length, $1.flags, HB_COMP_PARAM ); $1.string = NULL; } BlockVarList '|' BlockExpList '}' { $$ = $2; } ; diff --git a/harbour/source/compiler/harbour.yyc b/harbour/source/compiler/harbour.yyc index f88f9192ec..bfe7039384 100644 --- a/harbour/source/compiler/harbour.yyc +++ b/harbour/source/compiler/harbour.yyc @@ -6700,7 +6700,7 @@ yyreduce: case 528: #line 1122 "harbour.y" - { (yyval.asExpr) = hb_compExprNewCodeBlock( (yyvsp[(1) - (1)].asCodeblock).string, (yyvsp[(1) - (1)].asCodeblock).length, (yyvsp[(1) - (1)].asCodeblock).flags, HB_COMP_PARAM ); ;} + { (yyval.asExpr) = hb_compExprNewCodeBlock( (yyvsp[(1) - (1)].asCodeblock).string, (yyvsp[(1) - (1)].asCodeblock).length, (yyvsp[(1) - (1)].asCodeblock).flags, HB_COMP_PARAM ); (yyvsp[(1) - (1)].asCodeblock).string = NULL; ;} break; case 529: diff --git a/harbour/source/compiler/hbfix.c b/harbour/source/compiler/hbfix.c index acc551fbbc..1089396bd9 100644 --- a/harbour/source/compiler/hbfix.c +++ b/harbour/source/compiler/hbfix.c @@ -201,8 +201,8 @@ static HB_FIX_FUNC( hb_p_poplocalnear ) char sTemp[16]; char sTemp2[16]; - sprintf( sTemp, "%i", pFunc->wParamCount ); - sprintf( sTemp2, "%i", iVar ); + snprintf( sTemp, sizeof( sTemp ), "%i", pFunc->wParamCount ); + snprintf( sTemp2, sizeof( sTemp2 ), "%i", iVar ); hb_compGenError( cargo->HB_COMP_PARAM, hb_comp_szErrors, 'F', HB_COMP_ERR_OPTIMIZEDLOCAL_OUT_OF_RANGE, sTemp2, sTemp ); } } @@ -230,8 +230,8 @@ static HB_FIX_FUNC( hb_p_pushlocalnear ) char sTemp[16]; char sTemp2[16]; - sprintf( sTemp, "%i", pFunc->wParamCount ); - sprintf( sTemp2, "%i", iVar ); + snprintf( sTemp, sizeof( sTemp ), "%i", pFunc->wParamCount ); + snprintf( sTemp2, sizeof( sTemp2 ), "%i", iVar ); hb_compGenError( cargo->HB_COMP_PARAM, hb_comp_szErrors, 'F', HB_COMP_ERR_OPTIMIZEDLOCAL_OUT_OF_RANGE, sTemp2, sTemp ); } } @@ -258,8 +258,8 @@ static HB_FIX_FUNC( hb_p_localnearaddint ) char sTemp[16]; char sTemp2[16]; - sprintf( sTemp, "%i", pFunc->wParamCount ); - sprintf( sTemp2, "%i", uiVar ); + snprintf( sTemp, sizeof( sTemp ), "%i", pFunc->wParamCount ); + snprintf( sTemp2, sizeof( sTemp2 ), "%i", uiVar ); hb_compGenError( cargo->HB_COMP_PARAM, hb_comp_szErrors, 'F', HB_COMP_ERR_OPTIMIZEDLOCAL_OUT_OF_RANGE, sTemp2, sTemp ); } } diff --git a/harbour/source/compiler/hbfunchk.c b/harbour/source/compiler/hbfunchk.c index 1bd5a57123..466a4c0a2d 100644 --- a/harbour/source/compiler/hbfunchk.c +++ b/harbour/source/compiler/hbfunchk.c @@ -145,11 +145,11 @@ BOOL hb_compFunCallCheck( HB_COMP_DECL, char * szFuncCall, int iArgs ) char szMsg[ 40 ]; if( f[ iPos ].iMaxParam == -1 ) - sprintf( szMsg, "\nPassed: %i, expected: at least %i", iArgs, f[ iPos ].iMinParam ); + snprintf( szMsg, sizeof( szMsg ), "\nPassed: %i, expected: at least %i", iArgs, f[ iPos ].iMinParam ); else if( f[ iPos ].iMinParam == f[ iPos ].iMaxParam ) - sprintf( szMsg, "\nPassed: %i, expected: %i", iArgs, f[ iPos ].iMinParam ); + snprintf( szMsg, sizeof( szMsg ), "\nPassed: %i, expected: %i", iArgs, f[ iPos ].iMinParam ); else - sprintf( szMsg, "\nPassed: %i, expected: %i - %i", iArgs, f[ iPos ].iMinParam, f[ iPos ].iMaxParam ); + snprintf( szMsg, sizeof( szMsg ), "\nPassed: %i, expected: %i - %i", iArgs, f[ iPos ].iMinParam, f[ iPos ].iMaxParam ); hb_compGenError( HB_COMP_PARAM, hb_comp_szErrors, 'E', HB_COMP_ERR_CHECKING_ARGS, szFuncCall, szMsg ); return FALSE; diff --git a/harbour/source/pp/ppcore.c b/harbour/source/pp/ppcore.c index 9a6f796642..f1199bf1f0 100644 --- a/harbour/source/pp/ppcore.c +++ b/harbour/source/pp/ppcore.c @@ -616,7 +616,7 @@ static void hb_pp_readLine( PHB_PP_STATE pState ) char szLine[ 12 ]; pState->pFile->iLastDisp = iLine; - sprintf( szLine, "\r%i00\r", iLine ); + snprintf( szLine, sizeof( szLine ), "\r%i00\r", iLine ); hb_pp_disp( pState, szLine ); } } @@ -859,7 +859,7 @@ static void hb_pp_getLine( PHB_PP_STATE pState ) if( pState->pInLineFunc ) { char szFunc[ 24 ]; - sprintf( szFunc, "HB_INLINE_%03d", ++pState->iInLineCount ); + snprintf( szFunc, sizeof( szFunc ), "HB_INLINE_%03d", ++pState->iInLineCount ); if( pInLinePtr && * pInLinePtr ) hb_pp_tokenSetValue( *pInLinePtr, szFunc, strlen( szFunc ) ); pState->pInLineFunc( pState->cargo, szFunc, @@ -1092,7 +1092,7 @@ static void hb_pp_getLine( PHB_PP_STATE pState ) char szCh[3]; hb_pp_tokenAddNext( pState, pBuffer, ++ul, HB_PP_TOKEN_NUL ); - sprintf( szCh, "%02x", ch & 0xff ); + snprintf( szCh, sizeof( szCh ), "%02x", ch & 0xff ); hb_pp_error( pState, 'E', HB_PP_ERR_ILLEGAL_CHAR, szCh ); } else if( HB_PP_ISDIGIT( ch ) ) @@ -2145,7 +2145,7 @@ static void hb_pp_pragmaNew( PHB_PP_STATE pState, PHB_PP_TOKEN pToken ) { char szLine[ 12 ]; - sprintf( szLine, "%d", pState->pFile->iCurrentLine ); + snprintf( szLine, sizeof( szLine ), "%d", pState->pFile->iCurrentLine ); hb_membufFlush( pState->pBuffer ); hb_membufAddCh( pState->pBuffer, '(' ); hb_membufAddStr( pState->pBuffer, szLine ); @@ -4086,9 +4086,9 @@ static void hb_pp_genLineTokens( PHB_PP_STATE pState ) #else if( pState->pFile->fGenLineInfo ) { - char szLine[ 10 ]; + char szLine[ 12 ]; - sprintf( szLine, "%d", pState->pFile->iCurrentLine ); + snprintf( szLine, sizeof( szLine ), "%d", pState->pFile->iCurrentLine ); hb_pp_tokenAdd( &pState->pNextTokenPtr, "#", 1, 0, HB_PP_TOKEN_DIRECTIVE | HB_PP_TOKEN_STATIC ); hb_pp_tokenAdd( &pState->pNextTokenPtr, "line", 4, 0, HB_PP_TOKEN_KEYWORD | HB_PP_TOKEN_STATIC ); hb_pp_tokenAdd( &pState->pNextTokenPtr, szLine, strlen( szLine ), 1, HB_PP_TOKEN_NUMBER ); @@ -4437,7 +4437,7 @@ PHB_PP_TOKEN hb_pp_tokenGet( PHB_PP_STATE pState ) hb_pp_tokenStr( pState->pTokenOut, pState->pBuffer, TRUE, TRUE, pState->iLastType ); #endif - pState->iLastType = pState->pTokenOut->type; + pState->iLastType = HB_PP_TOKEN_TYPE( pState->pTokenOut->type ); fwrite( hb_membufPtr( pState->pBuffer ), sizeof( char ), hb_membufLen( pState->pBuffer ), pState->file_out ); } @@ -4539,7 +4539,7 @@ void hb_pp_initDynDefines( PHB_PP_STATE pState ) /* __PLATFORM__* */ pSrc = szPlatform = hb_verPlatform(); - pDst = strcpy( szDefine, "__PLATFORM__" ); + pDst = strncpy( szDefine, "__PLATFORM__", sizeof( szDefine ) ); i = 12; while( pSrc[ 0 ] > ' ' && i < ( int ) sizeof( szDefine ) - 1 ) { @@ -4564,12 +4564,12 @@ void hb_pp_initDynDefines( PHB_PP_STATE pState ) hb_pp_addDefine( pState, szDefine, szResult ); #ifdef HB_OS_UNIX - strcpy( szDefine + 12, "UNIX" ); + strncpy( szDefine + 12, "UNIX", sizeof( szDefine ) ); hb_pp_addDefine( pState, szDefine, szResult ); #endif /* __HARBOUR__ */ - sprintf( szResult, "%05d", HB_MAX( ( HB_VER_MAJOR << 8 ) | HB_VER_MINOR, 1 ) ); + snprintf( szResult, sizeof( szResult ), "%05d", HB_MAX( ( HB_VER_MAJOR << 8 ) | HB_VER_MINOR, 1 ) ); hb_pp_addDefine( pState, "__HARBOUR__", szResult ); /* __DATE__ */ @@ -4587,7 +4587,7 @@ void hb_pp_initDynDefines( PHB_PP_STATE pState ) szResult[ 10 ] = '\0'; hb_pp_addDefine( pState, "__TIME__", szResult ); - sprintf( szResult, "%d", ( int ) sizeof( void * ) ); + snprintf( szResult, sizeof( szResult ), "%d", ( int ) sizeof( void * ) ); #if defined( HB_ARCH_16BIT ) hb_pp_addDefine( pState, "__ARCH16BIT__", szResult ); #elif defined( HB_ARCH_32BIT ) @@ -5049,19 +5049,21 @@ char * hb_pp_tokenBlockString( PHB_PP_STATE pState, PHB_PP_TOKEN pToken, hb_membufFlush( pState->pBuffer ); if( HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_LEFT_CB ) { + USHORT ltype = HB_PP_TOKEN_NUL; int iBraces = 0; do { - hb_pp_tokenStr( pToken, pState->pBuffer, iBraces != 0, FALSE, 0 ); + hb_pp_tokenStr( pToken, pState->pBuffer, FALSE, TRUE, ltype ); + ltype = HB_PP_TOKEN_TYPE( pToken->type ); if( HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_AMPERSAND ) { if( pToken->pNext && - HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_LEFT_PB ) - * piType |= HB_BLOCK_LATEEVAL; + HB_PP_TOKEN_TYPE( pToken->pNext->type ) == HB_PP_TOKEN_LEFT_PB ) + * piType |= HB_BLOCK_MACRO | HB_BLOCK_LATEEVAL; } else if( HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_MACROVAR || HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_MACROTEXT ) - * piType |= HB_BLOCK_LATEEVAL | HB_BLOCK_MACRO; + * piType |= HB_BLOCK_MACRO; else if( HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_RIGHT_CB ) --iBraces; else if( HB_PP_TOKEN_TYPE( pToken->type ) == HB_PP_TOKEN_LEFT_CB ) diff --git a/harbour/source/pp/ppgen.c b/harbour/source/pp/ppgen.c index 756fef62bd..2bbbce70bb 100644 --- a/harbour/source/pp/ppgen.c +++ b/harbour/source/pp/ppgen.c @@ -142,13 +142,13 @@ static int hb_pp_writeRules( FILE * fout, PHB_PP_RULE pFirst, char * szName ) ++iRule; if( pRule->pMatch ) { - sprintf( szMatch, "s_%cm%03d", szName[0], iRule ); + snprintf( szMatch, sizeof( szMatch ), "s_%cm%03d", szName[0], iRule ); hb_pp_writeTokenList( fout, pRule->pMatch, szMatch ); } if( pRule->pResult ) { - sprintf( szResult, "s_%cr%03d", szName[0], iRule ); + snprintf( szResult, sizeof( szResult ), "s_%cr%03d", szName[0], iRule ); hb_pp_writeTokenList( fout, pRule->pResult, szResult ); } pRule = pRule->pPrev; @@ -163,13 +163,13 @@ static int hb_pp_writeRules( FILE * fout, PHB_PP_RULE pFirst, char * szName ) { ++iRule; if( pRule->pMatch ) - sprintf( szMatch, "s_%cm%03d", szName[0], iRule ); + snprintf( szMatch, sizeof( szMatch ), "s_%cm%03d", szName[0], iRule ); else - strcpy( szMatch, "NULL " ); + strncpy( szMatch, "NULL ", sizeof( szResult ) ); if( pRule->pResult ) - sprintf( szResult, "s_%cr%03d", szName[0], iRule ); + snprintf( szResult, sizeof( szResult ), "s_%cr%03d", szName[0], iRule ); else - strcpy( szResult, "NULL " ); + strncpy( szResult, "NULL ", sizeof( szResult ) ); ulRepeatBits = 0; for( u = 0, ulBit = 1; u < pRule->markers; ++u, ulBit <<= 1 ) diff --git a/harbour/source/pp/pplib.c b/harbour/source/pp/pplib.c index 0f2f5af070..e4be7176e8 100644 --- a/harbour/source/pp/pplib.c +++ b/harbour/source/pp/pplib.c @@ -71,7 +71,8 @@ static void hb_pp_ErrorMessage( void * cargo, char * szMsgTable[], { char szMsgBuf[ 1024 ]; PHB_ITEM pError; - sprintf( szMsgBuf, szMsgTable[ iCode - 1 ], szParam1, szParam2 ); + snprintf( szMsgBuf, sizeof( szMsgBuf ), szMsgTable[ iCode - 1 ], + szParam1, szParam2 ); pError = hb_errRT_New( ES_ERROR, "PP", 9999, ( ULONG ) iCode, szMsgBuf, NULL, 0, EF_NONE | EF_CANDEFAULT ); hb_errLaunch( pError ); diff --git a/harbour/source/rdd/dbcmd.c b/harbour/source/rdd/dbcmd.c index eb3385995b..cfd31ddccc 100644 --- a/harbour/source/rdd/dbcmd.c +++ b/harbour/source/rdd/dbcmd.c @@ -350,7 +350,7 @@ static char * hb_rddDefaultDrv( char * szDriver ) { return NULL; } - strcpy( s_szDefDriver, szNewDriver ); + hb_strncpy( s_szDefDriver, szNewDriver, HARBOUR_MAX_RDD_DRIVERNAME_LENGTH ); } else if( !fInit && !s_szDefDriver[ 0 ] && s_uiRddMax ) { @@ -361,7 +361,7 @@ static char * hb_rddDefaultDrv( char * szDriver ) { if( hb_rddFindNode( szDrvTable[ i ], NULL ) ) { - strcpy( s_szDefDriver, szDrvTable[ i ] ); + hb_strncpy( s_szDefDriver, szDrvTable[ i ], HARBOUR_MAX_RDD_DRIVERNAME_LENGTH ); break; } } @@ -378,7 +378,7 @@ HB_EXPORT int hb_rddRegister( char * szDriver, USHORT uiType ) { LPRDDNODE pRddNewNode; PHB_DYNS pGetFuncTable; - char * szGetFuncTable; + char szGetFuncTable[ HARBOUR_MAX_RDD_DRIVERNAME_LENGTH + 14 ]; USHORT uiFunctions; HB_TRACE(HB_TR_DEBUG, ("hb_rddRegister(%s, %hu)", szDriver, uiType)); @@ -388,11 +388,9 @@ HB_EXPORT int hb_rddRegister( char * szDriver, USHORT uiType ) return 1; } - szGetFuncTable = ( char * ) hb_xgrab( strlen( szDriver ) + 14 ); - strcpy( szGetFuncTable, szDriver ); - strcat( szGetFuncTable, "_GETFUNCTABLE" ); + snprintf( szGetFuncTable, sizeof( szGetFuncTable ), "%s_GETFUNCTABLE", + szDriver ); pGetFuncTable = hb_dynsymFindName( szGetFuncTable ); - hb_xfree( szGetFuncTable ); if( !pGetFuncTable ) { return 2; /* Not valid RDD */ @@ -4675,7 +4673,7 @@ HB_EXPORT ERRCODE hb_rddGetTempAlias( char * szAliasTmp ) for( i = 1 ; i < 1000 ; i++ ) { - sprintf( szAliasTmp, "__HBTMP%03i", i); + snprintf( szAliasTmp, 11, "__HBTMP%03i", i); if( hb_rddGetAliasNumber( szAliasTmp, &iArea ) != SUCCESS ) { diff --git a/harbour/source/rdd/dbfcdx/dbfcdx1.c b/harbour/source/rdd/dbfcdx/dbfcdx1.c index 0625a08aa8..d58ef36f22 100644 --- a/harbour/source/rdd/dbfcdx/dbfcdx1.c +++ b/harbour/source/rdd/dbfcdx/dbfcdx1.c @@ -3439,11 +3439,11 @@ static void hb_cdxTagHeaderStore( LPCDXTAG pTag ) HB_PUT_LE_UINT16( tagHeader.forExpLen, uiForLen + 1 ); if ( uiKeyLen > 0 ) { - strcpy( ( char * ) tagHeader.keyExpPool, pTag->KeyExpr ); + memcpy( tagHeader.keyExpPool, pTag->KeyExpr, uiKeyLen + 1 ); } if ( uiForLen > 0 ) { - strcpy( ( char * ) tagHeader.keyExpPool + uiKeyLen + 1, pTag->ForExpr ); + memcpy( tagHeader.keyExpPool + uiKeyLen + 1, pTag->ForExpr, uiForLen + 1 ); } hb_cdxIndexPageWrite( pTag->pIndex, pTag->TagBlock, (BYTE *) &tagHeader, sizeof( CDXTAGHEADER ) ); } @@ -7427,7 +7427,7 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo } else { - strcpy( szTagName, szCpndTagName ); + hb_strncpy( szTagName, szCpndTagName, CDX_MAXTAGNAMELEN ); fNewFile = TRUE; } diff --git a/harbour/source/rdd/dbffpt/dbffpt1.c b/harbour/source/rdd/dbffpt/dbffpt1.c index 6b43a9e40e..a513e1ae35 100644 --- a/harbour/source/rdd/dbffpt/dbffpt1.c +++ b/harbour/source/rdd/dbffpt/dbffpt1.c @@ -3891,15 +3891,15 @@ static ERRCODE hb_fptCreateMemFile( FPTAREAP pArea, LPDBOPENINFO pCreateInfo ) ulSize = 512; if ( pArea->uiMemoVersion == DB_MEMOVER_SIX ) { - strcpy( ( char *) fptHeader.signature1, "SIxMemo" ); + memcpy( fptHeader.signature1, "SIxMemo", 8 ); } else { - strcpy( ( char *) fptHeader.signature1, "Harbour" ); + memcpy( fptHeader.signature1, "Harbour", 8 ); if( pArea->uiMemoVersion == DB_MEMOVER_FLEX || pArea->uiMemoVersion == DB_MEMOVER_CLIP ) { - strcpy( ( char *) fptHeader.signature2, "FlexFile3\003" ); + memcpy( fptHeader.signature2, "FlexFile3\003", 11 ); ulSize = sizeof( FPTHEADER ); if( pArea->rddID == s_uiRddIdBLOB ) { diff --git a/harbour/source/rdd/hsx/hsx.c b/harbour/source/rdd/hsx/hsx.c index d13cc63996..49955e68a7 100644 --- a/harbour/source/rdd/hsx/hsx.c +++ b/harbour/source/rdd/hsx/hsx.c @@ -1961,7 +1961,7 @@ HB_FUNC( HS_VERSION ) pszHBVersion = hb_verHarbour(); iLen = strlen( szVer ) + strlen( pszHBVersion ) + 2; pszVersion = ( char * ) hb_xgrab( iLen + 1 ); - sprintf( pszVersion, "%s: %s", szVer, pszHBVersion ); + snprintf( pszVersion, iLen + 1, "%s: %s", szVer, pszHBVersion ); hb_retclen_buffer( pszVersion, iLen ); hb_xfree( pszHBVersion ); } diff --git a/harbour/source/rdd/workarea.c b/harbour/source/rdd/workarea.c index 9ac89d47ad..c068e967a8 100644 --- a/harbour/source/rdd/workarea.c +++ b/harbour/source/rdd/workarea.c @@ -1527,7 +1527,7 @@ ERRCODE hb_waError( AREAP pArea, PHB_ITEM pError ) if( pArea && pArea->lprfsHost->sysName ) SELF_SYSNAME( pArea, ( BYTE * ) szRddName ); else - strcpy( szRddName, "???DRIVER" ); + strncpy( szRddName, "???DRIVER", HARBOUR_MAX_RDD_DRIVERNAME_LENGTH ); hb_errPutSeverity( pError, ES_ERROR ); hb_errPutSubSystem( pError, szRddName ); hb_xfree( szRddName ); diff --git a/harbour/source/rtl/accept.c b/harbour/source/rtl/accept.c index ac7a0a04f8..d289b745e3 100644 --- a/harbour/source/rtl/accept.c +++ b/harbour/source/rtl/accept.c @@ -124,7 +124,7 @@ HB_FUNC( __ACCEPT ) szAcceptResult[ ulLen ] = '\0'; #ifdef HB_C52_UNDOC - strcpy( s_szAcceptResult, szAcceptResult ); + hb_strncpy( s_szAcceptResult, szAcceptResult, sizeof( s_szAcceptResult ) - 1 ); #endif hb_retc( szAcceptResult ); diff --git a/harbour/source/rtl/ampm.c b/harbour/source/rtl/ampm.c index d1274d2f05..1da0e1d200 100644 --- a/harbour/source/rtl/ampm.c +++ b/harbour/source/rtl/ampm.c @@ -57,11 +57,14 @@ HB_FUNC( AMPM ) char * pszTime = hb_parc( 1 ); ULONG ulTimeLen = hb_parclen( 1 ); char * pszResult = ( char * ) hb_xgrab( HB_MAX( ulTimeLen, 2 ) + 3 + 1 ); - USHORT uiHour = ( USHORT ) hb_strVal( pszTime, ulTimeLen ); + USHORT uiHour = 0; BOOL bAM; - memset( pszResult, '\0', 3 ); - memcpy( pszResult, pszTime, ulTimeLen ); + if( ulTimeLen ) + { + memcpy( pszResult, pszTime, ulTimeLen ); + uiHour = ( USHORT ) hb_strVal( pszTime, ulTimeLen ); + } if( uiHour == 0 || uiHour == 24 ) { @@ -89,7 +92,7 @@ HB_FUNC( AMPM ) else bAM = ( uiHour != 12 ); - strcpy( pszResult + ulTimeLen, bAM ? " am" : " pm" ); + memcpy( pszResult + ulTimeLen, bAM ? " am" : " pm", 4 ); hb_retclen_buffer( pszResult, ulTimeLen + 3 ); } diff --git a/harbour/source/rtl/console.c b/harbour/source/rtl/console.c index dc9715aced..fe31767b1a 100644 --- a/harbour/source/rtl/console.c +++ b/harbour/source/rtl/console.c @@ -321,26 +321,38 @@ HB_FUNC( QOUT ) if( hb_set.HB_SET_PRINTER && hb_set.hb_set_printhan != FS_ERROR ) { USHORT uiErrorOld = hb_fsError(); /* Save current user file error code */ - USHORT uiCount; + BYTE buf[ 80 ]; s_uiPRow++; - - uiCount = s_uiPCol = hb_set.HB_SET_MARGIN; - while( uiCount-- > 0 ) - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) " ", 1 ); - + s_uiPCol = hb_set.HB_SET_MARGIN; + if( s_uiPCol ) + { + if( s_uiPCol > sizeof( buf ) ) + { + BYTE * pBuf = ( BYTE * ) hb_xgrab( s_uiPCol ); + memset( pBuf, ' ', s_uiPCol ); + hb_fsWrite( hb_set.hb_set_printhan, pBuf, s_uiPCol ); + hb_xfree( pBuf ); + } + else + { + memset( buf, ' ', s_uiPCol ); + hb_fsWrite( hb_set.hb_set_printhan, buf, s_uiPCol ); + } + } hb_fsSetError( uiErrorOld ); /* Restore last user file error code */ } - HB_FUNCNAME( QQOUT )(); + HB_FUNC_EXEC( QQOUT ); } HB_FUNC( __EJECT ) /* Ejects the current page from the printer */ { - if( hb_stricmp( hb_set.HB_SET_DEVICE, "PRINTER" ) == 0 && hb_set.hb_set_printhan != FS_ERROR ) + if( hb_set.hb_set_printhan != FS_ERROR && hb_stricmp( hb_set.HB_SET_DEVICE, "PRINTER" ) == 0 ) { + static const BYTE byEop[ 2 ] = { 0x0C, 0x0D }; USHORT uiErrorOld = hb_fsError(); /* Save current user file error code */ - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) "\x0C\x0D", 2 ); + hb_fsWrite( hb_set.hb_set_printhan, byEop, 2 ); hb_fsSetError( uiErrorOld ); /* Restore last user file error code */ } @@ -366,42 +378,64 @@ static void hb_conDevPos( SHORT iRow, SHORT iCol ) if( hb_set.hb_set_printhan != FS_ERROR && hb_stricmp( hb_set.HB_SET_DEVICE, "PRINTER" ) == 0 ) { - USHORT uiErrorOld = hb_fsError(); /* Save current user file error code */ USHORT uiPRow = ( USHORT ) iRow; USHORT uiPCol = ( USHORT ) iCol + hb_set.HB_SET_MARGIN; - if( s_uiPRow != uiPRow ) + if( s_uiPRow != uiPRow || s_uiPCol != uiPCol ) { - if( ++s_uiPRow > uiPRow ) + USHORT uiErrorOld = hb_fsError(); /* Save current user file error code */ + BYTE buf[ 256 ]; + int iPtr = 0; + + if( s_uiPRow != uiPRow ) { - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) "\x0C\x0D", 2 ); - s_uiPRow = 0; + if( ++s_uiPRow > uiPRow ) + { + memcpy( &buf[ iPtr ], "\x0C\x0D", 2 ); + iPtr += 2; + s_uiPRow = 0; + } + else + { + memcpy( &buf[ iPtr ], s_szCrLf, s_iCrLfLen ); + iPtr += s_iCrLfLen; + } + + while( s_uiPRow < uiPRow ) + { + if( iPtr + s_iCrLfLen > ( int ) sizeof( buf ) ) + { + hb_fsWrite( hb_set.hb_set_printhan, buf, iPtr ); + iPtr = 0; + } + memcpy( &buf[ iPtr ], s_szCrLf, s_iCrLfLen ); + iPtr += s_iCrLfLen; + ++s_uiPRow; + } + s_uiPCol = 0; } - else + else if( s_uiPCol > uiPCol ) { - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) s_szCrLf, s_iCrLfLen ); + buf[ iPtr++ ] = '\x0D'; + s_uiPCol = 0; } - while( s_uiPRow < uiPRow ) + while( s_uiPCol < uiPCol ) { - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) s_szCrLf, s_iCrLfLen ); - ++s_uiPRow; + if( iPtr == ( int ) sizeof( buf ) ) + { + hb_fsWrite( hb_set.hb_set_printhan, buf, iPtr ); + iPtr = 0; + } + buf[ iPtr++ ] = ' '; + ++s_uiPCol; } - s_uiPCol = 0; - } - else if( s_uiPCol > uiPCol ) - { - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) "\x0D", 1 ); - s_uiPCol = 0; - } - while( s_uiPCol < uiPCol ) - { - hb_fsWrite( hb_set.hb_set_printhan, ( BYTE * ) " ", 1 ); - ++s_uiPCol; - } + if( iPtr ) + hb_fsWrite( hb_set.hb_set_printhan, buf, iPtr ); - hb_fsSetError( uiErrorOld ); /* Restore last user file error code */ + hb_fsSetError( uiErrorOld ); /* Restore last user file error code */ + } } else hb_gtSetPos( iRow, iCol ); diff --git a/harbour/source/rtl/defpath.c b/harbour/source/rtl/defpath.c index a8436b0811..9f58cedfdc 100644 --- a/harbour/source/rtl/defpath.c +++ b/harbour/source/rtl/defpath.c @@ -57,7 +57,7 @@ HB_FUNC( DEFPATH ) { - char buffer[ _POSIX_PATH_MAX +1 ]; + char buffer[ _POSIX_PATH_MAX + 1 ]; char delimiter[ 2 ] = ":"; int size; @@ -85,7 +85,7 @@ HB_FUNC( DEFPATH ) { if( size > 1 || OS_PATH_LIST_SEPARATOR == ':' ) delimiter[ 0 ] = OS_PATH_DELIMITER; - strcat( buffer, delimiter ); + hb_strncat( buffer, delimiter, _POSIX_PATH_MAX ); } hb_retc( buffer ); diff --git a/harbour/source/rtl/errorint.c b/harbour/source/rtl/errorint.c index b3f9dd89d9..ebf7f7e065 100644 --- a/harbour/source/rtl/errorint.c +++ b/harbour/source/rtl/errorint.c @@ -69,15 +69,15 @@ void hb_errInternal( ULONG ulIntCode, const char * szText, const char * szPar1, hb_conOutErr( hb_conNewLine(), 0 ); if( fLang ) - sprintf( buffer, ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ERRINTR ), ulIntCode ); + snprintf( buffer, sizeof( buffer ), ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ERRINTR ), ulIntCode ); else - sprintf( buffer, "Unrecoverable error %lu: ", ulIntCode ); + snprintf( buffer, sizeof( buffer ), "Unrecoverable error %lu: ", ulIntCode ); hb_conOutErr( buffer, 0 ); if( szText ) - sprintf( buffer, szText, szPar1, szPar2 ); + snprintf( buffer, sizeof( buffer ), szText, szPar1, szPar2 ); else if( fLang ) - sprintf( buffer, ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ERRINTR + ulIntCode - 9000 ), szPar1, szPar2 ); + snprintf( buffer, sizeof( buffer ), ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ERRINTR + ulIntCode - 9000 ), szPar1, szPar2 ); else buffer[ 0 ] = '\0'; hb_conOutErr( buffer, 0 ); diff --git a/harbour/source/rtl/filesys.c b/harbour/source/rtl/filesys.c index 8333014800..6cd63b51cf 100644 --- a/harbour/source/rtl/filesys.c +++ b/harbour/source/rtl/filesys.c @@ -920,7 +920,7 @@ HB_EXPORT USHORT hb_fsRead( FHANDLE hFileHandle, BYTE * pBuff, USHORT uiCount ) return uiRead; } -HB_EXPORT USHORT hb_fsWrite( FHANDLE hFileHandle, BYTE * pBuff, USHORT uiCount ) +HB_EXPORT USHORT hb_fsWrite( FHANDLE hFileHandle, const BYTE * pBuff, USHORT uiCount ) { USHORT uiWritten; @@ -1051,7 +1051,7 @@ HB_EXPORT ULONG hb_fsReadLarge( FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCount return ulRead; } -HB_EXPORT ULONG hb_fsWriteLarge( FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCount ) +HB_EXPORT ULONG hb_fsWriteLarge( FHANDLE hFileHandle, const BYTE * pBuff, ULONG ulCount ) { ULONG ulWritten; @@ -1085,7 +1085,7 @@ HB_EXPORT ULONG hb_fsWriteLarge( FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCoun ULONG ulLeftToWrite = ulCount; USHORT uiToWrite; USHORT uiWritten; - BYTE * pPtr = pBuff; + BYTE * pPtr = ( BYTE * ) pBuff; ulWritten = 0; @@ -1232,56 +1232,47 @@ HB_EXPORT BOOL hb_fsLock ( FHANDLE hFileHandle, ULONG ulStart, HB_TRACE(HB_TR_DEBUG, ("hb_fsLock(%p, %lu, %lu, %hu)", hFileHandle, ulStart, ulLength, uiMode)); #if defined(HB_WIN32_IO) + switch( uiMode & FL_MASK ) { - static BOOL s_bInit = 0, s_bWinNt ; - if ( !s_bInit ) - { - s_bInit = TRUE ; - s_bWinNt = hb_iswinnt() ; + case FL_LOCK: + { + if( hb_iswinnt() ) + { + OVERLAPPED sOlap ; + DWORD dwFlags ; + memset( &sOlap, 0, sizeof( OVERLAPPED ) ) ; + sOlap.Offset = ( ULONG ) ulStart ; + dwFlags = ( uiMode & FLX_SHARED ) ? 0 : LOCKFILE_EXCLUSIVE_LOCK ; + if( !s_fUseWaitLocks || !( uiMode & FLX_WAIT ) ) + { + dwFlags |= LOCKFILE_FAIL_IMMEDIATELY ; + } + bResult = LockFileEx( DostoWinHandle( hFileHandle ), dwFlags, 0, ulLength, 0, &sOlap ); + } + else + { + bResult = LockFile( DostoWinHandle( hFileHandle ), ulStart, 0, ulLength,0 ); + } + break; + } + case FL_UNLOCK: + { + if( hb_iswinnt() ) + { + OVERLAPPED sOlap ; + memset( &sOlap, 0, sizeof( OVERLAPPED ) ) ; + sOlap.Offset = ( ULONG ) ulStart ; + bResult = UnlockFileEx( DostoWinHandle( hFileHandle ), 0, ulLength,0, &sOlap ); + } + else + { + bResult = UnlockFile( DostoWinHandle( hFileHandle ), ulStart, 0, ulLength,0 ); + } + break; - } - switch( uiMode & FL_MASK ) - { - case FL_LOCK: - { - if ( s_bWinNt ) - { - OVERLAPPED sOlap ; - DWORD dwFlags ; - memset( &sOlap, 0, sizeof( OVERLAPPED ) ) ; - sOlap.Offset = ( ULONG ) ulStart ; - dwFlags = ( uiMode & FLX_SHARED ) ? 0 : LOCKFILE_EXCLUSIVE_LOCK ; - if ( !s_fUseWaitLocks || !( uiMode & FLX_WAIT ) ) - { - dwFlags |= LOCKFILE_FAIL_IMMEDIATELY ; - } - bResult = LockFileEx( DostoWinHandle( hFileHandle ), dwFlags, 0, ulLength, 0, &sOlap ); - } - else - { - bResult = LockFile( DostoWinHandle( hFileHandle ), ulStart, 0, ulLength,0 ); - } - break; - } - case FL_UNLOCK: - { - if ( s_bWinNt ) - { - OVERLAPPED sOlap ; - memset( &sOlap, 0, sizeof( OVERLAPPED ) ) ; - sOlap.Offset = ( ULONG ) ulStart ; - bResult = UnlockFileEx( DostoWinHandle( hFileHandle ), 0, ulLength,0, &sOlap ); - } - else - { - bResult = UnlockFile( DostoWinHandle( hFileHandle ), ulStart, 0, ulLength,0 ); - } - break; - - } - default: - bResult = FALSE; - } + } + default: + bResult = FALSE; } hb_fsSetIOError( bResult, 0 ); #elif defined(HB_OS_OS2) @@ -1437,24 +1428,16 @@ HB_EXPORT BOOL hb_fsLockLarge( FHANDLE hFileHandle, HB_FOFFSET ulStart, dwLengthLo = ( DWORD ) ( ulLength & 0xFFFFFFFF ), dwLengthHi = ( DWORD ) ( ulLength >> 32 ); - static BOOL s_bInit = 0, s_bWinNt ; - - if ( !s_bInit ) - { - s_bInit = TRUE ; - s_bWinNt = hb_iswinnt() ; - } - switch( uiMode & FL_MASK ) { case FL_LOCK: - if ( s_bWinNt ) + if( hb_iswinnt() ) { OVERLAPPED sOlap ; DWORD dwFlags ; dwFlags = ( ( uiMode & FLX_SHARED ) ? 0 : LOCKFILE_EXCLUSIVE_LOCK ); - if ( !s_fUseWaitLocks || !( uiMode & FLX_WAIT ) ) + if( !s_fUseWaitLocks || !( uiMode & FLX_WAIT ) ) { dwFlags |= LOCKFILE_FAIL_IMMEDIATELY ; } @@ -1475,7 +1458,7 @@ HB_EXPORT BOOL hb_fsLockLarge( FHANDLE hFileHandle, HB_FOFFSET ulStart, break; case FL_UNLOCK: - if ( s_bWinNt ) + if( hb_iswinnt() ) { OVERLAPPED sOlap ; @@ -1498,7 +1481,6 @@ HB_EXPORT BOOL hb_fsLockLarge( FHANDLE hFileHandle, HB_FOFFSET ulStart, bResult = FALSE; } hb_fsSetIOError( bResult, 0 ); - } #elif defined(HB_OS_LINUX) && defined(__USE_LARGEFILE64) /* @@ -2301,7 +2283,7 @@ HB_EXPORT FHANDLE hb_fsExtOpen( BYTE * pFilename, BYTE * pDefExt, } if( uiExFlags & FXO_COPYNAME && hFile != FS_ERROR ) - strcpy( ( char * ) pFilename, ( char * ) szPath ); + hb_strncpy( ( char * ) pFilename, ( char * ) szPath, _POSIX_PATH_MAX ); hb_xfree( szPath ); return hFile; diff --git a/harbour/source/rtl/fkmax.c b/harbour/source/rtl/fkmax.c index c0405dc1b9..35b8eb13a0 100644 --- a/harbour/source/rtl/fkmax.c +++ b/harbour/source/rtl/fkmax.c @@ -74,8 +74,7 @@ HB_FUNC( FKLABEL ) { char szName[ 4 ]; - sprintf( szName, "F%i", uiFKey ); - + snprintf( szName, sizeof( szName ), "F%i", uiFKey ); hb_retc( szName ); } else diff --git a/harbour/source/rtl/fstemp.c b/harbour/source/rtl/fstemp.c index aadc22b9b9..87f080d281 100644 --- a/harbour/source/rtl/fstemp.c +++ b/harbour/source/rtl/fstemp.c @@ -151,7 +151,7 @@ static BOOL fsGetTempDirByCase( BYTE *pszName, const char *pszTempDir ) if ( pszTempDir != NULL && *pszTempDir != '\0' ) { bOk = TRUE; - strcpy( ( char * ) pszName, ( char * ) pszTempDir ); + hb_strncpy( ( char * ) pszName, ( char * ) pszTempDir, _POSIX_PATH_MAX ); if ( hb_set.HB_SET_DIRCASE == HB_SET_CASE_LOWER || hb_set.HB_SET_DIRCASE == HB_SET_CASE_UPPER ) { /* check to see if temp directory already upper or lower. If not use current directory ( "." ) */ @@ -187,12 +187,12 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, if( pszDir != NULL && pszDir[0] != '\0' ) { - strcpy( ( char * ) pszName, ( char * ) pszDir ); + hb_strncpy( ( char * ) pszName, ( char * ) pszDir, _POSIX_PATH_MAX ); } else if( !fsGetTempDirByCase( pszName, getenv( "TMPDIR" ) ) && !fsGetTempDirByCase( pszName, P_tmpdir ) ) { - strcpy( ( char * ) pszName, "." ); + hb_strncpy( ( char * ) pszName, ".", _POSIX_PATH_MAX ); } if( pszName[0] != '\0' ) { @@ -204,7 +204,7 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, if ( pszPrefix != NULL ) { - strcat( ( char * ) pszName, ( char * ) pszPrefix ); + hb_strncat( ( char * ) pszName, ( char * ) pszPrefix, _POSIX_PATH_MAX ); } iLen = strlen( ( char * ) pszName ); @@ -234,7 +234,7 @@ HB_EXPORT FHANDLE hb_fsCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, #if !defined(__WATCOMC__) && ( defined( HB_OS_LINUX ) || defined( HB_OS_BSD ) ) else { - strcat( ( char * ) pszName, "XXXXXX" ); + hb_strncat( ( char * ) pszName, "XXXXXX", _POSIX_PATH_MAX ); fd = (FHANDLE) mkstemp( ( char * ) pszName ); hb_fsSetIOError( fd != (FHANDLE) -1, 0 ); } diff --git a/harbour/source/rtl/gtcrs/chrmap.c b/harbour/source/rtl/gtcrs/chrmap.c index 92536fb309..16053cc547 100644 --- a/harbour/source/rtl/gtcrs/chrmap.c +++ b/harbour/source/rtl/gtcrs/chrmap.c @@ -332,8 +332,8 @@ int hb_gt_crs_chrmapinit( int *piTransTbl, char *pszTerm ) if ( pszFile != NULL && sizeof(szFile) > strlen(pszFile) + strlen(s_szDefaultCharMapFile) ) { - strcpy(szFile, pszFile); - strcat(szFile, s_szDefaultCharMapFile); + hb_strncpy(szFile, pszFile, _POSIX_PATH_MAX); + hb_strncat(szFile, s_szDefaultCharMapFile, _POSIX_PATH_MAX); nRet = hb_gt_crs_chrmapread( szFile, pszTerm, piTransTbl ); } } diff --git a/harbour/source/rtl/gtcrs/gtcrs.c b/harbour/source/rtl/gtcrs/gtcrs.c index 775ac4dfd3..82e50c29fc 100644 --- a/harbour/source/rtl/gtcrs/gtcrs.c +++ b/harbour/source/rtl/gtcrs/gtcrs.c @@ -1870,7 +1870,7 @@ static void setDispTrans( InOutBase * ioBase, char *src, char *dst, int box ) ioBase->std_chmap[i] |= ch; ioBase->box_chmap[i] |= ch; - if ( ( unsigned int ) i != ( ch & A_CHARTEXT ) && + if ( i != ( int ) ( ch & A_CHARTEXT ) && ( ioBase->std_chmap[i] & A_ALTCHARSET ) == 0 ) { if ( ioBase->out_transtbl == NULL ) diff --git a/harbour/source/rtl/gtcrs/gtcrs.h b/harbour/source/rtl/gtcrs/gtcrs.h index 884cb76ec3..b0a0faf161 100644 --- a/harbour/source/rtl/gtcrs/gtcrs.h +++ b/harbour/source/rtl/gtcrs/gtcrs.h @@ -101,8 +101,13 @@ #if ( defined( HB_OS_LINUX ) || defined( HB_OS_BSD ) ) && !defined(__WATCOMC__) # if defined( HB_OS_LINUX ) # include /* for openpty and forkpty */ -# elif defined( HB_OS_DARWIN ) +# elif defined( HB_OS_DARWIN ) || defined( __NetBSD__ ) || defined( __OpenBSD__ ) # include /* for openpty and forkpty */ +# if defined( __NetBSD__ ) +# include +# define tigetnum(id) tgetnum(id) +# define tigetstr(id) tgetstr(id,NULL) +# endif # elif defined( HB_OS_BSD ) # include /* for openpty and forkpty */ # endif diff --git a/harbour/source/rtl/gtpca/gtpca.c b/harbour/source/rtl/gtpca/gtpca.c index b89b31be45..1209094705 100644 --- a/harbour/source/rtl/gtpca/gtpca.c +++ b/harbour/source/rtl/gtpca/gtpca.c @@ -284,7 +284,7 @@ static void hb_gt_pca_AnsiSetCursorPos( int iRow, int iCol ) if( s_iRow != iRow || s_iCol != iCol ) { char buff[16]; - sprintf( buff, "\x1B[%d;%dH", iRow + 1, iCol + 1 ); + snprintf( buff, sizeof( buff ), "\x1B[%d;%dH", iRow + 1, iCol + 1 ); hb_gt_pca_termOut( ( BYTE * ) buff, strlen( buff ) ); s_iRow = iRow; s_iCol = iCol; diff --git a/harbour/source/rtl/gtwvt/gtwvt.c b/harbour/source/rtl/gtwvt/gtwvt.c index 36b65bd0ff..4cc6f84a07 100644 --- a/harbour/source/rtl/gtwvt/gtwvt.c +++ b/harbour/source/rtl/gtwvt/gtwvt.c @@ -153,7 +153,7 @@ static void hb_gt_wvt_InitStatics( void ) _s.fontWidth = 10; _s.fontWeight = FW_NORMAL; _s.fontQuality = DEFAULT_QUALITY; - strcpy( _s.fontFace,"Courier New" ); + hb_strncpy( _s.fontFace, "Courier New", sizeof( _s.fontFace ) - 1 ); _s.CentreWindow = TRUE; /* Default is to always display window in centre of screen */ _s.CodePage = OEM_CHARSET; /* GetACP(); - set code page to default system */ @@ -201,7 +201,7 @@ static HFONT hb_gt_wvt_GetFont( char * pszFace, int iHeight, int iWidth, int iWe logfont.lfHeight = iHeight; logfont.lfWidth = iWidth < 0 ? -iWidth : iWidth ; - strcpy( logfont.lfFaceName,pszFace ); + hb_strncpy( logfont.lfFaceName, pszFace, sizeof( logfont.lfFaceName ) - 1 ); hFont = CreateFontIndirect( &logfont ); } diff --git a/harbour/source/rtl/gtxwc/gtxwc.c b/harbour/source/rtl/gtxwc/gtxwc.c index 08a5c0a474..51d145ad30 100644 --- a/harbour/source/rtl/gtxwc/gtxwc.c +++ b/harbour/source/rtl/gtxwc/gtxwc.c @@ -456,7 +456,7 @@ static int s_errorHandler( Display *dpy, XErrorEvent *e ) { char errorText[1024]; - sprintf( errorText, "%s", "Xlib error: " ); + snprintf( errorText, sizeof( errorText ), "%s", "Xlib error: " ); XGetErrorText( dpy, e->error_code, errorText + strlen( errorText ), sizeof(errorText) - strlen( errorText ) ); diff --git a/harbour/source/rtl/hbffind.c b/harbour/source/rtl/hbffind.c index 938f297759..0540abc3ee 100644 --- a/harbour/source/rtl/hbffind.c +++ b/harbour/source/rtl/hbffind.c @@ -635,36 +635,36 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) dirname[ 0 ] = '\0'; info->pattern[ 0 ] = '\0'; -/* strcpy( string, pszFileName ); */ - strcpy( string, ffind->pszFileMask ); + /* hb_strncpy( string, pszFileName, sizeof( string ) - 1 ); */ + hb_strncpy( string, ffind->pszFileMask, sizeof( string ) - 1 ); pos = strrchr( string, OS_PATH_DELIMITER ); if( pos ) { - strcpy( info->pattern, pos + 1 ); + hb_strncpy( info->pattern, pos + 1, sizeof( info->pattern ) - 1 ); *( pos + 1 ) = '\0'; - strcpy( dirname, string ); + hb_strncpy( dirname, string, sizeof( dirname ) - 1 ); } else { - strcpy( info->pattern, string ); - strcpy( dirname, ".X" ); + hb_strncpy( info->pattern, string, sizeof( info->pattern ) - 1 ); + hb_strncpy( dirname, ".X", sizeof( dirname ) - 1 ); dirname[ 1 ] = OS_PATH_DELIMITER; } if( info->pattern[ 0 ] == '\0' ) - strcpy( info->pattern, "*" ); + hb_strncpy( info->pattern, "*", sizeof( info->pattern ) - 1 ); tzset(); info->dir = opendir( dirname ); - strcpy( info->path, dirname ); + hb_strncpy( info->path, dirname, sizeof( info->path ) - 1 ); } if( info->dir != NULL) { while( ( info->entry = readdir( info->dir ) ) != NULL ) { - strcpy( string, info->entry->d_name ); + hb_strncpy( string, info->entry->d_name, sizeof( string ) - 1 ); #if defined( __WATCOMC__ ) if( hb_strMatchWild( string, info->pattern ) ) @@ -684,8 +684,8 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) { struct stat sStat; - strcpy( dirname, info->path ); - strcat( dirname, info->entry->d_name ); + hb_strncpy( dirname, info->path, sizeof( dirname ) - 1 ); + hb_strncat( dirname, info->entry->d_name, sizeof( dirname ) - 1 ); if( stat( dirname, &sStat ) != 0 ) printf("\n%s (%i)", dirname, errno ); @@ -749,7 +749,7 @@ static BOOL hb_fsFindNextLow( PHB_FFIND ffind ) hb_dateStrPut( ffind->szDate, nYear, nMonth, nDay ); ffind->szDate[ 8 ] = '\0'; - sprintf( ffind->szTime, "%02d:%02d:%02d", nHour, nMin, nSec ); + snprintf( ffind->szTime, sizeof( ffind->szTime ), "%02d:%02d:%02d", nHour, nMin, nSec ); } return bFound; diff --git a/harbour/source/rtl/hbgtcore.c b/harbour/source/rtl/hbgtcore.c index 5872673d26..5af682e273 100644 --- a/harbour/source/rtl/hbgtcore.c +++ b/harbour/source/rtl/hbgtcore.c @@ -2751,7 +2751,8 @@ static char * hb_gtFindDefault( void ) for( iPos = 0; iPos < s_iGtCount; iPos++ ) { - sprintf( szFuncName, "HB_GT_%s_DEFAULT", s_gtInit[ iPos ]->id ); + snprintf( szFuncName, sizeof( szFuncName ), + "HB_GT_%s_DEFAULT", s_gtInit[ iPos ]->id ); if( hb_dynsymFind( szFuncName ) ) return s_gtInit[ iPos ]->id; } diff --git a/harbour/source/rtl/langapi.c b/harbour/source/rtl/langapi.c index e979c3840b..1c8c4a0a1f 100644 --- a/harbour/source/rtl/langapi.c +++ b/harbour/source/rtl/langapi.c @@ -337,15 +337,18 @@ char * hb_langID( void ) char * hb_langName( void ) { - char * pszName = ( char * ) hb_xgrab( 128 ); + char * pszName; if( s_lang ) - sprintf( pszName, "Harbour Language: %s %s (%s)", + { + pszName = ( char * ) hb_xgrab( 128 ); + snprintf( pszName, 128, "Harbour Language: %s %s (%s)", ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ID + HB_LANG_ITEM_ID_ID ), ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ID + HB_LANG_ITEM_ID_NAME ), ( char * ) hb_langDGetItem( HB_LANG_ITEM_BASE_ID + HB_LANG_ITEM_ID_NAMENAT ) ); + } else - strcpy( pszName, "Harbour Language: (not installed)" ); + pszName = hb_strdup( "Harbour Language: (not installed)" ); return pszName; } diff --git a/harbour/source/rtl/seconds.c b/harbour/source/rtl/seconds.c index c1cadae9f6..b195f1da8f 100644 --- a/harbour/source/rtl/seconds.c +++ b/harbour/source/rtl/seconds.c @@ -130,16 +130,8 @@ HB_FUNC( HB_CLOCKS2SECS ) HB_EXPORT double hb_secondsCPU( int n ) { double d = 0.0; - #if defined( HB_OS_WIN_32 ) FILETIME Create, Exit, Kernel, User; - static BOOL s_fInit = FALSE, s_fWinNT = FALSE; - - if( !s_fInit ) - { - s_fInit = TRUE ; - s_fWinNT = hb_iswinnt() ; - } #endif if( ( n < 1 || n > 3 ) && ( n < 11 || n > 13 ) ) @@ -172,7 +164,7 @@ HB_EXPORT double hb_secondsCPU( int n ) if( n > 10 ) n -= 10; #if defined( HB_OS_WIN_32 ) - if( s_fWinNT && + if( hb_iswinnt() && GetProcessTimes( GetCurrentProcess(), &Create, &Exit, &Kernel, &User ) ) { if( n & 1 ) diff --git a/harbour/source/rtl/set.c b/harbour/source/rtl/set.c index 07cf875989..60cf1d0f32 100644 --- a/harbour/source/rtl/set.c +++ b/harbour/source/rtl/set.c @@ -388,14 +388,14 @@ HB_FUNC( __SETCENTURY ) int format_len; if( y_start > 0 ) memcpy( szNewFormat, szDateFormat, y_start ); szNewFormat[ y_start ] = '\0'; - strcat( szNewFormat, "YY" ); - if( hb_set.hb_set_century ) strcat( szNewFormat, "YY" ); + hb_strncat( szNewFormat, "YY", size ); + if( hb_set.hb_set_century ) hb_strncat( szNewFormat, "YY", size ); format_len = strlen( szDateFormat ); - if( y_stop < format_len ) strcat( szNewFormat, szDateFormat + y_stop ); - hb_xfree( szDateFormat ); + if( y_stop < format_len ) hb_strncat( szNewFormat, szDateFormat + y_stop, size ); /* DATE FORMAT is under direct control of SET, so notify when it it is changed indirectly via __SETCENTURY() */ hb_setListenerNotify( HB_SET_DATEFORMAT, HB_SET_LISTENER_BEFORE ); + hb_xfree( szDateFormat ); hb_set.HB_SET_DATEFORMAT = szNewFormat; hb_setListenerNotify( HB_SET_DATEFORMAT, HB_SET_LISTENER_AFTER ); } diff --git a/harbour/source/rtl/tobject.prg b/harbour/source/rtl/tobject.prg index a54362b5d3..2e1d9a8485 100644 --- a/harbour/source/rtl/tobject.prg +++ b/harbour/source/rtl/tobject.prg @@ -154,19 +154,19 @@ FUNCTION HBObject() /* Currently limited to 20 param */ /* Will be re-written in C later to avoid this */ -static function HBObject_New(xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; - xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) +static function HBObject_New( xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; + xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) -return QSelf():Init(xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; - xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) +return QSelf():Init( xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; + xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) static function HBObject_Init() return QSelf() -static function HBObject_Dftonerror(xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; - xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) +static function HBObject_Dftonerror( xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; + xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) return QSelf():MSGNOTFOUND( __GetMessage(), xPar0, xPar1, xPar2, xPar3, xPar4, xPar5, xPar6, xPar7, xPar8, xPar9, ; - xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) + xPar10,xPar11,xPar12,xPar13,xPar14,xPar15,xPar16,xPar17,xPar18,xPar19 ) static function HBObject_Error( cDesc, cClass, cMsg, nCode ) @@ -177,5 +177,3 @@ static function HBObject_Error( cDesc, cClass, cMsg, nCode ) ENDIF RETURN __errRT_SBASE( EG_NOMETHOD, nCode, cDesc, cClass + ":" + cMsg, 1, QSelf() ) - - diff --git a/harbour/source/rtl/transfrm.c b/harbour/source/rtl/transfrm.c index 490298554d..2aa7066fd8 100644 --- a/harbour/source/rtl/transfrm.c +++ b/harbour/source/rtl/transfrm.c @@ -395,6 +395,7 @@ HB_FUNC( TRANSFORM ) int iWidth; /* Width of string */ int iDec; /* Number of decimals */ ULONG i; + ULONG ulBufSize; int iCount = 0; char * szStr; @@ -423,7 +424,8 @@ HB_FUNC( TRANSFORM ) } /* TODO: maybe replace this 16 with something else */ - szResult = ( char * ) hb_xgrab( ulPicLen + (ULONG) iOrigWidth + (ULONG) iOrigDec + 16 ); /* Grab enough */ + ulBufSize = ulPicLen + (ULONG) iOrigWidth + (ULONG) iOrigDec + 16; + szResult = ( char * ) hb_xgrab( ulBufSize ); /* Grab enough */ *szResult = '\0'; for( i = 0; i < ulPicLen && !bFound; i++ ) /* Count number in front */ @@ -570,8 +572,8 @@ HB_FUNC( TRANSFORM ) } else { - strcpy( szResult, szStr ); - i = strlen( szStr ); + hb_strncpy( szResult, szStr, ulBufSize - 1 ); + i = strlen( szResult ); } if( ( uiPicFlags & PF_PARNEG ) && dValue < 0 && !( uiPicFlags & PF_PARNEGWOS ) ) diff --git a/harbour/source/vm/classes.c b/harbour/source/vm/classes.c index dd3db7432b..94e3d6a671 100644 --- a/harbour/source/vm/classes.c +++ b/harbour/source/vm/classes.c @@ -1892,7 +1892,21 @@ static HB_TYPE hb_clsGetItemType( PHB_ITEM pItem ) case 'C': case 'c': case '\0': - return HB_IT_STRING; + if( hb_strnicmp( hb_itemGetCPtr( pItem ), "code", 4 ) == 0 ) + return HB_IT_BLOCK; + else + return HB_IT_STRING; + + case 'S': + case 's': + if( hb_strnicmp( hb_itemGetCPtr( pItem ), "str", 3 ) == 0 ) + return HB_IT_STRING; + else + return HB_IT_SYMBOL; + + case 'B': + case 'b': + return HB_IT_BLOCK; case 'D': case 'd': @@ -1914,20 +1928,9 @@ static HB_TYPE hb_clsGetItemType( PHB_ITEM pItem ) case 'a': return HB_IT_ARRAY; - case 'B': - case 'b': - return HB_IT_BLOCK; - case 'P': case 'p': return HB_IT_POINTER; - - case 'S': - case 's': - if( hb_strnicmp( hb_itemGetCPtr( pItem ), "str", 3 ) == 0 ) - return HB_IT_STRING; - else - return HB_IT_SYMBOL; } } else if( HB_IS_ARRAY( pItem ) ) @@ -3503,12 +3506,12 @@ static HARBOUR hb___msgNoMethod( void ) if( pSym->szName[ 0 ] == '_' ) { - sprintf( szDesc, "Class: '%s' has no property", hb_objGetClsName( hb_stackSelfItem() ) ); + snprintf( szDesc, sizeof( szDesc ), "Class: '%s' has no property", hb_objGetClsName( hb_stackSelfItem() ) ); hb_errRT_BASE_SubstR( EG_NOVARMETHOD, 1005, szDesc, pSym->szName + 1, HB_ERR_ARGS_BASEPARAMS ); } else { - sprintf( szDesc, "Class: '%s' has no exported method", hb_objGetClsName( hb_stackSelfItem() ) ); + snprintf( szDesc, sizeof( szDesc ), "Class: '%s' has no exported method", hb_objGetClsName( hb_stackSelfItem() ) ); hb_errRT_BASE_SubstR( EG_NOMETHOD, 1004, szDesc, pSym->szName, HB_ERR_ARGS_BASEPARAMS ); } #endif @@ -3521,36 +3524,35 @@ static HARBOUR hb___msgNoMethod( void ) */ static HARBOUR hb___msgScopeErr( void ) { - char szProcName[ HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 5 ]; + char * pszProcName; PHB_ITEM pObject = hb_stackSelfItem(); PMETHOD pMethod = s_pClasses[ hb_stackBaseItem()->item.asSymbol.stackstate->uiClass ].pMethods + hb_stackBaseItem()->item.asSymbol.stackstate->uiMethod; - strcpy( szProcName, s_pClasses[ - pObject->item.asArray.value->uiClass ].szName ); - strcat( szProcName, ":" ); - strcat( szProcName, pMethod->pMessage->pSymbol->szName ); - + pszProcName = hb_xstrcpy( NULL, + s_pClasses[ pObject->item.asArray.value->uiClass ].szName, + ":", pMethod->pMessage->pSymbol->szName, NULL ); if( pMethod->uiScope & HB_OO_CLSTP_HIDDEN ) - hb_errRT_BASE( EG_NOMETHOD, 41, "Scope violation (hidden)", szProcName, 0 ); + hb_errRT_BASE( EG_NOMETHOD, 41, "Scope violation (hidden)", pszProcName, 0 ); else - hb_errRT_BASE( EG_NOMETHOD, 42, "Scope violation (protected)", szProcName, 0 ); + hb_errRT_BASE( EG_NOMETHOD, 42, "Scope violation (protected)", pszProcName, 0 ); + hb_xfree( pszProcName ); } static HARBOUR hb___msgTypeErr( void ) { - char szProcName[ HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 5 ]; + char * pszProcName; PHB_ITEM pObject = hb_stackSelfItem(); PMETHOD pMethod = s_pClasses[ hb_stackBaseItem()->item.asSymbol.stackstate->uiClass ].pMethods + hb_stackBaseItem()->item.asSymbol.stackstate->uiMethod; - strcpy( szProcName, s_pClasses[ - pObject->item.asArray.value->uiClass ].szName ); - strcat( szProcName, ":" ); - strcat( szProcName, pMethod->pMessage->pSymbol->szName + 1 ); - hb_errRT_BASE( EG_NOMETHOD, 44, "Assigned value is wrong class", szProcName, HB_ERR_ARGS_BASEPARAMS ); + pszProcName = hb_xstrcpy( NULL, + s_pClasses[ pObject->item.asArray.value->uiClass ].szName, + ":", pMethod->pMessage->pSymbol->szName + 1, NULL ); + hb_errRT_BASE( EG_NOMETHOD, 44, "Assigned value is wrong class", pszProcName, HB_ERR_ARGS_BASEPARAMS ); + hb_xfree( pszProcName ); } /* diff --git a/harbour/source/vm/cmdarg.c b/harbour/source/vm/cmdarg.c index c207d85831..3b926a4c51 100644 --- a/harbour/source/vm/cmdarg.c +++ b/harbour/source/vm/cmdarg.c @@ -327,8 +327,8 @@ ULONG hb_cmdargProcessVM( int *pCancelKey, int *pCancelKeyEx ) { char buffer[ 128 ]; - /* sprintf( buffer, "DS avail=%luKB OS avail=%luKB EMM avail=%luKB", hb_xquery( HB_MEM_BLOCK ), hb_xquery( HB_MEM_VM ), hb_xquery( HB_MEM_EMS ) ); */ - sprintf( buffer, "DS avail=%luKB OS avail=%luKB EMM avail=%luKB MemStat:%s", hb_xquery( HB_MEM_BLOCK ), hb_xquery( HB_MEM_VM ), hb_xquery( HB_MEM_EMS ), hb_xquery( HB_MEM_USEDMAX ) ? "On" : "Off" ); + /* snprintf( buffer, sizeof( buffer ), "DS avail=%luKB OS avail=%luKB EMM avail=%luKB", hb_xquery( HB_MEM_BLOCK ), hb_xquery( HB_MEM_VM ), hb_xquery( HB_MEM_EMS ) ); */ + snprintf( buffer, sizeof( buffer ), "DS avail=%luKB OS avail=%luKB EMM avail=%luKB MemStat:%s", hb_xquery( HB_MEM_BLOCK ), hb_xquery( HB_MEM_VM ), hb_xquery( HB_MEM_EMS ), hb_xquery( HB_MEM_USEDMAX ) ? "On" : "Off" ); hb_conOutErr( buffer, 0 ); hb_conOutErr( hb_conNewLine(), 0 ); } diff --git a/harbour/source/vm/debug.c b/harbour/source/vm/debug.c index ca0179e522..dfdff34027 100644 --- a/harbour/source/vm/debug.c +++ b/harbour/source/vm/debug.c @@ -70,9 +70,9 @@ static void AddToArray( PHB_ITEM pItem, PHB_ITEM pReturn, ULONG ulPos ) if( pArrayItem ) { ULONG ulLen = strlen( pItem->item.asSymbol.value->szName ) + 2; - char * szBuff = ( char * ) hb_xgrab( ulLen ); + char * szBuff = ( char * ) hb_xgrab( ulLen + 1 ); - sprintf( szBuff, "[%s]", pItem->item.asSymbol.value->szName ); + snprintf( szBuff, ulLen + 1, "[%s]", pItem->item.asSymbol.value->szName ); hb_itemPutCPtr( pArrayItem, szBuff, ulLen ); } } diff --git a/harbour/source/vm/estack.c b/harbour/source/vm/estack.c index d997e5e742..f66df2bd5b 100644 --- a/harbour/source/vm/estack.c +++ b/harbour/source/vm/estack.c @@ -460,11 +460,13 @@ void hb_stackBaseProcInfo( char * szProcName, USHORT * puiProcLine ) { /* * This function is called by FM module and has to be ready for execution - * before stack initialization, [druzus]; + * before hb_stack initialization, [druzus] + * szProcName should be at least HB_SYMBOL_NAME_LEN + 1 bytes buffer */ if( hb_stack.pPos > hb_stack.pBase ) { - strcpy( szProcName, ( * hb_stack.pBase )->item.asSymbol.value->szName ); + hb_strncpy( szProcName, ( * hb_stack.pBase )->item.asSymbol.value->szName, + HB_SYMBOL_NAME_LEN ); * puiProcLine = ( * hb_stack.pBase )->item.asSymbol.stackstate->uiLineNo; } else @@ -562,13 +564,13 @@ void hb_stackDispCall( void ) char buffer[ HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 32 ]; if( HB_IS_OBJECT( *( pBase + 1 ) ) ) - sprintf( buffer, HB_I_("Called from %s:%s(%i)"), hb_objGetClsName( *(pBase + 1) ), - ( *pBase )->item.asSymbol.value->szName, - ( *pBase )->item.asSymbol.stackstate->uiLineNo ); + snprintf( buffer, sizeof( buffer ), HB_I_("Called from %s:%s(%i)"), hb_objGetClsName( *(pBase + 1) ), + ( *pBase )->item.asSymbol.value->szName, + ( *pBase )->item.asSymbol.stackstate->uiLineNo ); else - sprintf( buffer, HB_I_("Called from %s(%i)"), - ( *pBase )->item.asSymbol.value->szName, - ( *pBase )->item.asSymbol.stackstate->uiLineNo ); + snprintf( buffer, sizeof( buffer ), HB_I_("Called from %s(%i)"), + ( *pBase )->item.asSymbol.value->szName, + ( *pBase )->item.asSymbol.stackstate->uiLineNo ); hb_conOutErr( buffer, 0 ); hb_conOutErr( hb_conNewLine(), 0 ); @@ -621,15 +623,15 @@ LONG WINAPI hb_UnhandledExceptionFilter( struct _EXCEPTION_POINTERS * ExceptionI char buffer[ HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 32 ]; if( HB_IS_OBJECT( *( pBase + 1 ) ) ) - sprintf( buffer, HB_I_("Called from %s:%s(%i)\n"), hb_objGetClsName( *(pBase + 1) ), - ( *pBase )->item.asSymbol.value->szName, - ( *pBase )->item.asSymbol.stackstate->uiLineNo ); + snprintf( buffer, sizeof( buffer ), HB_I_("Called from %s:%s(%i)\n"), hb_objGetClsName( *(pBase + 1) ), + ( *pBase )->item.asSymbol.value->szName, + ( *pBase )->item.asSymbol.stackstate->uiLineNo ); else - sprintf( buffer, HB_I_("Called from %s(%i)\n"), - ( *pBase )->item.asSymbol.value->szName, - ( *pBase )->item.asSymbol.stackstate->uiLineNo ); + snprintf( buffer, sizeof( buffer ), HB_I_("Called from %s(%i)\n"), + ( *pBase )->item.asSymbol.value->szName, + ( *pBase )->item.asSymbol.stackstate->uiLineNo ); - strcat( msg, buffer ); + hb_strncat( msg, buffer, sizeof( msg ) - 1 ); pBase = hb_stack.pItems + ( *pBase )->item.asSymbol.stackstate->lBaseItem; } diff --git a/harbour/source/vm/fm.c b/harbour/source/vm/fm.c index ba479db928..4f6735cd65 100644 --- a/harbour/source/vm/fm.c +++ b/harbour/source/vm/fm.c @@ -197,7 +197,7 @@ HB_EXPORT void * hb_xalloc( ULONG ulSize ) /* allocates fixed memory, re * hb_xalloc/hb_xgrab */ pMem->uiProcLine = hb_tr_line_; /* C line number */ - strcpy( pMem->szProcName, hb_tr_file_ ); + hb_strncpy( pMem->szProcName, hb_tr_file_, HB_SYMBOL_NAME_LEN ); } else { @@ -262,7 +262,7 @@ HB_EXPORT void * hb_xgrab( ULONG ulSize ) /* allocates fixed memory, exi * hb_xalloc/hb_xgrab */ pMem->uiProcLine = hb_tr_line_; /* C line number */ - strcpy( pMem->szProcName, hb_tr_file_ ); + hb_strncpy( pMem->szProcName, hb_tr_file_, HB_SYMBOL_NAME_LEN ); } else { @@ -577,7 +577,7 @@ HB_EXPORT void hb_xexit( void ) /* Deinitialize fixed memory subsystem */ hb_conOutErr( hb_conNewLine(), 0 ); hb_conOutErr( "----------------------------------------", 0 ); hb_conOutErr( hb_conNewLine(), 0 ); - sprintf( buffer, "Total memory allocated: %li bytes (%li blocks)", s_lMemoryMaxConsumed, s_lMemoryMaxBlocks ); + snprintf( buffer, sizeof( buffer ), "Total memory allocated: %li bytes (%li blocks)", s_lMemoryMaxConsumed, s_lMemoryMaxBlocks ); hb_conOutErr( buffer, 0 ); if ( hLog ) @@ -596,7 +596,7 @@ HB_EXPORT void hb_xexit( void ) /* Deinitialize fixed memory subsystem */ if( s_lMemoryBlocks ) { hb_conOutErr( hb_conNewLine(), 0 ); - sprintf( buffer, "WARNING! Memory allocated but not released: %li bytes (%li blocks)", s_lMemoryConsumed, s_lMemoryBlocks ); + snprintf( buffer, sizeof( buffer ), "WARNING! Memory allocated but not released: %li bytes (%li blocks)", s_lMemoryConsumed, s_lMemoryBlocks ); hb_conOutErr( buffer, 0 ); if ( hLog ) fprintf( hLog, "%s\n", buffer ); diff --git a/harbour/source/vm/hvm.c b/harbour/source/vm/hvm.c index a2b994c9f0..20467fc5ec 100644 --- a/harbour/source/vm/hvm.c +++ b/harbour/source/vm/hvm.c @@ -5896,7 +5896,7 @@ HB_EXPORT PHB_SYMB hb_vmProcessSymbolsEx( PHB_SYMB pSymbols, USHORT uiModuleSymb uiPCodeVer < HB_PCODE_VER_MIN ) /* the module is compiled with olde compiler version */ { char szPCode[ 10 ]; - sprintf( szPCode, "%i.%i", uiPCodeVer>>8, uiPCodeVer &0xff ); + snprintf( szPCode, sizeof( szPCode ), "%i.%i", uiPCodeVer>>8, uiPCodeVer &0xff ); hb_errInternal( HB_EI_ERRUNRECOV, "Module '%s'\n" "was compiled with unsupported PCODE version %s.\n" @@ -6170,7 +6170,7 @@ void hb_vmRequestCancel( void ) { char buffer[ HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 5 + 10 ]; /* additional 10 bytes for line info (%hu) overhead */ USHORT uiLine; - int i = 0; + int i = 0, l; hb_conOutErr( hb_conNewLine(), 0 ); hb_conOutErr( "Cancelled at: ", 0 ); @@ -6178,7 +6178,8 @@ void hb_vmRequestCancel( void ) do { - sprintf( buffer + strlen( buffer ), " (%hu)", uiLine ); + l = strlen( buffer ); + snprintf( buffer + l, sizeof( buffer ) - l, " (%hu)", uiLine ); hb_conOutErr( buffer, 0 ); hb_conOutErr( hb_conNewLine(), 0 ); diff --git a/harbour/source/vm/macro.c b/harbour/source/vm/macro.c index 945854417a..4429b9f50f 100644 --- a/harbour/source/vm/macro.c +++ b/harbour/source/vm/macro.c @@ -1187,11 +1187,7 @@ void hb_compGenMessageData( char * szMsg, BOOL bIsObject, HB_COMP_DECL ) HB_TRACE(HB_TR_DEBUG, ("hb_compGenMessageData(%s)", szMsg)); - szResult = ( char * ) hb_xgrab( strlen( szMsg ) + 2 ); - - strcpy( szResult, "_" ); - strcat( szResult, szMsg ); - + szResult = hb_xstrcpy( NULL, "_", szMsg, NULL ); hb_compGenMessage( szResult, bIsObject, HB_COMP_PARAM ); hb_xfree( szResult ); } diff --git a/harbour/source/vm/proc.c b/harbour/source/vm/proc.c index 6ed82a4f7d..41f46eb302 100644 --- a/harbour/source/vm/proc.c +++ b/harbour/source/vm/proc.c @@ -139,7 +139,7 @@ HB_FUNC( PROCFILE ) /* NOTE: szName size must be an at least: HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 5 [vszakats] */ - +#define HB_PROCBUF_LEN ( HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 4 ) char * hb_procname( int iLevel, char * szName, BOOL fMethodName ) { long lOffset = hb_stackBaseProcOffset( iLevel ); @@ -171,22 +171,24 @@ char * hb_procname( int iLevel, char * szName, BOOL fMethodName ) if( pBase->item.asSymbol.value == &hb_symEval || pBase->item.asSymbol.value->pDynSym == hb_symEval.pDynSym ) { - strcat( szName, "(b)" ); + hb_strncat( szName, "(b)", HB_PROCBUF_LEN ); if( HB_IS_BLOCK( pSelf ) ) - strcat( szName, pSelf->item.asBlock.value->pDefSymb->szName ); + hb_strncat( szName, pSelf->item.asBlock.value->pDefSymb->szName, + HB_PROCBUF_LEN ); else - strcat( szName, pBase->item.asSymbol.value->szName ); + hb_strncat( szName, pBase->item.asSymbol.value->szName, HB_PROCBUF_LEN ); } else { /* it is a method name? */ if( pBase->item.asSymbol.stackstate->uiClass ) { - strcat( szName, hb_clsName( pBase->item.asSymbol.stackstate->uiClass ) ); - strcat( szName, ":" ); + hb_strncat( szName, hb_clsName( pBase->item.asSymbol.stackstate->uiClass ), + HB_PROCBUF_LEN ); + hb_strncat( szName, ":", HB_PROCBUF_LEN ); } - strcat( szName, pBase->item.asSymbol.value->szName ); + hb_strncat( szName, pBase->item.asSymbol.value->szName, HB_PROCBUF_LEN ); } } @@ -217,21 +219,23 @@ BOOL hb_procinfo( int iLevel, char * szName, USHORT * puiLine, char * szFile ) szName[ 0 ] = '\0'; if( pSym == &hb_symEval || pSym->pDynSym == hb_symEval.pDynSym ) { - strcat( szName, "(b)" ); + hb_strncat( szName, "(b)", HB_PROCBUF_LEN ); if( HB_IS_BLOCK( pSelf ) ) - strcat( szName, pSelf->item.asBlock.value->pDefSymb->szName ); + hb_strncat( szName, pSelf->item.asBlock.value->pDefSymb->szName, + HB_PROCBUF_LEN ); else - strcat( szName, pSym->szName ); + hb_strncat( szName, pSym->szName, HB_PROCBUF_LEN ); } else { if( pBase->item.asSymbol.stackstate->uiClass ) /* it is a method name */ { - strcat( szName, hb_clsName( pBase->item.asSymbol.stackstate->uiClass ) ); - strcat( szName, ":" ); + hb_strncat( szName, hb_clsName( pBase->item.asSymbol.stackstate->uiClass ), + HB_PROCBUF_LEN ); + hb_strncat( szName, ":", HB_PROCBUF_LEN ); } - strcat( szName, pSym->szName ); + hb_strncat( szName, pSym->szName, HB_PROCBUF_LEN ); } } @@ -249,7 +253,7 @@ BOOL hb_procinfo( int iLevel, char * szName, USHORT * puiLine, char * szFile ) szModule = hb_vmFindModuleSymbolName( hb_vmGetRealFuncSym( pSym ) ); if( szModule ) - strcpy( szFile, szModule ); + hb_strncpy( szFile, szModule, _POSIX_PATH_MAX ); else szFile[ 0 ] = '\0'; } diff --git a/harbour/source/vm/runner.c b/harbour/source/vm/runner.c index 9badcf8269..829c8a2539 100644 --- a/harbour/source/vm/runner.c +++ b/harbour/source/vm/runner.c @@ -72,7 +72,6 @@ #include "hb_io.h" /* TODO: Fill the error codes with valid ones (instead of 9999) */ -/* TOFIX: Fix the memory leak on error. */ typedef struct { diff --git a/harbour/utils/hbmake/hbmlang.c b/harbour/utils/hbmake/hbmlang.c index c1c1b24b5c..5ac0faf3c2 100644 --- a/harbour/utils/hbmake/hbmlang.c +++ b/harbour/utils/hbmake/hbmlang.c @@ -281,8 +281,7 @@ static void hb_gaugeUpdate( PHB_ITEM pArray, float fPercent ) if( hb_arrayGetL( pArray, B_DISPLAYNUM ) ) { -// sprintf( szPct, "%3.0f\%", fPercent * 100 ); - sprintf( szPct, "%3.0f%%", fPercent * 100 ); + snprintf( szPct, sizeof( szPct ), "%3.0f%%", fPercent * 100 ); hb_gtWriteAt( (USHORT) hb_arrayGetNL( pArray, B_TOP ), (USHORT) iCenter + 2, (BYTE *) szPct, 4 ); } diff --git a/harbour/utils/hbpp/hbpp.c b/harbour/utils/hbpp/hbpp.c index 8c85bf3fe0..f45fce595e 100644 --- a/harbour/utils/hbpp/hbpp.c +++ b/harbour/utils/hbpp/hbpp.c @@ -149,8 +149,7 @@ int main( int argc, char * argv[] ) szDefText[ pAssign - szDefText ] = '\0'; //hb_pp_AddDefine_( szDefText, pAssign + 1 ); - sDefLine = (char*) hb_xgrab( strlen( szDefText ) + 1 + strlen( pAssign + 1 ) + 1 ); - sprintf( sDefLine, "%s %s", szDefText, pAssign + 1 ); + sDefLine = hb_xstrcpy( NULL, szDefText, " ", pAssign + 1, NULL ); hb_pp_ParseDefine_( sDefLine ); hb_xfree( sDefLine ); } diff --git a/harbour/utils/hbpp/hbppcomp.c b/harbour/utils/hbpp/hbppcomp.c index 70c8346548..bd3b774a94 100644 --- a/harbour/utils/hbpp/hbppcomp.c +++ b/harbour/utils/hbpp/hbppcomp.c @@ -273,7 +273,7 @@ int hb_pp_Internal_( FILE * handl_o, char * sOut ) if( hb_comp_files.iFiles == 1 ) hb_pp_LastOutLine = hb_comp_iLine; - sprintf( ptrOut, "#line %d \"%s\"", ( hb_comp_files.pLast->iLine ) , hb_comp_files.pLast->szFileName ); + snprintf( ptrOut, HB_PP_STR_SIZE, "#line %d \"%s\"", ( hb_comp_files.pLast->iLine ) , hb_comp_files.pLast->szFileName ); while( *ptrOut ) ptrOut++; @@ -347,13 +347,14 @@ static void pp_ParseBuffer( PFILE pFile, int *plLine ) if( *plLine ) { - sprintf( s_szLine, "#line %d \"%s\"\n", pFile->iLine, pFile->szFileName ); + snprintf( s_szLine, HB_PP_STR_SIZE, "#line %d \"%s\"\n", pFile->iLine, pFile->szFileName ); *plLine = 0; } else *s_szLine = '\0'; - sprintf( s_szLine + strlen(s_szLine), "#line 1 \"%s\"", hb_comp_files.pLast->szFileName ); + snprintf( s_szLine + strlen(s_szLine), HB_PP_STR_SIZE - strlen(s_szLine), + "#line 1 \"%s\"", hb_comp_files.pLast->szFileName ); bCont = FALSE; } else if( bIgnore ) @@ -431,7 +432,7 @@ static void pp_TextBlockFinish( void ) hb_pp_StreamBlock = 0; if( s_TextEndFunc ) { - strcpy( s_szLine, s_TextEndFunc ); + hb_strncpy( s_szLine, s_TextEndFunc, sizeof( s_szLine ) - 1 ); hb_xfree( (void *)s_TextEndFunc ); s_TextEndFunc = NULL; } @@ -523,16 +524,16 @@ static void pp_StreamBlockFinish( void ) s_szLine[ 0 ] = '\0'; if( s_TextStartFunc ) { - strcat( s_szLine, s_TextStartFunc ); + hb_strncat( s_szLine, s_TextStartFunc, HB_PP_STR_SIZE - 1 ); hb_xfree( (void *)s_TextStartFunc ); s_TextStartFunc = NULL; } - strcat( s_szLine, "[" ); - strcat( s_szLine, s_szOutLine ); - strcat( s_szLine, "]" ); + hb_strncat( s_szLine, "[", HB_PP_STR_SIZE - 1 ); + hb_strncat( s_szLine, s_szOutLine, HB_PP_STR_SIZE - 1 ); + hb_strncat( s_szLine, "]", HB_PP_STR_SIZE - 1 ); if( s_TextEndFunc ) { - strcat( s_szLine, s_TextEndFunc ); + hb_strncat( s_szLine, s_TextEndFunc, HB_PP_STR_SIZE - 1 ); hb_xfree( (void *)s_TextEndFunc ); s_TextEndFunc = NULL; } diff --git a/harbour/utils/hbpp/hbppcore.c b/harbour/utils/hbpp/hbppcore.c index 35f47f6d66..aeb20e3c1f 100644 --- a/harbour/utils/hbpp/hbppcore.c +++ b/harbour/utils/hbpp/hbppcore.c @@ -395,7 +395,7 @@ void hb_pp_Init( void ) char *szPlatform = hb_verPlatform( ); int n; - strcpy( sOS, "__PLATFORM__" ); + hb_strncpy( sOS, "__PLATFORM__", sizeof( sOS ) - 1 ); pSrc = szPlatform; n = strlen( sOS ); @@ -424,7 +424,7 @@ void hb_pp_Init( void ) hb_pp_AddDefine_( sOS, sVer ); #ifdef HB_OS_UNIX - strcpy( &sOS[12], "UNIX" ); + hb_strncpy( &sOS[12], "UNIX", sizeof( sOS ) - 13 ); hb_pp_AddDefine_( sOS, sVer ); #endif hb_xfree( szPlatform ); @@ -441,7 +441,7 @@ void hb_pp_Init( void ) The check below is to ensure that __HARBOUR__ gets the value of 1 by default */ - sprintf( szResult, "%05d", ( usHarbour ? usHarbour : 1 ) ); + snprintf( szResult, sizeof( szResult ), "%05d", ( usHarbour ? usHarbour : 1 ) ); hb_pp_AddDefine_( "__HARBOUR__", szResult ); } @@ -453,17 +453,17 @@ void hb_pp_Init( void ) time( &t ); oTime = localtime( &t ); - sprintf( szResult, "\"%04d%02d%02d\"", oTime->tm_year + 1900, oTime->tm_mon + 1, oTime->tm_mday ); + snprintf( szResult, sizeof( szResult ), "\"%04d%02d%02d\"", oTime->tm_year + 1900, oTime->tm_mon + 1, oTime->tm_mday ); hb_pp_AddDefine_( "__DATE__", szResult ); - sprintf( szResult, "\"%02d:%02d:%02d\"", oTime->tm_hour, oTime->tm_min, oTime->tm_sec ); + snprintf( szResult, sizeof( szResult ), "\"%02d:%02d:%02d\"", oTime->tm_hour, oTime->tm_min, oTime->tm_sec ); hb_pp_AddDefine_( "__TIME__", szResult ); } { char szResult[11]; - sprintf( szResult, "%d", ( int ) sizeof( void * ) ); + snprintf( szResult, sizeof( szResult ), "%d", ( int ) sizeof( void * ) ); #if defined( HB_ARCH_16BIT ) hb_pp_AddDefine_( "__ARCH16BIT__", szResult ); #elif defined( HB_ARCH_32BIT ) @@ -658,7 +658,7 @@ int hb_pp_ParseDefine_( char *sLine ) cParams[iParLen] = '\0'; } cParams[iParLen] = '\001'; - strcpy( cParams + iParLen + 1, pars ); + memcpy( cParams + iParLen + 1, pars, iLen + 1 ); iParLen += iLen + 1; npars++; HB_SKIPTABSPACES( sLine ); @@ -1461,7 +1461,7 @@ int hb_pp_ParseExpression( char *sLine, char *sOutLine, BOOL bSplitLines ) { int bIgnore; - strcpy( rpatt, ptri ); + hb_strncpy( rpatt, ptri, PATTERN_SIZE - 1 ); bIgnore = hb_pp_ParseDirective_( rpatt ); if( ipos > 0 ) diff --git a/harbour/utils/hbpp/hbpplib.c b/harbour/utils/hbpp/hbpplib.c index a1180561a1..7d4338c3f4 100644 --- a/harbour/utils/hbpp/hbpplib.c +++ b/harbour/utils/hbpp/hbpplib.c @@ -230,7 +230,7 @@ void hb_compGenError( HB_COMP_DECL, char * szErrors[], char cPrefix, int iError, /* TOFIX: The internal buffers allocated by the preprocessor should be deallocated here */ - sprintf( buffer, szErrors[ iError - 1 ], szError1, szError2 ); + snprintf( buffer, sizeof( buffer ), szErrors[ iError - 1 ], szError1, szError2 ); pError = hb_errRT_New( ES_ERROR, "PP", 9999, ( ULONG ) iError, buffer, NULL, 0, EF_NONE | EF_CANDEFAULT ); hb_errLaunch( pError ); hb_errRelease( pError ); diff --git a/harbour/utils/hbpp/pragma.c b/harbour/utils/hbpp/pragma.c index 9366c3c64d..d4269eacac 100644 --- a/harbour/utils/hbpp/pragma.c +++ b/harbour/utils/hbpp/pragma.c @@ -246,16 +246,16 @@ BOOL hb_pp_ParsePragma( char * szLine ) } } + iSize = strlen( (char*) sBuffer ); if( pInline->pCode == NULL ) { - pInline->pCode = (BYTE *) hb_xgrab( ( iSize = strlen( (char*) sBuffer ) ) + 1 ); - strcpy( (char*) pInline->pCode, (char*) sBuffer ); + pInline->pCode = (BYTE *) hb_xgrab( iSize + 1 ); } else { - pInline->pCode = (BYTE *) hb_xrealloc( pInline->pCode, pInline->lPCodeSize + ( iSize = strlen( (char*) sBuffer ) ) + 1 ); - strcpy( (char *) (pInline->pCode + pInline->lPCodeSize), (char*) sBuffer ); + pInline->pCode = (BYTE *) hb_xrealloc( pInline->pCode, pInline->lPCodeSize + iSize + 1 ); } + memcpy( pInline->pCode + pInline->lPCodeSize, sBuffer, iSize + 1 ); pInline->lPCodeSize += iSize; goto DigestInline; diff --git a/harbour/utils/hbver/hbverfix.c b/harbour/utils/hbver/hbverfix.c index d6714d11f5..53bb5b8b81 100644 --- a/harbour/utils/hbver/hbverfix.c +++ b/harbour/utils/hbver/hbverfix.c @@ -78,7 +78,7 @@ static char * szIncrementNumber( char * szBuffer, size_t stSkipOver ) char szIntBuf[ 8 ]; size_t stStart = i; iVersion = atoi( &szBuffer[ stStart ] ) + 1; - sprintf( szIntBuf, "%-7d", iVersion ); + snprintf( szIntBuf, sizeof( szIntBuf ), "%-7d", iVersion ); for( i = 0; i < strlen( szIntBuf ); i++ ) { szBuffer[ stStart + i ] = szIntBuf[ i ]; @@ -204,7 +204,7 @@ int main( int argc, char * argv[] ) BOOL bFoundLog = FALSE; FILE * fhChangeLog; - sprintf( szErrBuf, "Opening %s", cszChangeLogName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Opening %s", cszChangeLogName ); fhChangeLog = fopen( cszChangeLogName, "rt" ); if( fhChangeLog == NULL ) { @@ -220,7 +220,7 @@ int main( int argc, char * argv[] ) } while( ! ( bFoundID && bFoundLog ) && ! feof( fhChangeLog ) ) { - sprintf( szErrBuf, "Reading from %s", cszChangeLogName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Reading from %s", cszChangeLogName ); fgets( szInputBuffer, MAX_BUF_LEN, fhChangeLog ); if( ferror( fhChangeLog ) ) { @@ -275,14 +275,14 @@ int main( int argc, char * argv[] ) FILE * fhRewrite; FILE * fhVersion; - sprintf( szErrBuf, "Opening %s", cszVersionName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Opening %s", cszVersionName ); fhVersion = fopen( cszVersionName, "rt" ); if( fhVersion == NULL ) { perror( szErrBuf ); return 9; } - sprintf( szErrBuf, "Creating %s", cszRewriteName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Creating %s", cszRewriteName ); fhRewrite = fopen( cszRewriteName, "wt" ); if( fhRewrite == NULL ) { @@ -291,7 +291,7 @@ int main( int argc, char * argv[] ) } while( ! feof( fhVersion ) ) { - sprintf( szErrBuf, "Reading from %s", cszVersionName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Reading from %s", cszVersionName ); fgets( szInputBuffer, MAX_BUF_LEN, fhVersion ); if( iDebugLevel > 0 ) fprintf( stderr, "\n==> %u <==> %s <==", ( unsigned ) strlen( szInputBuffer ), szInputBuffer ); if( ferror( fhVersion ) ) @@ -327,7 +327,7 @@ int main( int argc, char * argv[] ) } if( !feof( fhVersion ) ) { - sprintf( szErrBuf, "Writing to %s", cszRewriteName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Writing to %s", cszRewriteName ); fputs( szInputBuffer, fhRewrite ); if( ferror( fhRewrite ) ) { @@ -338,13 +338,13 @@ int main( int argc, char * argv[] ) } fclose( fhVersion ); fclose( fhRewrite ); - sprintf( szErrBuf, "Deleting %s", cszVersionName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Deleting %s", cszVersionName ); if( remove( cszVersionName ) ) { perror( szErrBuf ); return 11; } - sprintf( szErrBuf, "Renaming %s to %s", cszRewriteName, cszVersionName ); + snprintf( szErrBuf, sizeof( szErrBuf ), "Renaming %s to %s", cszRewriteName, cszVersionName ); if( rename( cszRewriteName, cszVersionName ) ) { perror( szErrBuf );