Menu

Snap7.cpp - CliErrorText - Wrong TextLen

2017-03-01
2017-03-01
  • Keith Birkenkampf

    Dear Snap7 Community and Developer,
    i use ur library for a studentproject. I found a small bug in your Code.
    In snap7.cpp you have this code:

    TextString CliErrorText(int Error)
    {
        char text[TextLen];
        Cli_ErrorText(Error, text, TextLen);
        return TextString(text);
    }
    

    and in snap7_libmain.cpp you have the following code:

    int S7API Cli_ErrorText(int Error, char *Text, int TextLen)
    {
        try{
            ErrCliText(Error, Text, TextLen);
            Text[TextLen] = '\0';
        }
        catch (...){
            return errLibInvalidParam;
        }
        return 0;
    }
    

    Here you write the '\0' at the position TextLen and that is after the char[] and not within. I think it should be [TextLen - 1].
    With the original code i get some: STATUS_STACK_BUFFER_OVERRUN. (Qt Creator)

    I use the Snap7 1.4.2 Version from Sourceforge.

    I hope i could help you!

     

    Last edit: Keith Birkenkampf 2017-03-01
  • Davide Nardella

    Davide Nardella - 2017-03-01

    Many thanks ;) , I will look at it.

    I looked, you are right.
    I will fix it into next release.

     

    Last edit: Davide Nardella 2017-03-01
  • RBob

    RBob - 2017-08-24

    Hi Davide, thank you very much for Snap7 library!

    I probably found a similar error.
    I'm using Snap7.pas (1.4.2) in DelphiXE4 and CliErrorText funtion return me an empty string.
    So, to test it, I simulate an error and I debug your CliErrorText to indagate the problem;
    with a breakpoint on the last line of the code, Result seems to be correct, but it becomes empty returning the value in the caller. Probably there is a problem in the strings transformation: it seems an error in the pascal string length assignement (or the error is in the external function CliErrorText as Keith suggested).

    This is your code:

    function CliErrorText(Error : integer) : string;
    Var
    Text : packed array[0..TextLen-1] of AnsiChar;
    begin
    if Cli_ErrorText(Error, @Text, TextLen)=0 then
    Result:=String(Text)
    else
    Result:='LIB : Error getting text';
    end;

    Follow versions seem to solve the problem:

    function CliErrorText(Error: Integer): string;
    const TextLen = 1024;
    var Text: PAnsiChar;
    begin
    GetMem(Text, TextLen * SizeOf(AnsiChar));
    try
    if Cli_ErrorText(Error, Text, TextLen)=0 then
    Result:= string(Text)
    else Result:='LIB : Error getting text';
    finally
    FreeMem(Text);
    end
    end;

    {// versione alternativa
    function CliErrorText(Error : integer) : string;
    const TextLen = 1024;
    var Text : packed array[0..TextLen-1] of AnsiChar;
    Len: Integer;
    begin
    if Cli_ErrorText(Error, @Text, TextLen)=0 then begin
    Result:= Text;
    Len:= 0; while Text[Len]<>#0 do Inc(Len);
    SetLength(Result, Len)
    end else
    Result:='LIB : Error getting text';
    end;}

    Thank you!

     
  • Davide Nardella

    Davide Nardella - 2017-08-24

    thanks for reporting.
    I fixed the error reported by Keith, try to re-download Snap7 1.4.2. I use Snap7 mainly with Delphi (XE5) and works fine.

     
  • RBob

    RBob - 2017-08-26

    ok, thank you;
    last download works perfectly!
    r.

     

Log in to post a comment.