Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2022-04

Incompatible assignments but no compile error (char array and shortstring)

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Wayne Sherman

Tested with fpc 3.3.1 trunk (as of 2022-Mar-12) and 3.2.2 stable. Ubuntu 20.04 64-bit

Good compile time error checking is one of the wonderful things about Pascal. But some char array and shortstring assignments which are not size compatible do not produce a compile error as expected. Please see below.

{$mode objfpc}{$H+}
type
  TCharArray8    = packed array[0..7] of Char;
  TCharArray9    = packed array[0..8] of Char;
  TShortString8  = string[8];
const
  StringConst8 = '12345678';
  StringConst9 = '123456789';
  CharArrayConst8a: TCharArray8 = '12345678';
  CharArrayConst8b: TCharArray8 = StringConst8;
  CharArrayConst8c: TCharArray8 = ('1','2','3','4','5','6','7','8');
  //CharArrayConst8d: TCharArray8 = '123456789';   //compile error as expected
  //CharArrayConst8e: TCharArray8 = StringConst9;  //compile error as expected
  //CharArrayConst8f: TCharArray8 =
('1','2','3','4','5','6','7','8','9'); //compile error as expected
  ShortStringConst8a: TShortString8 = '12345678';
  ShortStringConst8b: TShortString8 = StringConst8;
  ShortStringConst8c: TShortString8 = '123456789';   //did not get
expected compile error (bug?)
  ShortStringConst8d: TShortString8 = StringConst9;  //did not get
expected compile error (bug?)
var
  CharArrayVar8:   TCharArray8;
  CharArrayVar9:   TCharArray9;
  ShortStringVar8: TShortString8;
begin
  CharArrayVar8 := '12345678';
  CharArrayVar8 := StringConst8;
  CharArrayVar8 := ['1','2','3','4','5','6','7','8'];  //requires fpc 3.3.1
  CharArrayVar8 := CharArrayConst8c;
  //CharArrayVar8 := ['1','2','3','4','5','6','7','8','9']; //compile
error as expected
  CharArrayVar8 := '123456789';   //did not get expected compile error (bug?)
  CharArrayVar8 := StringConst9;  //did not get expected compile error (bug?)
  CharArrayVar8 := ShortStringVar8;
  ShortStringVar8 := CharArrayVar8;
  ShortStringVar8 := '12345678';
  ShortStringVar8 := CharArrayConst8a;
  ShortStringVar8 := TCharArray8(['1','2','3','4','5','6','7','8']);
//requires fpc 3.3.1
  ShortStringVar8 := '123456789';   //did not get expected compile error (bug?)
  ShortStringVar8 := CharArrayVar9; //did not get expected compile error (bug?)
  ShortStringVar8 :=
TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get
expected compile error (bug?) (requires fpc 3.3.1)
end;

Should I open a couple of bug reports?

Reply by email

J. Gareth Moreton
to Wayne Sherman

It does look like a bug on the surface.  What happens if you run the program with these false negatives?  If you get a buffer overrun, then it is pretty serious.

Gareth aka. Kit

Wayne Sherman wrote 71 lines
Tested with fpc 3.3.1 trunk (as of 2022-Mar-12) and 3.2.2 stable. Ubuntu 20.04 64-bit Good compile time error checking is one of the wonderful things about Pascal. But some char array and shortstring assignments which are not size compatible do not produce a compile error as expected. Please see below. {$mode objfpc}{$H+} type TCharArray8 = packed array[0..7] of Char; TCharArray9 = packed array[0..8] of Char; TShortString8 = string[8]; const StringConst8 = '12345678'; StringConst9 = '123456789'; CharArrayConst8a: TCharArray8 = '12345678'; CharArrayConst8b: TCharArray8 = StringConst8; CharArrayConst8c: TCharArray8 = ('1','2','3','4','5','6','7','8'); //CharArrayConst8d: TCharArray8 = '123456789'; //compile error as expected //CharArrayConst8e: TCharArray8 = StringConst9; //compile error as expected //CharArrayConst8f: TCharArray8 = ('1','2','3','4','5','6','7','8','9'); //compile error as expected ShortStringConst8a: TShortString8 = '12345678'; ShortStringConst8b: TShortString8 = StringConst8; ShortStringConst8c: TShortString8 = '123456789'; //did not get expected compile error (bug?) ShortStringConst8d: TShortString8 = StringConst9; //did not get expected compile error (bug?) var CharArrayVar8: TCharArray8; CharArrayVar9: TCharArray9; ShortStringVar8: TShortString8; begin CharArrayVar8 := '12345678'; CharArrayVar8 := StringConst8; CharArrayVar8 := ['1','2','3','4','5','6','7','8']; //requires fpc 3.3.1 CharArrayVar8 := CharArrayConst8c; //CharArrayVar8 := ['1','2','3','4','5','6','7','8','9']; //compile error as expected CharArrayVar8 := '123456789'; //did not get expected compile error (bug?) CharArrayVar8 := StringConst9; //did not get expected compile error (bug?) CharArrayVar8 := ShortStringVar8; ShortStringVar8 := CharArrayVar8; ShortStringVar8 := '12345678'; ShortStringVar8 := CharArrayConst8a; ShortStringVar8 := TCharArray8(['1','2','3','4','5','6','7','8']); //requires fpc 3.3.1 ShortStringVar8 := '123456789'; //did not get expected compile error (bug?) ShortStringVar8 := CharArrayVar9; //did not get expected compile error (bug?) ShortStringVar8 := TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get expected compile error (bug?) (requires fpc 3.3.1) end; Should I open a couple of bug reports? _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Reply by email

