程序在 FPC 和 ObjFPC 中表现不同

Program behaving differently in FPC and ObjFPC

下面是lazarus中给定一个字符串进行RC4加解密的程序。当使用编译器指令 {$mode fpc} 并输入字符串 123 时,它会因 External: SIGSEGV 而崩溃。但是,在 {$mode objfpc} 中输入相同的字符串时不会发生崩溃。另一个不一致是,在 {$mode objfpc} 中,程序使用 hellow.

的输入再次引发异常 External: SIGSEGV
//{$mode fpc}
{$mode objfpc}
program project1;
uses
  sysutils, strutils;
type
  myArray = array[0..255] of integer;
  dynamicArray = array of integer;
  dynamicArrayString = array of string;
const
  key = 'qwertyui';
var
  plaintext : string;

function KSA(const key: string): myArray;
var
  i, j, key_length, temp: integer;
  S : myArray;
begin
  key_length := length(key);
  j := 0;
  for i := Low(S) to High(S) do
    S[i] := i;
  for i := Low(S) to High(S) do
  begin
    j := ((j + S[i] + ord(key[i mod key_length + 1])) mod 256);
    temp := S[i];
    S[i] := S[j];
    S[j] := temp;
  end;
  KSA := S;
end;

function PRGA(S : myArray; n : integer) : dynamicArray;
var
  i, j, K, temp, sizeOfArray : integer;
  key : dynamicArray;
begin
  i := 0;
  j := 0;
  K := 0;
  temp := 0;
  sizeOfArray := n - 1;
  SetLength(key, sizeOfArray);
  while n > 0 do
  begin
    n := n - 1;
    i := (i + 1) mod 256;
    j := (j + S[i]) mod 256;
    temp := S[i];
    S[i] := S[j];
    S[j] := temp;
    K := S[(S[i] + S[j]) mod 256];
    key[i-1] := K;
  end;
  PRGA := key;
end;

procedure getPlaintext;
begin
  readln(plaintext);
end;

function encrypt : string;
var
  sizeOfArray, i : integer;
  cipherString : string;
  cipher, keystream: dynamicArray;
  S : myArray;
begin
  S := KSA(key);
  keystream := PRGA(S, length(plaintext));
  sizeOfArray := 0;
  for i := 0 to (length(plaintext) - 1) do
  begin
    sizeOfArray := sizeOfArray + 1;
    SetLength(cipher, sizeOfArray);
    cipher[i] := (keystream[i]) xor (ord(plaintext[i + 1]));
  end;
  cipherString := '';
  for i := 0 to High(cipher) do
    cipherString := cipherString + IntToHex(cipher[I], 2);
  encrypt := cipherString;
end;

function stringToHex(cipherString : string) : dynamicArrayString;
var
  sizeOfArray, i: integer;
  DecryptArrayString : dynamicArrayString;
begin
  sizeOfArray := 0;
  i := 0;
  // Turn the string into an array of hex
  while length(cipherString) > 0 do
  begin
    sizeOfArray := sizeOfArray + 1;
    SetLength(DecryptArrayString, sizeOfArray);
    DecryptArrayString[i] := cipherString[1] + cipherString[2];
    i := i + 1;
    cipherString := rightstr(cipherString, length(cipherString) - 2);
  end;
  stringToHex :=  DecryptArrayString;
end;

function hexToDecimal(DecryptArrayString : dynamicArrayString) : dynamicArray;
var
  sizeOfDecryptArrayInt, i : integer;
  DecryptArrayInt : dynamicArray;
begin
  sizeOfDecryptArrayInt := 0;
  // Hex to decimal
  for i := 0 to high(DecryptArrayString) do
  begin
    sizeOfDecryptArrayInt := sizeOfDecryptArrayInt + 1;
    SetLength(DecryptArrayInt, sizeOfDecryptArrayInt);
    DecryptArrayInt[i] := Hex2Dec(DecryptArrayString[i]);
  end;
  hexToDecimal := DecryptArrayInt;
end;

function decrypt(DecryptArrayInt : dynamicArray) : string;
var
  DecryptedString : string;
  S : myArray;
  keystream, Decrypted : dynamicArray;
  sizeOfArray, i : integer;
begin
  sizeOfArray := 0;
  for i := 0 to high(DecryptArrayInt) do
  begin
    sizeOfArray := sizeOfArray + 1;
    SetLength(Decrypted, sizeOfArray);
    S := KSA(key);
    keystream := PRGA(S, length(plaintext));
    Decrypted[i] := (keystream[i] xor DecryptArrayInt[i]);
  end;
  decryptedString := '';
  // Turn array to string
  for i := 0 to high(Decrypted) do
    decryptedString := decryptedString + chr(Decrypted[i]);
  decrypt := decryptedString;
end;

procedure encryptDecrypt;
var
  cipherString, DecryptedString : string;
  DecryptArrayString : dynamicArrayString;
  DecryptArrayInt : dynamicArray;
begin
  cipherString := encrypt;
  writeln(cipherString);
  DecryptArrayString := stringToHex(cipherString);
  DecryptArrayInt := hexToDecimal(DecryptArrayString);
  DecryptedString := decrypt(DecryptArrayInt);
  writeln(DecryptedString);
end;

begin
  getPlaintext;
  encryptDecrypt;
  readln;
end.

除了任何三个字符的字符串在 {$mode fpc} 中总是会崩溃但在 {$mode objfpc}[=20 中不会,我一直无法找到导致程序崩溃的许多具体情况=]

您应该调试(并观察)您的代码,以便更容易地追踪任何错误。使用FPIDE,很有帮助

同时,注意这一行:

keystream := PRGA(S, length(plaintext));

在 PRGA 函数中观察这些行:

sizeOfArray := n - 1;
SetLength(key, sizeOfArray);

这是怎么回事?好吧,假设 "plaintext" 只有 1 个字符。那么,"Length(plaintext)"=1,对吧?在 PRGA 中,您执行 "sizeOfArray:= n-1;",因此 sizeOfArray = 0。当您执行 SetLength 时,"Key" 将为 NIL,因为您指示索引长度为零。稍后,在循环中,您有:

key[i-1] := K;

程序将失败,因为您正在索引 0 处访问并且 Key 变量为 NIL。

注意:在 "SetLength(key, sizeOfArray);" 上,键的有效索引在 [0..sizeOfArray-1] 范围内,但 sizeOfArray 必须至少为 1 才能有效(sizeOfArray >= 1)。

所以,你应该检查你的PRGA功能。最终的解决方案取决于您。不要放弃。祝你好运!