Board index » delphi » Fastcode Trim B&V Ver 3.0 Validation Errors

Fastcode Trim B&V Ver 3.0 Validation Errors


2007-02-11 09:16:41 PM
delphi276
There are a couple of significant errors in the validation, that need to
fixed ASAP.
1) Validate3 passes the wrong parameter into the test function.
S2 := TrimFunction(S1);
needs to be replaced with:-
S2 := TrimFunction(ValidationTestSetArray[TestNo].S);
The current code wrongly passes the already trimmed string into the test
function.
2) Validate11 creates an invalid zero-length non-nil string.
GlobalStrValidate11 := 'A';
needs to be replaced with:-
GlobalStrValidate11 := #0;
Note - This second change also needs to be made to many other Fastcode
challenges (zero-length non-nil string must still contain a null terminator)
--
regards,
John
The Fastcode Project:
www.fastcodeproject.org/
 
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi John,
Quote

GlobalStrValidate11 := 'A';

needs to be replaced with:-

GlobalStrValidate11 := #0;

Note - This second change also needs to be made to many other Fastcode
challenges (zero-length non-nil string must still contain a null terminator)

Do you already have a list of those ready? I will make updates
to many challenges after cpuid and systeminfo units are updated.
My next thing is to work on those.
Regards,
Jouni
--
The Fastcode Project: www.fastcodeproject.org/
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi John
Thanks I will fix that tomorrow. In good time before reaching 3.0 :-) We
take 0.4.0 first.
Quote
>Note - This second change also needs to be made to many other Fastcode
>challenges (zero-length non-nil string must still contain a null
terminator)
>

Do you already have a list of those ready? I will make updates
to many challenges after cpuid and systeminfo units are updated.
My next thing is to work on those.
Jouni: I have the lock on the Trim B&V until it reaches 1.0.0, but feel free
to modify all other B&V's.
Best regards
Dennis Kjaer Christensen
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi Dennis,
Quote

Jouni: I have the lock on the Trim B&V until it reaches 1.0.0, but feel free
to modify all other B&V's.

Ok. I am too busy at work these days, haven't had time for
those 2007 modifications yet. Will start when possible.
Regards,
Jouni
--
The Fastcode Project: www.fastcodeproject.org/
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi John
Quote
1) Validate3 passes the wrong parameter into the test function.

S2 := TrimFunction(S1);

needs to be replaced with:-

S2 := TrimFunction(ValidationTestSetArray[TestNo].S);

The current code wrongly passes the already trimmed string into the test
function.
My code looked like this
S1 := ValidationTestSetArray[TestNo].CorrectResult;
S2 := TrimFunction(S1);
if S2 <>ValidationTestSetArray[TestNo].CorrectResult then
and should of course be
S1 := ValidationTestSetArray[TestNo].S;
S2 := TrimFunction(S1);
if S2 <>ValidationTestSetArray[TestNo].CorrectResult then
Thanks for reviewing it.
Best regards
Dennis Kjaer Christensen
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi
Quote
2) Validate11 creates an invalid zero-length non-nil string.

GlobalStrValidate11 := 'A';

needs to be replaced with:-

GlobalStrValidate11 := #0;