Wayne Sherman
to J. Gareth Moreton
Gareth aka. Kit wrote
What happens if you run the program with these false negatives? If you get a buffer overrun, then it is pretty serious.

When running in debug mode, I get no range check or overflow errors. A 9-byte sized value when assigned to an 8-byte sized variable gets truncated to 8-bytes.

I missed these at first, but when I compile I am getting these warnings:
const
  ShortStringConst8c: TShortString8 = '123456789';   //did not get
expected compile error (bug?)
  (unit1.pas(53,50) Warning: String "123456789" is longer than "8")
  ShortStringConst8d: TShortString8 = StringConst9;  //did not get
expected compile error (bug?)
  (unit1.pas(54,51) Warning: String "123456789" is longer than "8")
begin
...
  ShortStringVar8 := '123456789';   //did not get expected compile error (bug?)
  (unit1.pas(92,19) Warning: String literal has more characters than
short string length)
But no compile warnings or errors on these:
begin
...
  CharArrayVar8 := '123456789';   //did not get expected compile
errors or warnings
  CharArrayVar8 := StringConst9;  //did not get expected compile
errors or warnings
  ShortStringVar8 := CharArrayVar9; //did not get expected compile
errors or warnings
  ShortStringVar8 :=
TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get
expected compile errors or warnings
  ShortStringVar8 := CharArrayConst9;  //did not get expected compile
errors or warnings

So it looks like the compiler does not treat string compatible types as strictly as other types and when assigning it copies as many characters as will fit. But it is not consistent in generating warnings.

PM J. Gareth Moreton wrote 87 lines
It does look like a bug on the surface. What happens if you run the program with these false negatives? If you get a buffer overrun, then it is pretty serious. Gareth aka. Kit On 16/04/2022 21:35, Wayne Sherman via fpc-devel wrote: > Tested with fpc 3.3.1 trunk (as of 2022-Mar-12) and 3.2.2 stable. > Ubuntu 20.04 64-bit > > Good compile time error checking is one of the wonderful things about > Pascal. But some char array and shortstring assignments which are not > size compatible do not produce a compile error as expected. Please > see below. > > {$mode objfpc}{$H+} > type > TCharArray8 = packed array[0..7] of Char; > TCharArray9 = packed array[0..8] of Char; > TShortString8 = string[8]; > > const > StringConst8 = '12345678'; > StringConst9 = '123456789'; > > CharArrayConst8a: TCharArray8 = '12345678'; > CharArrayConst8b: TCharArray8 = StringConst8; > CharArrayConst8c: TCharArray8 = ('1','2','3','4','5','6','7','8'); > > //CharArrayConst8d: TCharArray8 = '123456789'; //compile error as expected > //CharArrayConst8e: TCharArray8 = StringConst9; //compile error as expected > //CharArrayConst8f: TCharArray8 = > ('1','2','3','4','5','6','7','8','9'); //compile error as expected > > ShortStringConst8a: TShortString8 = '12345678'; > ShortStringConst8b: TShortString8 = StringConst8; > ShortStringConst8c: TShortString8 = '123456789'; //did not get > expected compile error (bug?) > ShortStringConst8d: TShortString8 = StringConst9; //did not get > expected compile error (bug?) > > var > CharArrayVar8: TCharArray8; > CharArrayVar9: TCharArray9; > ShortStringVar8: TShortString8; > > begin > CharArrayVar8 := '12345678'; > CharArrayVar8 := StringConst8; > CharArrayVar8 := ['1','2','3','4','5','6','7','8']; //requires fpc 3.3.1 > CharArrayVar8 := CharArrayConst8c; > > //CharArrayVar8 := ['1','2','3','4','5','6','7','8','9']; //compile > error as expected > CharArrayVar8 := '123456789'; //did not get expected compile error (bug?) > CharArrayVar8 := StringConst9; //did not get expected compile error (bug?) > > CharArrayVar8 := ShortStringVar8; > ShortStringVar8 := CharArrayVar8; > > ShortStringVar8 := '12345678'; > ShortStringVar8 := CharArrayConst8a; > ShortStringVar8 := TCharArray8(['1','2','3','4','5','6','7','8']); > //requires fpc 3.3.1 > > ShortStringVar8 := '123456789'; //did not get expected compile error (bug?) > ShortStringVar8 := CharArrayVar9; //did not get expected compile error (bug?) > ShortStringVar8 := > TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get > expected compile error (bug?) (requires fpc 3.3.1) > > end; > > Should I open a couple of bug reports? > _______________________________________________ > fpc-devel maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel > -- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

