When using a plain String I'm getting this warning
warning: missing string capacity -- assuming 255
Getting rid of this warning for Var's is not a problem as I just do:
mystring : String (some number);
But how do you get rid of the warning when a function returns a string such as this:
function foobar( test : Integer): String;
I have tried to add a capacity argument to String but then I get this error:
parse error before '('
So the question is how can you assign the capacity to a String type when you are returning it from a function? I would prefer to not have to make a type for this if possible.
Also while on the subject of strings are gpc strings protected against buffer overruns?
Thanks Richard Jackson
"Richard D. Jackson" wrote:
... snip ...
So the question is how can you assign the capacity to a String type when you are returning it from a function? I would prefer to not have to make a type for this if possible.
Just think about functions in general. What are you going to do with the values they return? In general, you will store them in a variable, which must have a type that can receive that function result. To have two (or more) things of the same type it is necessary to declare that type.
CBFalconer wrote:
Just think about functions in general. What are you going to do with the values they return? In general, you will store them in a variable, which must have a type that can receive that function result. To have two (or more) things of the same type it is necessary to declare that type.
In general, this is a valid argument -- though not in the special case of strings, since according to EP (not CP, AFAIK), all string values are assignment compatible (except for capacity overruns), so a function result of some string type can be assigned to some other string type variable. But still, the syntax requires a function result to be a type-name.
Frank
On Wed, 2003-02-12 at 19:01, CBFalconer wrote:
"Richard D. Jackson" wrote:
... snip ...
So the question is how can you assign the capacity to a String type when you are returning it from a function? I would prefer to not have to make a type for this if possible.
Just think about functions in general. What are you going to do with the values they return? In general, you will store them in a variable, which must have a type that can receive that function result. To have two (or more) things of the same type it is necessary to declare that type.
True but it seems gpc does not enforce this. For instance lets say:
type str255 = String (255);
var myStr : String (100);
function foo: Str255; begin ... end;
begin myStr := foo; ... end.
In this case what gpc does is truncate the return value to fit into myStr. Other words this does not cause a compiler error so the compiler sees these two strings as being equivlant even though that is not the case. The question I have is WriteLn just reading String.Length and only ouputing that amount of data or is String.String actualy constrained to 255. If you look below you will see why I wonder that.
On Wed, 2003-02-12 at 19:34, Frank Heckenbach wrote:
So the question is how can you assign the capacity to a String type
when
you are returning it from a function? I would prefer to not have to make a type for this if possible.
You'll have to do exactly this, I'm afraid. E.g., the `GPC' unit defines a type `TString' which it uses for all string function results.
Also while on the subject of strings are gpc strings protected against buffer overruns?
Modulo possible bugs, yes. (But I don't know if anyone has run extensive tests, so if you want to do this, I'll try to fix any bugs you might find ...)
What is interesting is that. Given
var buff : String (255);
I can stuff 30925 chars into it after that majic number I get a segfault. So what is happening here? If I suround a string with other vars they are not getting overwriten wich is a good thing but I still wonder why it did not segfault at 256. I guess it is something I will have to look into when I have more time to dig into it with gdb.
Richard
Richard D. Jackson wrote:
On Wed, 2003-02-12 at 19:01, CBFalconer wrote:
Just think about functions in general. What are you going to do with the values they return? In general, you will store them in a variable, which must have a type that can receive that function result. To have two (or more) things of the same type it is necessary to declare that type.
True but it seems gpc does not enforce this. For instance lets say:
type str255 = String (255);
var myStr : String (100);
function foo: Str255; begin ... end;
begin myStr := foo; ... end.
In this case what gpc does is truncate the return value to fit into myStr. Other words this does not cause a compiler error so the compiler sees these two strings as being equivlant even though that is not the case.
My previous reply to Chuck's mail should have made this clear.
The question I have is WriteLn just reading String.Length and only ouputing that amount of data or is String.String actualy constrained to 255.
Yes (both).
On Wed, 2003-02-12 at 19:34, Frank Heckenbach wrote:
So the question is how can you assign the capacity to a String type
when
you are returning it from a function? I would prefer to not have to make a type for this if possible.
You'll have to do exactly this, I'm afraid. E.g., the `GPC' unit defines a type `TString' which it uses for all string function results.
Also while on the subject of strings are gpc strings protected against buffer overruns?
Modulo possible bugs, yes. (But I don't know if anyone has run extensive tests, so if you want to do this, I'll try to fix any bugs you might find ...)
What is interesting is that. Given
var buff : String (255);
I can stuff 30925 chars into it after that majic number I get a segfault.
I can't. The following stops at 255.
program Foo;
var buff : String (255);
begin buff := ''; repeat buff := buff + ' '; WriteLn (Length (buff)) until False end.
If what you mean is treating the string as an array of char and indexing out of range, this would be a matter of range-checking(*) -- I'm not sure if you mean this, that's why it's always a good idea to include some example code!
(*) which GPC is currently lacking. I wrote a mail about it on 2002-12-14, but there doesn't seem to be much interested from the users.
If I suround a string with other vars they are not getting overwriten wich is a good thing but I still wonder why it did not segfault at 256. I guess it is something I will have to look into when I have more time to dig into it with gdb.
Programming languages have more or less implementation-defined behaviour, but I don't think any language would guarantee a segfault in a certain situation. ;-)
Frank
Frank Heckenbach wrote:
... snip ...
If what you mean is treating the string as an array of char and indexing out of range, this would be a matter of range-checking(*) -- I'm not sure if you mean this, that's why it's always a good idea to include some example code!
(*) which GPC is currently lacking. I wrote a mail about it on 2002-12-14, but there doesn't seem to be much interested from the users.
Because there isn't any rangechecking. Pascal is not complete without runtime rangechecks, IMO.
CBFalconer wrote:
Frank Heckenbach wrote:
... snip ...
If what you mean is treating the string as an array of char and indexing out of range, this would be a matter of range-checking(*) -- I'm not sure if you mean this, that's why it's always a good idea to include some example code!
(*) which GPC is currently lacking. I wrote a mail about it on 2002-12-14, but there doesn't seem to be much interested from the users.
Because there isn't any rangechecking. Pascal is not complete without runtime rangechecks, IMO.
The standard says:
: 6.4.6 AssignmentÂcompatibility : : [...] : : At any place where the rule of assignmentÂcompatibility is used : : a) it shall be an error if T1 and T2 are compatible ordinalÂtypes and the : value of type T2 is not in the closed interval specified by the type : T1;
: 3.2 Error : : A violation by a program of the requirements of this International Standard : that a processor is permitted to leave undetected.
You're entitled to your opinion, of course, and range checking may be a desirable feature, but from a standards point of view, it's no more essential than many other runtime checks GPC currently doesn't do (some of which would be very hard to implement, such as anonymous variant checks and pointer reference checks) ...
Frank
In article 200302130727.IAA30049@goedel.fjf.gnu.de, Frank Heckenbach frank@g-n-u.de writes
If what you mean is treating the string as an array of char and indexing out of range, this would be a matter of range-checking(*) -- I'm not sure if you mean this, that's why it's always a good idea to include some example code!
(*) which GPC is currently lacking. I wrote a mail about it on 2002-12-14, but there doesn't seem to be much interested from the users.
I'd like to see range checking available as an option but I think the work to integrate gpc with gcc is more important.
On Thu, 2003-02-13 at 04:59, Martin Liddle wrote:
In article 200302130727.IAA30049@goedel.fjf.gnu.de, Frank Heckenbach frank@g-n-u.de writes
If what you mean is treating the string as an array of char and indexing out of range, this would be a matter of range-checking(*) -- I'm not sure if you mean this, that's why it's always a good idea to include some example code!
(*) which GPC is currently lacking. I wrote a mail about it on 2002-12-14, but there doesn't seem to be much interested from the users.
I'd like to see range checking available as an option but I think the work to integrate gpc with gcc is more important.
I agree with this as a carfull programer will not allow this to happen. The one place where you do need range checking is input which you can get around by using a libc function I forget the exact function names but I know they are there in libc due to all of the buffer overflow security issues.
Richard
Richard D. Jackson wrote:
When using a plain String I'm getting this warning
warning: missing string capacity -- assuming 255
Getting rid of this warning for Var's is not a problem as I just do:
mystring : String (some number);
But how do you get rid of the warning when a function returns a string such as this:
function foobar( test : Integer): String;
I have tried to add a capacity argument to String but then I get this error:
parse error before '('
So the question is how can you assign the capacity to a String type when you are returning it from a function? I would prefer to not have to make a type for this if possible.
You'll have to do exactly this, I'm afraid. E.g., the `GPC' unit defines a type `TString' which it uses for all string function results.
Also while on the subject of strings are gpc strings protected against buffer overruns?
Modulo possible bugs, yes. (But I don't know if anyone has run extensive tests, so if you want to do this, I'll try to fix any bugs you might find ...)
Frank