From e2531e837774276f6ffb3b284703c594644761f8 Mon Sep 17 00:00:00 2001 From: burkphil Date: Sun, 21 Nov 2010 19:11:30 +0000 Subject: [PATCH] Add support for WATCOMC Add size checks for C to Forth string conversion --- csrc/pf_cglue.c | 2 +- csrc/pf_core.c | 10 +++ csrc/pf_core.h | 3 +- csrc/pf_inner.c | 4 +- csrc/pf_io.h | 2 +- csrc/pf_main.c | 8 +++ csrc/pf_text.c | 84 ++++++++++++++++++++++-- csrc/pf_text.h | 8 ++- csrc/pfcompil.c | 6 +- csrc/win32/pf_io_win32.c | 10 ++- csrc/win32_console/pf_io_win32_console.c | 2 +- releases.txt | 6 +- 12 files changed, 125 insertions(+), 20 deletions(-) diff --git a/csrc/pf_cglue.c b/csrc/pf_cglue.c index 2e6f52d..0ec1e70 100644 --- a/csrc/pf_cglue.c +++ b/csrc/pf_cglue.c @@ -85,7 +85,7 @@ Err CreateGlueToC( const char *CName, ucell_t Index, cell_t ReturnMode, int32_t ucell_t Packed; char FName[40]; - CStringToForth( FName, CName ); + CStringToForth( FName, CName, sizeof(FName) ); Packed = (Index & 0xFFFF) | 0 | (NumParams << 24) | (ReturnMode << 31); DBUG(("Packed = 0x%8x\n", Packed)); diff --git a/csrc/pf_core.c b/csrc/pf_core.c index f7bf38f..235c0a2 100644 --- a/csrc/pf_core.c +++ b/csrc/pf_core.c @@ -572,3 +572,13 @@ error1: return -1; } + + +#ifdef PF_UNIT_TEST +cell_t pfUnitTest( void ) +{ + cell_t numErrors = 0; + numErrors += pfUnitTestText(); + return numErrors; +} +#endif diff --git a/csrc/pf_core.h b/csrc/pf_core.h index 630d1a2..1279e8b 100644 --- a/csrc/pf_core.h +++ b/csrc/pf_core.h @@ -27,7 +27,8 @@ void pfInitGlobals( void ); void pfDebugMessage( const char *CString ); void pfDebugPrintDecimalNumber( int n ); - + +cell_t pfUnitTestText( void ); #ifdef __cplusplus } diff --git a/csrc/pf_inner.c b/csrc/pf_inner.c index fe60d15..dad9265 100644 --- a/csrc/pf_inner.c +++ b/csrc/pf_inner.c @@ -26,6 +26,8 @@ ** ***************************************************************/ +#include + #include "pf_all.h" #ifdef WIN32 @@ -1544,7 +1546,7 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); CodeSize = TOS; NameSize = M_POP; EntryPoint = M_POP; - ForthStringToC( gScratch, (char *) M_POP ); + ForthStringToC( gScratch, (char *) M_POP, sizeof(gScratch) ); TOS = ffSaveForth( gScratch, EntryPoint, NameSize, CodeSize ); } endcase; diff --git a/csrc/pf_io.h b/csrc/pf_io.h index 2a4af4b..beb3495 100644 --- a/csrc/pf_io.h +++ b/csrc/pf_io.h @@ -118,7 +118,7 @@ void ioTerm( void ); #define sdFlushFile fflush #define sdReadFile fread #define sdWriteFile fwrite - #if WIN32 + #if defined(WIN32) || defined(__NT__) /* TODO To support 64-bit file offset we probably need fseeki64(). */ #define sdSeekFile fseek #define sdTellFile ftell diff --git a/csrc/pf_main.c b/csrc/pf_main.c index 71defbc..a973553 100644 --- a/csrc/pf_main.c +++ b/csrc/pf_main.c @@ -129,6 +129,14 @@ int main( int argc, char **argv ) DicName = NULL; #endif +#ifdef PF_UNIT_TEST + if( (Result = pfUnitTest()) != 0 ) + { + ERR(("pForth stopping on unit test failure.\n")); + goto on_error; + } +#endif + Result = pfDoForth( DicName, SourceName, IfInit); on_error: diff --git a/csrc/pf_text.c b/csrc/pf_text.c index 9530546..6d36e2c 100644 --- a/csrc/pf_text.c +++ b/csrc/pf_text.c @@ -135,11 +135,16 @@ void pfReportThrow( ThrowCode code ) ** Copy a Forth String to a 'C' string. */ -char *ForthStringToC( char *dst, const char *FString ) +char *ForthStringToC( char *dst, const char *FString, cell_t dstSize ) { cell_t Len; Len = (cell_t) *FString; + /* Make sure the text + NUL can fit. */ + if( Len >= dstSize ) + { + Len = dstSize - 1; + } pfCopyMemory( dst, FString+1, Len ); dst[Len] = '\0'; @@ -149,17 +154,20 @@ char *ForthStringToC( char *dst, const char *FString ) /************************************************************** ** Copy a NUL terminated string to a Forth counted string. */ -char *CStringToForth( char *dst, const char *CString ) +char *CStringToForth( char *dst, const char *CString, cell_t dstSize ) { - char *s; cell_t i; - s = dst+1; - for( i=0; *CString; i++ ) + /* Make sure the SIZE+text can fit. */ + for( i=1; i /* Use console mode I/O so that KEY and ?TERMINAL will work. */ -#if WIN32 +#if defined(WIN32) || defined(__NT__) int sdTerminalOut( char c ) { +#if defined(__WATCOMC__) + return putch((char)(c)); +#else return _putch((char)(c)); +#endif } /* Needed cuz _getch() does not echo. */ int sdTerminalEcho( char c ) { +#if defined(__WATCOMC__) + return putch((char)(c)); +#else return _putch((char)(c)); +#endif } int sdTerminalIn( void ) diff --git a/csrc/win32_console/pf_io_win32_console.c b/csrc/win32_console/pf_io_win32_console.c index 8efd7d8..d36a04c 100644 --- a/csrc/win32_console/pf_io_win32_console.c +++ b/csrc/win32_console/pf_io_win32_console.c @@ -20,7 +20,7 @@ #include "../pf_all.h" -#if WIN32 +#if defined(WIN32) || defined(__NT__) #include diff --git a/releases.txt b/releases.txt index 571a512..82be64c 100644 --- a/releases.txt +++ b/releases.txt @@ -10,8 +10,12 @@ V27 http://code.google.com/p/pforth/issues/detail?id=4&can=1 - Fixed scrambled HISTORY on 64-bit systems. Was using CELL+ but really needed 4 +. - Fixed floating point input. Now accepts "1E" as 1.0. Was Issue #2. - - Fixed lots of warning and made code compatible with C89 and ANSI with -pedantic. + - Fixed lots of warning and made code compatible with C89 and ANSI. Uses -pedantic. - Use fseek and ftell on WIN32 instead of fseeko and ftello. + - Makefile is now more standard. Builds in same dir as Makefile. Uses CFLAGS etc. + - Add support for console IO with _WATCOMC_ + - Internal CStringToForth and ForthStringToC now take a destination size for safety. + - Run units tests for CStringToForth and ForthStringToC if PF_UNIT_TESTS is defined. V26 5/20/2010 - 64-bit support for M* UM/MOD etc by Aleksej Saushev. Thanks Aleksej! -- 2.20.1