Note - This second change also needs to be made to many other Fastcode
challenges (zero-length non-nil string must still contain a null
terminator)
I do not agree here. GlobalStrValidate11 does have a zero terminator
GlobalStrValidate11 := 'A';
But it is not located at GlobalStrValidate11[1]
I think that the point of the validation is that Trim must return an empty
string no matter what the input string is - when the length field is 0.
GlobalStrValidate11 := #0;
GlobalStrValidate11 := 'A';
are both good validation strings as any other string would be.
var
GlobalStrValidate11: AnsiString;
function TMainForm.Validate11 : Boolean;
var
S : string;
const
VALIDATENO : Cardinal = 11;
begin
Result := True;
GlobalStrValidate11 := 'A';
PInteger(Integer(GlobalStrValidate11)-4)^ := 0; //Set Length to 0
S := TrimFunction(GlobalStrValidate11);
if S <>'' then
begin
ErrorTrap(VALIDATENO, GlobalStrValidate11);
Result := False;
Exit;
end;
end;
Best regards
Dennis Kjaer Christensen
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi John and others
I have added these two validation functions for non nil zero length strings.
Are they OK?
var
GlobalStrValidate16: AnsiString;
function TMainForm.Validate16 : Boolean;
var
S : string;
const
VALIDATENO : Cardinal = 16;
begin
Result := True;
GlobalStrValidate16 := #0;
PInteger(Integer(GlobalStrValidate16)-4)^ := 0; //Set Length to 0
S := TrimFunction(GlobalStrValidate16);
if S <>'' then
begin
ErrorTrap(VALIDATENO, GlobalStrValidate16);
Result := False;
Exit;
end;
end;
var
GlobalStrValidate17: AnsiString;
function TMainForm.Validate17 : Boolean;
var
TestNo : Integer;
S2 : string;
const
VALIDATENO : Cardinal = 17;
begin
Result := True;
for TestNo := 0 to Length(ValidationTestSetArray) - 1 do
begin
GlobalStrValidate17 := ValidationTestSetArray[TestNo].S;
if Pointer(GlobalStrValidate17) = nil then
Continue;
PInteger(Integer(GlobalStrValidate17)-4)^ := 0; //Set Length to 0
S2 := TrimFunction(GlobalStrValidate17);
PInteger(Integer(GlobalStrValidate17)-4)^ :=
Length(ValidationTestSetArray[TestNo].S); //Set Length back to correct
length
if S2 <>'' then
begin
ErrorTrap(VALIDATENO, GlobalStrValidate17);
Result := False;
Exit;
end;
end;
end;
Best regards
Dennis Kjaer Christensen
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi Dennis,
The lines:-
GlobalStrValidate11 := 'A';
PInteger(Integer(GlobalStrValidate11)-4)^ := 0; //Set Length to 0
does sets the length to zero, but it does not create a valid Ansistring.
The first character after the last position within a string must be a null
terminator (#0).
In this case, since the length is zero, #0 must be stored at the string
address, not 'A'
--
regards,
John
The Fastcode Project:
www.fastcodeproject.org/
"Dennis" <XXXX@XXXXX.COM>writes
Quote
Hi

>2) Validate11 creates an invalid zero-length non-nil string.
>
>GlobalStrValidate11 := 'A';
>
>needs to be replaced with:-
>
>GlobalStrValidate11 := #0;
>
>Note - This second change also needs to be made to many other Fastcode
>challenges (zero-length non-nil string must still contain a null
terminator)

I do not agree here. GlobalStrValidate11 does have a zero terminator

GlobalStrValidate11 := 'A';

But it is not located at GlobalStrValidate11[1]

I think that the point of the validation is that Trim must return an empty
string no matter what the input string is - when the length field is 0.

GlobalStrValidate11 := #0;
GlobalStrValidate11 := 'A';

are both good validation strings as any other string would be.

var
GlobalStrValidate11: AnsiString;

function TMainForm.Validate11 : Boolean;
var
S : string;
const
VALIDATENO : Cardinal = 11;

begin
Result := True;
GlobalStrValidate11 := 'A';
PInteger(Integer(GlobalStrValidate11)-4)^ := 0; //Set Length to 0
S := TrimFunction(GlobalStrValidate11);
if S <>'' then
begin
ErrorTrap(VALIDATENO, GlobalStrValidate11);
Result := False;
Exit;
end;
end;

Best regards
Dennis Kjaer Christensen


 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

"Dennis" <XXXX@XXXXX.COM>writes
Quote
Hi John and others

I have added these two validation functions for non nil zero length
strings.
Are they OK?

