Recent

Author Topic: [SOLVED]External SIGSEGV in Loop  (Read 1196 times)

Sestus

  • New Member
  • *
  • Posts: 14
[SOLVED]External SIGSEGV in Loop
« on: September 17, 2021, 05:07:26 pm »
Hello, I'm currently working on a X-Tension for the program X-Ways Forensics with the help of Lazerus and Freepascal. I think i came really far with my code and currently i want to iron out any problems so the code works. My main Problem right now is that after the first running of the "while ItemSizeAbs > BufMax do" loop starting at LINE 64, which does everything as intended, i get the External SIGSEGV error message when debugging. Without debugging it also doesn't do what i want so there has to be a problem with the code. I tried to clean all used variables at the end with default values and zeros inside the arrays but it didn't help. Now I'm kinda lost because these type of error messages could mean anything. So is there only some kind of variable that is not declared or reset or some kind of missing or false code?
Code: Pascal  [Select][+][-]
  1. function XT_ProcessItem(nItemID: LongWord; lpReserved: Pointer): integer; stdcall; export;
  2.     const
  3.          BufLen = 4096;
  4.     var
  5.          Buf1, outputmessage1     : array[0..Buflen-1] of WideChar;
  6.          lpTypeDescr              : array[0..Buflen-1] of WideChar;
  7.          //UserInput              : Int64;   optional für später
  8.          hOpenResult              : THandle = Default(THandle);
  9.          nItemVMDKStatus          : LongInt = Default(LongInt);
  10.          //für Exportieren
  11.          ItemSize                 : Int64 = Default(Int64);
  12.          InputBytesBuffer         : TBytes;
  13.  
  14.          //n                          : Int64 = Default(Int64);
  15.          m                          : Int64 = Default(Int64);
  16.          x                          : Int64 = Default(Int64);
  17.          OGName                     : string = Default(string);
  18.          BufMax                     : Int64 = Default(Int64);
  19.          ItemSizeAbs                : Int64 = Default(Int64);
  20.          PartFileID                 : Int64 = Default(Int64);
  21.  
  22.          intBytesRead               : Int64 = Default(Int64);
  23.          BytesWritten               : Int64 = Default(Int64);
  24.          RTAdditionSuccess          : Integer = Default(Integer);
  25.          OutputStream               : TFileStream;
  26.          DelFileOK                  : Boolean = Default(Boolean);
  27.          scratchfilename            : string = Default(string);
  28.          //OutputFilename           : string = Default(string); Abwägung GUID oder nativer Filename
  29.          //für Importieren in XWF
  30.          FirstEvOCreated            : THandle = Default(THandle);
  31.          Buf2, ExportPath           : array[0..Buflen-1] of WideChar;
  32.          Buf3, ExportPathFinal      : array[0..Buflen-1] of WideChar;
  33.          Buf4, ExportPathOG         : array[0..Buflen-1] of WideChar;
  34.     begin
  35.         //Userinput := XWF_GetUserInput(,); //User wird gefragt welche Dateien (z.B. VMDK) er sucht, optional für später
  36.           FillChar(lpTypeDescr,         Length(lpTypeDescr),        $00);
  37.           FillChar(Buf1,                Length(Buf1),        $00);
  38.           FillChar(Buf2,                Length(Buf2),        $00);
  39.  
  40.           FillChar(outputmessage1,      Length(outputmessage1),     $00);
  41.  
  42.  
  43.         outputmessage1 := 'VMDK File found';   //Prepare Output Message
  44.         lstrcpyw(Buf1, outputmessage1);
  45.  
  46.         nItemVMDKStatus := XWF_GetItemType(nitemID, @lpTypeDescr, Length(lpTypeDescr));
  47.         ItemSize := XWF_GetItemSize(nitemID); //Größe der Datei bestimmen
  48.         //n := 0;
  49.         m := 0; //Stelle ab der die Datei in den Buffer gelesen wird
  50.         x := 1; //Variable zur Bennenung der Partfiles (Endung)
  51.         BufMax := BufLen; //Maximale Größe des Buffers
  52.         ItemSizeAbs := ItemSize; //Absolute ItemSize welche im Folgenden mit BufMax reduziert wird
  53.  
  54.  
  55.         if lpTypeDescr = 'vmdk' then  //wenn VMDK File ACHTUNG
  56.             begin
  57.                  XWF_OutputMessage(@Buf1[0], 0); //gib Nachricht 'VMDK File found' aus
  58.                  OGName := XWF_GetItemName(nitemID);  //Name der Originaldatei bestimmen, siehe oben
  59.                  //Exportieren der Datei
  60.                  hOpenResult := XWF_OpenItem(CurrentVolume, nItemID, $01); //Öffnen des Files, returned Handle, bei 0 unsuccessful
  61.                  if hOpenResult > 0 then  //wenn Öffnen des Items erfolgt
  62.                     begin
  63.  
  64.                          while ItemSizeAbs > BufMax do   //solange absolute ItemSize größer ist als der Maximale Buffer tue...
  65.                              begin
  66.                                   FillChar(InputBytesBuffer,   Length(InputBytesBuffer),  $00); //whipen des Buffers, um Probleme zu vermeiden
  67.                                   PartFileID := x; //Deklaration der ID des Splitterfiles (Endnung)
  68.  
  69.  
  70.                                   SetLength(InputBytesBuffer, BufMax); //Länge des Buffers auf BufMax festlegen da sonst zu groß
  71.  
  72.                                   intBytesRead := XWF_Read(hOpenResult, m, @(InputBytesBuffer)[0], BufMax);  //write von m bis BufMax in InputBytesBufferN
  73.                                   if intBytesRead > 0 then
  74.                                      begin
  75.                                      // Write the native file out to disk using the above declared stream
  76.                                      // Use a GUID as a temp filename, or 'tempfile.raw' if that fails,
  77.                                      // then write content of InputBytesBufferN to it
  78.                                      //scratchfilename := TGUID.NewGuid.ToString(true); // FÜR GUID Bezeichnung
  79.                                           scratchfilename := OGName; //FÜR ORIGINAL Bezeichnung
  80.                                           try
  81.                                              if Length(scratchfilename) > 0 then
  82.                                              begin
  83.                                                   ExportPath := IncludeTrailingPathDelimiter(OutputFolder) + scratchfilename + '.' + FormatFloat('000', PartFileID);  //ACHTUNG
  84.                                                   OutputStream := TFileStream.Create(ExportPath, fmCreate);
  85.                                              end
  86.                                              else
  87.                                                  begin
  88.                                                       scratchfilename := 'tempfile';
  89.                                                       OutputStream := TFileStream.Create(IncludeTrailingPathDelimiter(OutputFolder) + scratchfilename + '.' + FormatFloat('000', PartFileID), fmCreate);  //ACHTUNG
  90.                                                  end;
  91.                                              if OutPutStream.Handle > 0 then
  92.                                              begin
  93.                                                   BytesWritten := -1;
  94.                                                   BytesWritten := OutputStream.Write(InputBytesBuffer[0], intBytesRead);
  95.                                                   OutputStream.Free;
  96.                                              end;
  97.                                           finally
  98.                                           // nothing needed
  99.                                           end;
  100.                                      end;
  101.  
  102.                                   //n := n + 1;
  103.                                   m := m + BufMax;
  104.                                   x := x + 1;
  105.                                   ItemSizeAbs := ItemSizeAbs - BufMax;
  106.                                   FillChar(Buf2, Length(Buf2), $00);
  107.                                   scratchfilename :=  Default(string);
  108.                                   BytesWritten := Default(Int64);
  109.                                   IntBytesRead := Default(Int64);
  110.                                   PartFileID :=  Default(Int64);
  111.                              end;
  112.  
  113.                          end;
  114.                     //für den Fall dass, die ItemSizeAbs nun kleiner ist als die BufMax schreibe die letzte Datei
  115.                     FillChar(InputBytesBuffer,   Length(InputBytesBuffer),  $00); //whipen des Buffers, um Probleme zu vermeiden
  116.                     SetLength(InputBytesBuffer, ItemSizeAbs); //Länge des Buffers festlegen, Problem HIER ItemSize bei vmdk zu groß für InputBytesBuffer0
  117.  
  118.                     intBytesRead := XWF_Read(hOpenResult, m, @(InputBytesBuffer)[0], BufMax);  //write von m bis BufMax in InputBytesBufferN
  119.                                   if intBytesRead > 0 then
  120.                                      begin
  121.                                      // Write the native file out to disk using the above declared stream
  122.                                      // Use a GUID as a temp filename, or 'tempfile.raw' if that fails,
  123.                                      // then write content of InputBytesBufferN to it
  124.                                      //scratchfilename := TGUID.NewGuid.ToString(true); // FÜR GUID Bezeichnung
  125.                                           scratchfilename := OGName; //FÜR ORIGINAL Bezeichnung
  126.                                           try
  127.                                              if Length(scratchfilename) > 0 then
  128.                                              begin
  129.                                                   ExportPath := IncludeTrailingPathDelimiter(OutputFolder) + scratchfilename + '.' + FormatFloat('000', PartFileID);  //ACHTUNG
  130.                                                   ExportPathOG := IncludeTrailingPathDelimiter(OutputFolder) + scratchfilename + '.0001';
  131.                                                   ExportPathFinal := IncludeTrailingPathDelimiter(OutputFolder);
  132.                                                   OutputStream := TFileStream.Create(ExportPath, fmCreate);
  133.                                              end
  134.                                              else
  135.                                                  begin
  136.                                                       scratchfilename := 'tempfile';
  137.  
  138.                                                       OutputStream := TFileStream.Create(IncludeTrailingPathDelimiter(OutputFolder) + scratchfilename + '.' + FormatFloat('000', PartFileID), fmCreate);  //ACHTUNG
  139.                                                  end;
  140.                                              if OutPutStream.Handle > 0 then
  141.                                              begin
  142.                                                   BytesWritten := -1;
  143.                                                   BytesWritten := OutputStream.Write(InputBytesBuffer[0], intBytesRead);
  144.                                                   OutputStream.Free;
  145.                                              end;
  146.                                           finally
  147.                                           // nothing needed
  148.                                           end;
  149.  
  150.  
  151.                                      end;
  152.  
  153.  
  154.                  //Zusammensetzen der einzeln exportierten Teildateien
  155.                  lstrcpyw(Buf4, ExportPathOG);
  156.                  lstrcpyw(Buf3, ExportPathFinal);
  157.                  CombineFiles(ExportPathOG, ExportPathFinal + '.vmdk');
  158.  
  159.  
  160.                  //Importieren und öffnen der Datei zurück in XWF, in aktuellen Fall als eigenes 'Image'
  161.                  lstrcpyw(Buf2, ExportPath);
  162.                  FirstEvOCreated := XWF_CreateEvObj(1,0,ExportPath,0);
  163.  
  164.  
  165.  
  166.                  result := 0; //Rückgabewert für XT_ProcessItem
  167.                  end
  168.         else   //wenn keine vmdk-Datei
  169.             begin
  170.                  result := 0; //Rückgabewert für XT_ProcessItem
  171.             end;
  172. end;
  173.  

Thanks for everyone that replies its greatly appreciated  :) . Answers in german or english are OK.
« Last Edit: September 18, 2021, 02:30:26 am by Sestus »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: External SIGSEGV in Loop
« Reply #1 on: September 17, 2021, 09:14:05 pm »
Check where necessary, the size is needed in characters or in bytes. For example, initializing variables with FillChar in your code contains an error. Perhaps in other places when calling, you should also use SizeOf instead of Length.

Sestus

  • New Member
  • *
  • Posts: 14
Re: External SIGSEGV in Loop
« Reply #2 on: September 18, 2021, 02:29:59 am »
Wow, replacing Length with SizeOf actually helped and solved it, never would have guessed it THANKS A LOT :)

 

TinyPortal © 2005-2018