Recent

Author Topic: [SOVLED] weird compiler behaviour (bug?)  (Read 458 times)

MMarie

  • New Member
  • *
  • Posts: 44
  • Right, lets bodge this pisspot
    • Homepage
[SOVLED] weird compiler behaviour (bug?)
« on: May 06, 2025, 03:00:34 am »
Hi,
I just encountered some weird compiler behaviour while working on my test project for my UEFI bindings and wanted to share it here. It feels like a compiler bug but I don't want to jump the horse so quickly  :D

The quick break down: I have a procedure for converting a UInt64 to a hexadecimal string and printing it via UEFI. The weird thing is that depending on the placement of another call to my Print function, only one character or all characters from my string are shown.

Snippet with correct behaviour:
Code: Pascal  [Select][+][-]
  1. procedure PrintHex(_int: UInt64);
  2. const
  3.         MAX_DIGITS = 32;
  4.         DIGITS: array of WideChar = ('0','1','2','3','4','5','6','7','8','9','A','B','C',
  5.                                                                  'D','E','F');
  6. var
  7.         wix, ix: Integer;
  8.         b: UInt8;
  9.         c: array [0..1] of WideChar;
  10.         _str: array [0..MAX_DIGITS] of WideChar;
  11. begin
  12.         c[0] := '$';
  13.         c[1] := WideChar($0000);
  14.  
  15.         wix := High(_str);
  16.         _str[wix] := WideChar($0000);
  17.         Dec(wix);
  18.  
  19.         Print(@c[0]);
  20.  
  21.         repeat
  22.                 _str[wix] := DIGITS[_int mod 16];
  23.                 _int := _int div 16;
  24.                 Inc(ix);
  25.                 Dec(wix);
  26.         until (_int = 0) or (ix >= MAX_DIGITS);
  27.  
  28.         Print(PWideChar(_str) + wix);
  29. end;
  30.  

Snippet with incorrect behaviour:
Code: Pascal  [Select][+][-]
  1. procedure PrintHex(_int: UInt64);
  2. const
  3.         MAX_DIGITS = 32;
  4.         DIGITS: array of WideChar = ('0','1','2','3','4','5','6','7','8','9','A','B','C',
  5.                                                                  'D','E','F');
  6. var
  7.         wix, ix: Integer;
  8.         b: UInt8;
  9.         c: array [0..1] of WideChar;
  10.         _str: array [0..MAX_DIGITS] of WideChar;
  11. begin
  12.         c[0] := '$';
  13.         c[1] := WideChar($0000);
  14.  
  15.         Print(@c[0]);
  16.  
  17.         wix := High(_str);
  18.         _str[wix] := WideChar($0000);
  19.         Dec(wix);
  20.  
  21.         repeat
  22.                 _str[wix] := DIGITS[_int mod 16];
  23.                 _int := _int div 16;
  24.                 Inc(ix);
  25.                 Dec(wix);
  26.         until (_int = 0) or (ix >= MAX_DIGITS);
  27.  
  28.         Print(PWideChar(_str) + wix);
  29. end;
  30.  

(If you want to see it in full context, the code is available here)

EDIT: please also go easy on me, especially in regards to my code, i am very sleep deprived  %)
« Last Edit: May 06, 2025, 10:27:11 am by MMarie »
i use arch btw

Fibonacci

  • Hero Member
  • *****
  • Posts: 753
  • Internal Error Hunter
Re: weird compiler behaviour (bug?)
« Reply #1 on: May 06, 2025, 03:38:25 am »
Thanks. I dont know the answer to your problem, but thanks to you I just found another Internal Error (2013032603). I collect them. #41249

Code: Pascal  [Select][+][-]
  1. var
  2.   c: char;
  3.   s: string;
  4.  
  5. begin
  6.   Str(c, s); // Error: Internal error 2013032603
  7. end.

Fibonacci

  • Hero Member
  • *****
  • Posts: 753
  • Internal Error Hunter
Re: weird compiler behaviour (bug?)
« Reply #2 on: May 06, 2025, 03:58:34 am »
I dont see the "ix" initialized, I guess it may be unpredictable

MMarie

  • New Member
  • *
  • Posts: 44
  • Right, lets bodge this pisspot
    • Homepage
Re: weird compiler behaviour (bug?)
« Reply #3 on: May 06, 2025, 10:26:46 am »
I dont see the "ix" initialized, I guess it may be unpredictable

Oh yeah, you are completly correct. I overlooked that completly. A bit of a look into it shows that "ix" just happend to equal MAX_DIGITS - 1 when I forgot to initialise it.

Although now I'm wondering what I screwed up in my Makefile that I'm not getting any compiler warnings  :D
i use arch btw

 

TinyPortal © 2005-2018