Recent

Author Topic: FilterPaeth Arithmetic Overflow  (Read 4561 times)

abtaylr

  • Full Member
  • ***
  • Posts: 107
FilterPaeth Arithmetic Overflow
« on: May 03, 2016, 03:41:52 am »
I have added a StringGrid to an application form -- nothing fancy, just fixed columns and rows added using the InsertRowWithValues method. I found that I needed to add the feature for ColClickSorts, since the output from the insertion was not always alphabetical.  The program compiles, but upon debugging it always fails once I click the header for the column that I was to be sorted.

I am getting an Arithmetic Overflow error from the FilterPaeth subroutine in the TFPReaderPNG.DoFilter in FPReadPNG.pp. In a simple example program I wrote to test the StringGrid, everthing worked as advertised, but my more complicated version seems to fail.

This is my procedure:

Code: Pascal  [Select][+][-]
  1. procedure TFormVOBProcessing.ReadVobList(var FPN : string);
  2. var
  3.   C, K : integer;
  4.   D, R : smallint;
  5.   FileName, FilePath : String;
  6.   FN, Ext, SearchMask : string;
  7.   SearchRec1, SearchRec2, SearchRec3 : TSearchRec;
  8. begin
  9.   FileListVideo.Create;
  10.   FileListVst.Create;
  11.   FileListVobSub.Create;
  12.  
  13.   FileName := ExtractFileName(FPN);
  14.   FilePath := ExtractFilePath(FPN);
  15.   Ext := ExtractFileExt(FPN);
  16.  
  17.   Case Ext of
  18.     '.IFO' :  SearchMask := LeftStr(FileName,7) + '*';
  19.     '.BUP' :  SearchMask := LeftStr(FileName,7) + '*';
  20.     '.VOB' :  SearchMask := LeftStr(FileName,7) + '*';
  21.     '.IDX' :  SearchMask := FileName;
  22.     '.SUB' :  SearchMask := FileName;
  23.   end;
  24.  
  25.   FileListVideo := FindAllFiles(FilePath, SearchMask, true, faDirectory);
  26.  
  27.   C := FileListVideo.Count - 1;
  28.  
  29.   R := 1;
  30.  
  31.   if LeftStr(SearchMask,8) <> 'VIDEO_TS' then
  32.   begin
  33.     if LeftStr(SearchMask,4) = 'VTS_' then
  34.     begin
  35.       for D := 0 to FileListVideo.Count -1 do
  36.       begin
  37.         if FindFirstUtf8(FilePath, faAnyFile and faDirectory, SearchRec1)=0 then
  38.         begin
  39.           repeat
  40.             With SearchRec1 do
  41.             begin
  42.               FN := FileListVideo.Strings[D];
  43.               Ext := ExtractFileExt(FN);
  44.               K := FileSizeUtf8(FileListVideo.Strings[D]);
  45.               Case Ext of
  46.                 '.IFO' :  StringGridVob.InsertRowWithValues(R,['0',
  47.                   ExtractFileName(FN), IntToStr(K)]);
  48.                 '.VOB' :  StringGridVob.InsertRowWithValues(R,['1',
  49.                   ExtractFileName(FN), IntToStr(K)]);
  50.                 '.BUP' :  Continue;
  51.                 '.srm' :  Continue;
  52.                 '.SRM' :  Continue;
  53.               end;
  54.             inc(R, 1);
  55.             end;
  56.           until FindNextUtf8(SearchRec1) <> 0;
  57.         end;
  58.         FindCloseUtf8(SearchRec1);
  59.       end;
  60.     end;
  61.   end;
  62.  
  63.   if Ext = '.IDX' then
  64.   begin
  65.     for D := 0 to FileListVideo.Count -1 do
  66.     begin
  67.       if FindFirstUtf8(FilePath, faAnyFile and faDirectory,SearchRec2)=0 then
  68.       begin
  69.         repeat
  70.           //inc(D, 1);
  71.           With SearchRec2 do
  72.           begin
  73.             FN := FileListVideo.Strings[D];
  74.             K := FileSizeUtf8(FileListVideo.Strings[D]);
  75.             StringGridVob.InsertRowWithValues(R,['1',
  76.               ExtractFileName(FN), IntToStr(K)]);
  77.             inc(R, 1);
  78.           end;
  79.         until FindNextUtf8(SearchRec2) <> 0;
  80.       end;
  81.       FindCloseUtf8(SearchRec2);
  82.     end;
  83.   end;
  84.  
  85.   if Ext = '.SUB' then
  86.   begin
  87.     for D := 0 to FileListVideo.Count -1 do
  88.     begin
  89.       if FindFirstUtf8(FilePath, faAnyFile and faDirectory,SearchRec3)=0 then
  90.       begin
  91.         repeat
  92.           With SearchRec3 do
  93.           begin
  94.             FN := FileListVideo.Strings[D];
  95.             K := FileSizeUtf8(FileListVideo.Strings[D]);
  96.             StringGridVob.InsertRowWithValues(R,['1',
  97.               ExtractFileName(FN), IntToStr(K)]);
  98.             inc(R, 1);
  99.           end;
  100.         until FindNextUtf8(SearchRec3) <> 0;
  101.       end;
  102.       FindCloseUtf8(SearchRec3);
  103.     end;
  104.   end;
  105.  
  106.   if LeftStr(SearchMask,5) = 'VIDEO' then
  107.     MessageDLG('Choose either a VTS_*.IFO, VTS_*.VOB or an *.IDX or *.SUB file',
  108.       mtInformation,[mbOk],0);
  109.  
  110.   LabelVobPath.Caption := FilePath;
  111.   LabelVobPath.Hint := FilePath;
  112.   RefreshLanguageList;
  113. end;

