View Issue Details

IDProjectCategoryView StatusLast Update
0035161FPCFCLpublic2019-03-02 11:21
ReporterDoyenneAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product VersionProduct Build 
Target Version3.2.0Fixed in Version3.3.1 
Summary0035161: fcl-base/base64.pp: DecodeStringBase64('') raises exception
DescriptionIf DecodeStringBase64 is called with an empty string as parameter, it raises an exception.

Expected behaviour
I think it should return an empty string, as explained here:
https://stackoverflow.com/questions/15212127/is-an-empty-string-valid-base64-encoded-data-of-zero-bytes-length

In addition, the trunk version of base64.pp has the following code in EncodeStringBase64():
  if Length(s)=0 then
    Exit('');

Therefore, '' is encoded into '' and as such, '' should be decoded into '' as well.

Reason of the exception
DecodeStringBase64() uses the following code:

Outstream.CopyFrom(Decoder,Decoder.Size);

Decoder.Size is correctly calculated as 0. If a stream's CopyFrom() is called with Size=0, it executes TStream.Position := 0. However, TBase64DecodingStream.Seek() is not yet implemented and raises an exception.

Solution
The solution is easy. In trunk, a check was added in EncodeStringBase64() (see above). The same check should be added to DecodeStringBase64() to solve the issue.

See attachment for diff.
TagsNo tags attached.
Fixed in Revision41545.
FPCOldBugId
FPCTarget
Attached Files
  • solution_base64.diff (355 bytes)
    Index: src/base64.pp
    ===================================================================
    --- src/base64.pp	(revision 41518)
    +++ src/base64.pp	(working copy)
    @@ -425,6 +425,8 @@
       Outstream : TStringStream;
       Decoder   : TBase64DecodingStream;
     begin
    +  if Length(s)=0 then
    +    Exit('');
       SD:=S;
       while Length(Sd) mod 4 > 0 do 
         SD := SD + '=';
    
    solution_base64.diff (355 bytes)

Activities

Doyenne

2019-02-27 21:36

reporter  

solution_base64.diff (355 bytes)
Index: src/base64.pp
===================================================================
--- src/base64.pp	(revision 41518)
+++ src/base64.pp	(working copy)
@@ -425,6 +425,8 @@
   Outstream : TStringStream;
   Decoder   : TBase64DecodingStream;
 begin
+  if Length(s)=0 then
+    Exit('');
   SD:=S;
   while Length(Sd) mod 4 > 0 do 
     SD := SD + '=';
solution_base64.diff (355 bytes)

Michael Van Canneyt

2019-03-02 11:21

administrator   ~0114550

You are right, this is more consistent. Thanks for the patch!

Issue History

Date Modified Username Field Change
2019-02-27 21:36 Doyenne New Issue
2019-02-27 21:36 Doyenne File Added: solution_base64.diff
2019-03-02 08:22 Michael Van Canneyt Assigned To => Michael Van Canneyt
2019-03-02 08:22 Michael Van Canneyt Status new => assigned
2019-03-02 11:21 Michael Van Canneyt Fixed in Revision => 41545.
2019-03-02 11:21 Michael Van Canneyt Note Added: 0114550
2019-03-02 11:21 Michael Van Canneyt Status assigned => resolved
2019-03-02 11:21 Michael Van Canneyt Fixed in Version => 3.3.1
2019-03-02 11:21 Michael Van Canneyt Resolution open => fixed
2019-03-02 11:21 Michael Van Canneyt Target Version => 3.2.0