tobiasschulz / tar-cs

Automatically exported from code.google.com/p/tar-cs
Other
0 stars 0 forks source link

UpdateHeaderFromBytes conversion error #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Space and null characters can occur in Mode, UserId and GroupId header
strings 
2. Their conversion causes format exceptions while converting to int

What is the expected output? What do you see instead?
Expected output are properly converted header fields. Insted we get an
exception in Convert.ToInt32 function.

What version of the product are you using? On what operating system?
0.92

Please provide any additional information below.

Original issue reported on code.google.com by jure.mi...@gmail.com on 28 Oct 2009 at 11:27

GoogleCodeExporter commented 9 years ago
Yes, I reproduce this error also and it exactly throws:
FormatException (Could not find any recognizable digits.)

And this is the solution:
In TarHeaders.cs everywhere you find
'Convert.ToInt32(Encoding.ASCII.GetString(buffer, x, x), 8);'
add Trim() function like 'Convert.ToInt32(Encoding.ASCII.GetString(buffer, 100,
7).Trim(), 8);'

Original comment by pih...@gmail.com on 8 Dec 2009 at 8:16

GoogleCodeExporter commented 9 years ago
Get the latest version from SVN. It should be fine.

Original comment by vasilt...@gmail.com on 17 Jun 2011 at 8:25

GoogleCodeExporter commented 9 years ago
It's not fixed everywhere:
See line 123 and 125 in TarHeader.cs. I just ran into that.
https://code.google.com/p/tar-cs/source/browse/tar-cs/TarHeader.cs#123
https://code.google.com/p/tar-cs/source/browse/tar-cs/TarHeader.cs#125

Original comment by thefunkw...@gmail.com on 7 Aug 2014 at 3:17

GoogleCodeExporter commented 9 years ago
Additionally, .Trim() is not working in C# to get the "\0" removed.
See also 
http://stackoverflow.com/questions/3387733/how-to-remove-0-from-a-string-in-c

Instead of .Trim(), use .Trim().Replace("\0","") everywhere.

Example:
Mode = Convert.ToInt32(Encoding.ASCII.GetString(buffer, 100, 
7).Replace("\0",""), 8);

Or move that line of code in a dedicated method:

        private string TrimNulls(string input)
        {
            return input.Trim().Replace("\0", "");
        }

        public virtual bool UpdateHeaderFromBytes()
        {
            FileName = Encoding.ASCII.GetString(buffer, 0, 100);
            Mode = Convert.ToInt32(TrimNulls(Encoding.ASCII.GetString(buffer, 100, 7)), 8);

...

Original comment by thefunkw...@gmail.com on 14 Aug 2014 at 12:29