I have made a couple of debugging runs and I have attached the output.  Nearly the entirety of these files concerns this issue. I have only included a small part since they are upwards of 25mb long.  If anyone has any thoughts I am all ears.  Maybe my compiler switches are causing the problem or something similar.  Maybe it's a bug.


abtaylr

  • Full Member
  • ***
  • Posts: 107
Re: FilterPaeth Arithmetic Overflow
« Reply #1 on: May 03, 2016, 05:02:10 pm »
I have tried removing the compiler switches -Ci - IO checking and -Co Overflow checking, but no change.  The application still fails on line 357 of FilterPaeth -- r := l + p - lp.

I am using no optimization.  Currently, I am using the default fpu instructions and I have not selected any specific CPU.

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: FilterPaeth Arithmetic Overflow
« Reply #2 on: May 03, 2016, 05:05:52 pm »
Probably uninitialized variable somehere in your code or in lcl/fcl routines you're calling.

abtaylr

  • Full Member
  • ***
  • Posts: 107
Re: FilterPaeth Arithmetic Overflow
« Reply #3 on: May 03, 2016, 05:12:25 pm »
Ok, I'll run through my code and see what I can find.

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: FilterPaeth Arithmetic Overflow
« Reply #4 on: May 03, 2016, 06:00:00 pm »
I also see that your variable K isn't sized well. You're using it to get file size , so it shouldn't be integer ... look what data type FileSizeUTF8() returns, it's definitelly not integer so there may be range error.

abtaylr

  • Full Member
  • ***
  • Posts: 107
Re: FilterPaeth Arithmetic Overflow
« Reply #5 on: May 03, 2016, 08:11:32 pm »
That was a good catch, but it didn't solve the problem.

abtaylr

  • Full Member
  • ***
  • Posts: 107
Re: FilterPaeth Arithmetic Overflow
« Reply #6 on: May 03, 2016, 08:21:12 pm »
In the spirit of your last comment, I have also added SysToUtf8 for the ExtractFileName and ExtractFilePath functions since they return TRawByteStrings, but again, no joy.

abtaylr

  • Full Member
  • ***
  • Posts: 107
Re: FilterPaeth Arithmetic Overflow
« Reply #7 on: May 03, 2016, 08:27:00 pm »
Well SysToUtf8 just caused GDB to crash, so that wasn't the answer.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: FilterPaeth Arithmetic Overflow
« Reply #8 on: May 03, 2016, 09:21:54 pm »
Probably not the root cause of the crash, but there are several issues with your code

(1) You don't say what "FileListVideo" is - I suppose it is a TStringList. I've never seen something like "FileListVideo.Create". Instead, always specify the class type when creating class instances:
Code: Pascal  [Select][+][-]
  1.   FileListVideo := TStringList.Create;

(2) You must destroy all class instances that you create. This means: I am missing the "FileListVideo.Free" or "FreeAndNil(FileListVideo)". And the some for the other created FileList* things.

(3) "FindAllFiles" is a nasty function because it *creates* the stringlist for the found filenames and returns it as a function result. I don't like this construction at all, because people never are aware of it. This means that if you created FileListVideo as shown before now you have two instances, but you are using only one.

Depending on how often you call this function you will slowly run out of memory.

Quote
The application still fails on line 357 of FilterPaeth -- r := l + p - lp.
This is not contained in your code above. Please be aware: we're just ordinary people, we don't see what you don't show.

abtaylr

  • Full Member
  • ***
  • Posts: 107
[SOLVED]Re: FilterPaeth Arithmetic Overflow
« Reply #9 on: May 03, 2016, 11:16:51 pm »
I don't see any answer to the problem, so I have inserted:

StringGridVob.SortColRow(true,1);

as a workaround.  It solves the immediate issue.

Thanks to everyone for the assist.

 

TinyPortal © 2005-2018