[fpc-devel] Incompatible assignments but no compile error (char array and shortstring)
Wayne Sherman
wsherman at gmail.com
Sun Apr 17 00:39:11 CEST 2022
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
<fpc-devel at lists.freepascal.org> 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 - fpc-devel at lists.freepascal.org
> > 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 - fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
More information about the fpc-devel
mailing list