J. Gareth Moreton
to Wayne Sherman

It looks like it throws warnings if the compiler is able to recover (e.g. by truncating the literal).  If something is too short though, then it can't recover because it has no idea what to fill the end with.

If you include the warnings, are there any places where messages are not thrown?  I can't quite tell.

Gareth aka. Kit

Wayne Sherman wrote 132 lines
Gareth aka. Kit wrote: > What happens if you run the program with these > false negatives? If you get a buffer overrun, then > it is pretty serious. When running in debug mode, I get no range check or overflow errors. A 9-byte sized value when assigned to an 8-byte sized variable gets truncated to 8-bytes. I missed these at first, but when I compile I am getting these warnings: const ShortStringConst8c: TShortString8 = '123456789'; //did not get expected compile error (bug?) (unit1.pas(53,50) Warning: String "123456789" is longer than "8") ShortStringConst8d: TShortString8 = StringConst9; //did not get expected compile error (bug?) (unit1.pas(54,51) Warning: String "123456789" is longer than "8") begin ... ShortStringVar8 := '123456789'; //did not get expected compile error (bug?) (unit1.pas(92,19) Warning: String literal has more characters than short string length) But no compile warnings or errors on these: begin ... CharArrayVar8 := '123456789'; //did not get expected compile errors or warnings CharArrayVar8 := StringConst9; //did not get expected compile errors or warnings ShortStringVar8 := CharArrayVar9; //did not get expected compile errors or warnings ShortStringVar8 := TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get expected compile errors or warnings ShortStringVar8 := CharArrayConst9; //did not get expected compile errors or warnings So it looks like the compiler does not treat string compatible types as strictly as other types and when assigning it copies as many characters as will fit. But it is not consistent in generating warnings. On Sat, Apr 16, 2022 at 2:24 PM J. Gareth Moreton via fpc-devel wrote: > It does look like a bug on the surface. What happens if you run the > program with these false negatives? If you get a buffer overrun, then > it is pretty serious. > > Gareth aka. Kit > > On 16/04/2022 21:35, Wayne Sherman via fpc-devel wrote: >> Tested with fpc 3.3.1 trunk (as of 2022-Mar-12) and 3.2.2 stable. >> Ubuntu 20.04 64-bit >> >> Good compile time error checking is one of the wonderful things about >> Pascal. But some char array and shortstring assignments which are not >> size compatible do not produce a compile error as expected. Please >> see below. >> >> {$mode objfpc}{$H+} >> type >> TCharArray8 = packed array[0..7] of Char; >> TCharArray9 = packed array[0..8] of Char; >> TShortString8 = string[8]; >> >> const >> StringConst8 = '12345678'; >> StringConst9 = '123456789'; >> >> CharArrayConst8a: TCharArray8 = '12345678'; >> CharArrayConst8b: TCharArray8 = StringConst8; >> CharArrayConst8c: TCharArray8 = ('1','2','3','4','5','6','7','8'); >> >> //CharArrayConst8d: TCharArray8 = '123456789'; //compile error as expected >> //CharArrayConst8e: TCharArray8 = StringConst9; //compile error as expected >> //CharArrayConst8f: TCharArray8 = >> ('1','2','3','4','5','6','7','8','9'); //compile error as expected >> >> ShortStringConst8a: TShortString8 = '12345678'; >> ShortStringConst8b: TShortString8 = StringConst8; >> ShortStringConst8c: TShortString8 = '123456789'; //did not get >> expected compile error (bug?) >> ShortStringConst8d: TShortString8 = StringConst9; //did not get >> expected compile error (bug?) >> >> var >> CharArrayVar8: TCharArray8; >> CharArrayVar9: TCharArray9; >> ShortStringVar8: TShortString8; >> >> begin >> CharArrayVar8 := '12345678'; >> CharArrayVar8 := StringConst8; >> CharArrayVar8 := ['1','2','3','4','5','6','7','8']; //requires fpc 3.3.1 >> CharArrayVar8 := CharArrayConst8c; >> >> //CharArrayVar8 := ['1','2','3','4','5','6','7','8','9']; //compile >> error as expected >> CharArrayVar8 := '123456789'; //did not get expected compile error (bug?) >> CharArrayVar8 := StringConst9; //did not get expected compile error (bug?) >> >> CharArrayVar8 := ShortStringVar8; >> ShortStringVar8 := CharArrayVar8; >> >> ShortStringVar8 := '12345678'; >> ShortStringVar8 := CharArrayConst8a; >> ShortStringVar8 := TCharArray8(['1','2','3','4','5','6','7','8']); >> //requires fpc 3.3.1 >> >> ShortStringVar8 := '123456789'; //did not get expected compile error (bug?) >> ShortStringVar8 := CharArrayVar9; //did not get expected compile error (bug?) >> ShortStringVar8 := >> TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get >> expected compile error (bug?) (requires fpc 3.3.1) >> >> end; >> >> Should I open a couple of bug reports? >> _______________________________________________ >> fpc-devel maillist - >> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel >> > -- > This email has been checked for viruses by Avast antivirus software. > https://www.avast.com/antivirus > > _______________________________________________ > fpc-devel maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Reply by email

