Hello,
it seems that the value that start to lead to wrong values is 2048M.
Also the returned hash from there is always the same as for an empty file.

I first thought it has something to do with large file support, but it
looks like that is already enabled.

  (gdb) print sizeof(off_t)
  $1 = 8                                                                        
                                                                                
                    

So I think this is caused by using default int for variable val and size.
That way the read buffer size is not determined correctly and then
STREAMReadBytes called with a negative buffer size.

  int result, val, RetVal=FALSE;
  ...
  val=FileSize;
  if ((val==0) || ( val > BUFSIZ)) val=BUFSIZ;
  result=STREAMReadBytes(S,Tempstr,val);

At the end it is not checked if read bytes matches the file size.
Therefore always the hash for an empty file is printed.


Attached patch modifies just as much to get a result matching
the 'sha256sum' utility.


Kind regards,
Bernhard




(gdb) bt
#0  STREAMReadBytes (S=0x55555578d4d0, Buffer=0x555555791560 "", 
Buffsize=-2147483648) at file.c:852
#1  0x000055555555b894 in HashratHashFile (Ctx=<optimized out>, 
Hash=0x55555578d360, Type=0, Path=<optimized out>, FileSize=-2147483648) at 
files.c:202
#2  0x000055555555bb38 in HashratHashSingleFile (Ctx=0x555555789080, 
HashType=<optimized out>, FileType=0, Path=0x7fffffffe1cb "../../test-2048M", 
FStat=0x7fffffffdbe0, RetStr=0x7fffffffdb70) at files.c:280
#3  0x000055555555bf0e in HashItem (Ctx=0x555555789080, HashType=0x55555578d200 
"sha512", Path=0x7fffffffe1cb "../../test-2048M", FStat=0x7fffffffdbe0, 
HashStr=0x7fffffffdb70) at files.c:400
#4  0x000055555555bfd2 in HashratAction (Ctx=0x555555789080, 
Path=0x7fffffffe1cb "../../test-2048M", Stat=0x7fffffffdbe0) at files.c:436
#5  0x000055555555c55d in ProcessItem (Ctx=0x555555789080, Path=0x7fffffffe1cb 
"../../test-2048M", Stat=0x7fffffffdbe0) at files.c:637
#6  0x000055555555eb1f in ProcessCommandLine (Ctx=0x555555789080, 
argc=<optimized out>, argv=<optimized out>) at main.c:114
#7  0x0000555555557301 in main (argc=3, argv=0x7fffffffde68) at main.c:149

(gdb) up
#1  0x000055555555b894 in HashratHashFile (Ctx=<optimized out>, 
Hash=0x55555578d360, Type=0, Path=<optimized out>, FileSize=-2147483648) at 
files.c:202
202     result=STREAMReadBytes(S,Tempstr,val);

(gdb) print val
$3 = -2147483648
(gdb) print FileSize
$4 = -2147483648
From e54af652164b5aec4d836d0fe9c11ba080b284c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bernhard=20=C3=9Cbelacker?= <bernha...@mailbox.org>
Date: Sat, 27 May 2017 18:25:54 +0200
Subject: Avoid integer truncation on file size assignment if greater than 2G.

https://bugs.debian.org/863460
---
 files.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/files.c b/files.c
index 5c6de63..840e8d2 100644
--- a/files.c
+++ b/files.c
@@ -168,7 +168,8 @@ int HashratHashFile(HashratCtx *Ctx, THash *Hash, int Type, char *Path, off_t Fi
 {
 STREAM *S;
 char *Tempstr=NULL, *User=NULL, *Pass=NULL;
-int result, val, RetVal=FALSE;
+int result, RetVal=FALSE;
+off_t val;
 off_t bytes_read=0;
 
 switch (Type)
@@ -254,7 +255,7 @@ int HashratHashSingleFile(HashratCtx *Ctx, char *HashType, int FileType, char *P
 THash *Hash;
 struct stat XattrStat;
 char *ptr;
-int size=0;
+off_t size=0;
 
 		*RetStr=CopyStr(*RetStr,"");
 
-- 
2.11.0

Reply via email to