Recent

Author Topic: [SOLVED] Code trouble  (Read 962 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Code trouble
« on: October 23, 2020, 01:24:10 pm »
Hi All,

I am using nxnetwork.pas in a little program I am creating.

It allows to set a "key" to be used with TCP communication.

When I set the key string to '' the program runs fine but as soon as I use a key the program fails here

Code: Pascal  [Select][+][-]
  1. procedure TConnection.MaskCrypt(p: {$IFDEF fpc}PByte{$ELSE}PByteArray{$ENDIF};
  2.   size: integer);
  3. var i, l: integer;
  4. begin
  5.   l := length(FMask);
  6.   if l<1 then exit;
  7.   for i := 0 to size-1 do p[i] := p[i] xor byte(FMask[(i mod l)+1]);
  8. end;
  9.  

I get a SIGSEGV.

Can anyone tell me how to fix this?
« Last Edit: October 23, 2020, 07:22:31 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Code trouble
« Reply #1 on: October 23, 2020, 02:23:28 pm »
Again guessing topic #12131.
How it is so hard to give compilable code?

Run your program in debug mode with range check on.
« Last Edit: October 23, 2020, 02:28:55 pm by bytebites »

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Code trouble
« Reply #2 on: October 23, 2020, 04:27:15 pm »
OK. I've expanded the code -

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1 : TButton;
  16.     procedure Button1Click(Sender : TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1 : TForm1;
  25.  
  26. const
  27.   fMask = 'abcd';
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure MaskCrypt(p: {$IFDEF fpc}PByte{$ELSE}PByteArray{$ENDIF};
  36.                     size: integer);
  37. var i,
  38.     l : integer;
  39. begin
  40.   l := length(FMask);
  41.   if l < 1 then exit;
  42.   for i := 0 to size - 1 do p[i] := p[i] xor byte(FMask[(i mod l) + 1]);
  43. end;
  44.  
  45. procedure TForm1.Button1Click(Sender : TObject);
  46. var
  47.   s : string;
  48. begin
  49.   s := 'test';
  50.  
  51.   MaskCrypt(@s[1], length(s));
  52. end;
  53.  
  54. end.
  55.  
  56.  
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Code trouble
« Reply #3 on: October 23, 2020, 05:00:36 pm »
Try using UniqueString(S) before passing it to the function.
The only true wisdom is knowing you know nothing

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Code trouble
« Reply #4 on: October 23, 2020, 06:32:00 pm »
or don't use pchar:
Code: Pascal  [Select][+][-]
  1. procedure MaskCrypt(var s:string);
  2. var i,
  3.     l : integer;
  4. begin
  5.   l := length(FMask);
  6.   if l < 1 then exit;
  7.   for i := 1 to Length(s) do s[i] := chr(ord(s[i]) xor ord(FMask[(i mod l) + 1]));
  8. end;  

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Code trouble
« Reply #5 on: October 23, 2020, 07:21:35 pm »
OK. Thanks.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018