Sven Barth
to Wayne Sherman: Tested with fpc 3.3.1 trunk (as of 2022-Mar-12) and 3.2.2 stable. Ubuntu 20.04 64-bit…
Wayne Sherman wrote 67 lines
Tested with fpc 3.3.1 trunk (as of 2022-Mar-12) and 3.2.2 stable. Ubuntu 20.04 64-bit Good compile time error checking is one of the wonderful things about Pascal. But some char array and shortstring assignments which are not size compatible do not produce a compile error as expected. Please see below. {$mode objfpc}{$H+} type TCharArray8 = packed array[0..7] of Char; TCharArray9 = packed array[0..8] of Char; TShortString8 = string[8]; const StringConst8 = '12345678'; StringConst9 = '123456789'; CharArrayConst8a: TCharArray8 = '12345678'; CharArrayConst8b: TCharArray8 = StringConst8; CharArrayConst8c: TCharArray8 = ('1','2','3','4','5','6','7','8'); //CharArrayConst8d: TCharArray8 = '123456789'; //compile error as expected //CharArrayConst8e: TCharArray8 = StringConst9; //compile error as expected //CharArrayConst8f: TCharArray8 = ('1','2','3','4','5','6','7','8','9'); //compile error as expected ShortStringConst8a: TShortString8 = '12345678'; ShortStringConst8b: TShortString8 = StringConst8; ShortStringConst8c: TShortString8 = '123456789'; //did not get expected compile error (bug?) ShortStringConst8d: TShortString8 = StringConst9; //did not get expected compile error (bug?) var CharArrayVar8: TCharArray8; CharArrayVar9: TCharArray9; ShortStringVar8: TShortString8; begin CharArrayVar8 := '12345678'; CharArrayVar8 := StringConst8; CharArrayVar8 := ['1','2','3','4','5','6','7','8']; //requires fpc 3.3.1 CharArrayVar8 := CharArrayConst8c; //CharArrayVar8 := ['1','2','3','4','5','6','7','8','9']; //compile error as expected CharArrayVar8 := '123456789'; //did not get expected compile error (bug?) CharArrayVar8 := StringConst9; //did not get expected compile error (bug?) CharArrayVar8 := ShortStringVar8; ShortStringVar8 := CharArrayVar8; ShortStringVar8 := '12345678'; ShortStringVar8 := CharArrayConst8a; ShortStringVar8 := TCharArray8(['1','2','3','4','5','6','7','8']); //requires fpc 3.3.1 ShortStringVar8 := '123456789'; //did not get expected compile error (bug?) ShortStringVar8 := CharArrayVar9; //did not get expected compile error (bug?) ShortStringVar8 := TCharArray9(['1','2','3','4','5','6','7','8','9']); //did not get expected compile error (bug?) (requires fpc 3.3.1) end; Should I open a couple of bug reports?

For ShortString types it is by design that they're truncated implicitely (the most that current Delphi versions do is issue a warning). However the assignments to the static char array should be checked.

Regards,
Sven

Reply by email