On Sat, Apr 5, 2014 at 9:03 PM, Linus Torvalds <
[email protected]> wrote:

>
> On Apr 5, 2014 1:45 AM, "Venkatesh Shukla" <
> [email protected]> wrote:
> >
> > Arguments 2 and 3 in fread() seemed to be exchanged because of which
> > only one character was read into the temp xml file.
>
> This explanation confused me, because the order of arguments ends up being
> totally immaterial to the number of characters read by fread().
>
> So the commit message is misleading.
>
> The reason the patch matters is not because it changes the amount if data
> read, but because it changes the return value. The fread function returns
> the number of elements read, not the number of bytes.
>
> So if the difference is that with the
>
>    streamsize, 1
>
> order, the return value ends up begin 1 for the "I read everything" case.
> And for the
>
>   1,  streamsize
>
> case it ends up returning 'streamsize'. Because in the first case it read
> *one* element of size 'streamsize' and in the second case it read
> 'streamsize' elements of a single char each.
>
> I personally think the fread/fwrite interfaces are stupid and much too
> easy to get wrong, but whatever. They are what they are.
>
> BTW, I don't think 'sizeof(char)' makes any sense. In C, that is just a
> complex way to write 1.  It doesn't matter how many bits 'char' has, sizeof
> is always in terms of char. That's the definition of sizeof.
>
> So if you were to have a machine with a sixteen bit char, and a 32-bit
> 'int' then sizeof(char) is still one, and sizeof(int) would be two.
>
>             Linus
>
Thank you Linus

I was ignorant of the above two facts. I'll keep them in mind.
I have attached the updated patch which includes correct explanation of
error.

Regards.
-- 

*Venkatesh Shukla *
From 20e91e1f4d1eab720b5e0d02be8950d674b355c0 Mon Sep 17 00:00:00 2001
From: Venkatesh Shukla <[email protected]>
Date: Sat, 5 Apr 2014 20:30:46 +0530
Subject: [PATCH] Fixes divelogs.de upload error #483

In case of arguments "streamsize, 1" the returning value is 1, and
hence due to membuf[streamsize] = 0; line, membuf ends up being one
character long.
Fixed it by exchanging arguments to "1, streamsize". This way,
streamsize retains its value.

Signed-off-by: Venkatesh Shukla <[email protected]>
Acked-by: Lubomir I. Ivanov <[email protected]>
---
 qt-ui/subsurfacewebservices.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index 7077465..a8bce4a 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -159,7 +159,7 @@ bool DivelogsDeWebServices::prepare_dives_for_divelogs(const QString &tempfile,
 		rewind(f);
 
 		membuf = (char *)malloc(streamsize + 1);
-		if (!membuf || (streamsize = fread(membuf, streamsize, 1, f)) == 0) {
+		if (!membuf || (streamsize = fread(membuf, 1, streamsize, f)) == 0) {
 			report_error(tr("internal error: %s").toUtf8(), qt_error_string().toUtf8().data());
 			fclose(f);
 			free((void *)membuf);
-- 
1.9.0

_______________________________________________
subsurface mailing list
[email protected]
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to