Fixed a bug which was causing sprintf() to crash.

Added a test for it.
Issue #66


git-svn-id: http://picoc.googlecode.com/svn/trunk@401 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-11-14 11:11:38 +00:00
parent 84d2c663e8
commit 5e6ddfa7ff
4 changed files with 33 additions and 8 deletions

View file

@ -288,15 +288,11 @@ void LibPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
void LibSPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
struct OutputStream StrStream;
struct Value *DerefVal;
int DerefOffset;
StrStream.i.Str.WritePos = VariableDereferencePointer(StrStream.i.Str.Parser, Param[0], &DerefVal, &DerefOffset, NULL, NULL);
if (DerefVal->Typ->Base != TypeArray)
ProgramFail(Parser, "can only print to arrays of char");
StrStream.Putch = &SPutc;
StrStream.i.Str.Parser = Parser;
StrStream.i.Str.WritePos = Param[0]->Val->NativePointer;
GenericPrintf(Parser, ReturnValue, Param+1, NumArgs-1, &StrStream);
PrintCh(0, &StrStream);
ReturnValue->Val->NativePointer = *Param;

8
tests/37_sprintf.c Normal file
View file

@ -0,0 +1,8 @@
char Buf[100];
int Count;
for (Count = 1; Count <= 20; Count++)
{
sprintf(Buf, "->%02d<-\n", Count);
printf("%s", Buf);
}

20
tests/37_sprintf.expect Normal file
View file

@ -0,0 +1,20 @@
->01<-
->02<-
->03<-
->04<-
->05<-
->06<-
->07<-
->08<-
->09<-
->10<-
->11<-
->12<-
->13<-
->14<-
->15<-
->16<-
->17<-
->18<-
->19<-
->20<-

View file

@ -29,7 +29,8 @@ TESTS= 00_assignment.test \
29_array_address.test \
34_array_assignment.test \
35_sizeof.test \
36_array_initialisers.test
36_array_initialisers.test \
37_sprintf.test
%.test: %.expect %.c
@echo Test: $*...