Options

Function call by reference

ta5ta5 Member Posts: 1,164
Hi
I have a function "myFunc" which takes 1 parameter (Text,5) by reference. When this function is called with a parameter exceeding the length of 5 I would expect an error (overflow). This does not happen.

Is this a kind of a bug or is this by design? Is it safe to call the function without regarding the length of the parameter?
Many Thanks
Thomas

Comments

  • Options
    kinekine Member Posts: 12,562
    Which version you are using?

    Commonly there is error "The length of the source exceeds the size of the destination buffer."

    But only if the Value is longer, not the data type...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    ta5ta5 Member Posts: 1,164
    Hi

    I'm using Version 3.7
    I also know the message "The length of the source exceeds the size of the destination buffer." , but I can't reproduce this message. What's the difference between this message and the message "Overflow"?
    Thomas
  • Options
    kinekine Member Posts: 12,562
    Sorry, I made some small test (function with one param and passing longer string) -

    It seems, that the error about buffer is when size of parameters is longer than some stack size...

    If I pass only longer string, there was Overflow error.

    Do not forget, that you can have only 20 parameters in one function and may be there is some limit for size of all passed values (stack size)...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    ta5ta5 Member Posts: 1,164
    I conducted also a few more tests.
    It seems, that the "Buffer" error is produced when using Text Constants; the "Overflow" error is produced when using Text Variables.

    Anyway, do you have experiences with functions calls by ref (using longer parameters than defined)?
    Regards
    Thomas
  • Options
    kinekine Member Posts: 12,562
    I tested it, yes, it seems, that if you pass param by Ref, the local variable have same size as the passed... no truncate, no overflow, only if you assign bigger value than passed variable size...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    ta5ta5 Member Posts: 1,164
    Thank you.
    I dont understand this "only if you assign bigger value than passed variable size..."
  • Options
    kinekine Member Posts: 12,562
    I have Text10 : text[10]
    
    F(Text10);  //call the function
    
    Function F(Var Param:Text[1])
    begin
      Param := '1234567890A';
    end;
    

    there will be error, but when I assign only '1234567890' it will be ok (the Param is Text[1], but it takes it as Text[10]);
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    ta5ta5 Member Posts: 1,164
    Thank you, I got it now :)
    Do you think it's save to use longer parameters than defined? Anyway, I think its quite dangerous, because no error is produced.
    Thomas
  • Options
    kinekine Member Posts: 12,562
    I think, that you must think, that the parameter is only pointer, and the text length is only hint, but not limit... if it will check for the limit of the param, you can have problems, because you can write to memory without limit...

    example:
    Var
      Text1 : text[1]
    
    begin
      F(Text1);
    end;
    
    function F(var Param: text [10])
    begin
      Param := '1234567890';
    end;
    

    In this case, if the C/SIDE will check for the limit of the parameter type, it will write outside of the passed variable... Param is only pointer to the Text1 variable... C/SIDE correctly test for the limit of Text1...

    In this way, it is first example of dynamic variable in C/AL code... :lol:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    ta5ta5 Member Posts: 1,164
    I guess, things like this could be the reason for some of the mysterious errors "error xy in module xy".
    Its ok for me now, thank you again for your input.
    Thomas
Sign In or Register to comment.