I do not believe Validate17 is correct, for the reasons given in my previous
reply.
--
regards,
John
The Fastcode Project:
www.fastcodeproject.org/
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi John
Quote
The first character after the last position within a string must be a null
terminator (#0).

In this case, since the length is zero, #0 must be stored at the string
address, not 'A'
I understand your argument, but I do not agree.
A non nil zero length string is not really a valid AnsiString in the first
place. You argue that all non nil zero length strings in the real world will
contain a #0 in the first position and this is all we need to validate. I
argue that abusers of the AnsiString will set the length field to 0 to
"empty" a string and then not care about what is in the string. I think we
should validate against a bunch of non nil zero length strings with any
chars after the pointer. If the RTL Trim function passes this validation so
should our functions. (Whether to support these bad strings at all is
another discussion. We currently have a rule that we must if the RTL
function does).
I hope that more people have opinions.
Best regards
Dennis Kjaer Christensen
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Until CodeGear say otherwise, we need to treat Non-Nil Zero-Length
AnsiStrings (with a null terminator) as valid. Without the null terminator,
many of the existing RTL functions would fail.
I don't think we should even consider other AnsiStrings that are not valid.
--
regards,
John
The Fastcode Project:
www.fastcodeproject.org/
"Dennis" <XXXX@XXXXX.COM>writes
Quote
Hi John

>The first character after the last position within a string must be a
>null
>terminator (#0).
>
>In this case, since the length is zero, #0 must be stored at the string
>address, not 'A'

I understand your argument, but I do not agree.

A non nil zero length string is not really a valid AnsiString in the first
place. You argue that all non nil zero length strings in the real world
will
contain a #0 in the first position and this is all we need to validate. I
argue that abusers of the AnsiString will set the length field to 0 to
"empty" a string and then not care about what is in the string. I think we
should validate against a bunch of non nil zero length strings with any
chars after the pointer. If the RTL Trim function passes this validation
so
should our functions. (Whether to support these bad strings at all is
another discussion. We currently have a rule that we must if the RTL
function does).

I hope that more people have opinions.

Best regards
Dennis Kjaer Christensen


 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi John etc.
Quote
Until CodeGear say otherwise, we need to treat Non-Nil Zero-Length
AnsiStrings (with a null terminator) as valid. Without the null
terminator,
many of the existing RTL functions would fail.
I agree - I think I wrote that. All the strings I use in that validation are
zero terminated.
Quote
I don't think we should even consider other AnsiStrings that are not
valid.
Why not if the RTL Trim function handles them correctly? We should be 100%
consistent with the RTL function.
function Trim
begin
if Length(S) <= 0 then
Result := '';
........................
end;
This test is the one that catches Non-Nil Zero-Length strings and it does
not rely on anything but the length field. All of the validations I propose
will be handled properly. Try run the RTL Trim through the validations. It
passes just fine and so should all our functions.
Do we need a poll to settle this issue?
Best regards
Dennis Kjaer Christensen
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

Hi
Quote
>I don't think we should even consider other AnsiStrings that are not
>valid.
Agreed.
Quote
Do we need a poll to settle this issue?
No.
--
regards,
Aleksandr
 

Re:Fastcode Trim B&V Ver 3.0 Validation Errors

"Dennis" <XXXX@XXXXX.COM>writes
Quote
>I don't think we should even consider other AnsiStrings that are not
valid.

Why not if the RTL Trim function handles them correctly? We should be 100%
consistent with the RTL function.
But the RTL doesn't handle them correctly.
Consider:-
var
GlobalStr: AnsiString;
procedure TForm55.Button1Click(Sender: TObject);
var
I: Integer;
begin
GlobalStr := '1';
PInteger(Integer(GlobalStr)-4)^ := 0;
I := StrToIntDef(GlobalStr, 0);
ShowMessage(IntToStr(I));
end;
This displays '1' instead of '0'
Quote
Do we need a poll to settle this issue?
No
--
regards,
John
The Fastcode Project:
www.fastcodeproject.org/