curl/src
Emanuele Torre 77a6bf8489
tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3)
This loop was using the number of bytes read from the file as condition
to keep reading.

From Linux's fread(3) man page:
> On success, fread() and fwrite() return the number of items read or
> written. This number equals the number of bytes transferred only when
> size is 1. If an error occurs, or the end of the file is reached, the
> return value is a short item count (or zero).
>
> The file position indicator for the stream is advanced by the number
> of bytes successfully read or written.
>
> fread() does not distinguish between end-of-file and error, and
> callers must use feof(3) and ferror(3) to determine which occurred.

This means that nread!=0 doesn't make much sense as an end condition for
the loop: nread==0 doesn't necessarily mean that EOF has been reached or
an error has occured (but that is usually the case) and nread!=0 doesn't
necessarily mean that EOF has not been reached or that no read errors
have occured. feof(3) and ferror(3) should be uses when using fread(3).

Currently curl has to performs an extra fread(3) call to get a return
value equal to 0 to stop looping.

This usually "works" (even though nread==0 shouldn't be interpreted as
EOF) if stdin is a pipe because EOF usually marks the "real" end of the
stream, so the extra fread(3) call will return immediately and the extra
read syscall won't be noticeable:

    bash-5.1$ strace -e read curl -s -F file=@- 0x0.st <<< a 2>&1 |
    > tail -n 5
    read(0, "a\n", 4096)                    = 2
    read(0, "", 4096)                       = 0
    read(0, "", 4096)                       = 0
    http://0x0.st/oRs.txt
    +++ exited with 0 +++
    bash-5.1$

But this doesn't work if curl is reading from stdin, stdin is a
terminal, and the EOF is being emulated using a shell with ^D. Two
consecutive ^D will be required in this case to actually make curl stop
reading:

    bash-5.1$ curl -F file=@- 0x0.st
    a
    ^D^D
    http://0x0.st/oRs.txt
    bash-5.1$

A possible workaround to this issue is to use a program that handles EOF
correctly to indirectly send data to curl's stdin:

    bash-5.1$ cat - | curl -F file=@- 0x0.st
    a
    ^D
    http://0x0.st/oRs.txt
    bash-5.1$

This patch makes curl handle EOF properly when using fread(3) in
file2memory() so that the workaround is not necessary.

Since curl was previously ignoring read errors caused by this fread(3),
ferror(3) is also used in the condition of the loop: read errors and EOF
will have the same meaning; this is done to somewhat preserve the old
behaviour instead of making the command fail when a read error occurs.

Closes #8701
2022-04-17 11:36:28 +02:00
..
macos copyright: fix out-of-date copyright ranges and missing headers 2020-03-24 15:05:59 +01:00
.gitignore VC: remove the makefile.vc6 build infra 2017-01-23 14:27:32 +01:00
CMakeLists.txt copyright: update copyright year ranges to 2021 2021-05-26 08:18:11 +02:00
curl.rc copyright: fix year ranges 2020-11-05 08:22:10 +01:00
Makefile.am scripts: move three scripts from lib/ to scripts/ 2022-03-23 15:26:11 +01:00
makefile.amiga curl.se: new home 2020-11-04 23:59:47 +01:00
makefile.dj copyright: fix missing year (range) updates 2021-01-29 14:35:13 +01:00
Makefile.inc lib: remove support for CURL_DOES_CONVERSIONS 2022-02-04 08:05:35 +01:00
Makefile.m32 Makefile.m32: rename -winssl option to -schannel and tidy up 2021-11-25 17:36:38 +00:00
mkhelp.pl copyright: fix year ranges 2020-11-05 08:22:10 +01:00
slist_wc.c copyright: fix year ranges 2020-11-05 08:22:10 +01:00
slist_wc.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_binmode.c copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_binmode.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_bname.c copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_bname.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_cb_dbg.c lib: remove support for CURL_DOES_CONVERSIONS 2022-02-04 08:05:35 +01:00
tool_cb_dbg.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_cb_hdr.c tool_cb_hdr: Turn the Location: into a terminal hyperlink 2022-03-11 17:25:35 -08:00
tool_cb_hdr.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_cb_prg.c tool_cb_prg: make resumed upload progress bar show better 2021-09-27 23:17:44 +02:00
tool_cb_prg.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_cb_rea.c docs/examples: adjust prototypes for CURLOPT_READFUNCTION 2020-12-30 22:20:24 +01:00
tool_cb_rea.h docs/examples: adjust prototypes for CURLOPT_READFUNCTION 2020-12-30 22:20:24 +01:00
tool_cb_see.c [PellesC] fix _lseeki64() macro 2021-07-16 07:32:06 +02:00
tool_cb_see.h curl.se: new home 2020-11-04 23:59:47 +01:00
tool_cb_wrt.c misc: spelling fixes 2022-03-30 10:49:06 +02:00
tool_cb_wrt.h curl.se: new home 2020-11-04 23:59:47 +01:00
tool_cfgable.c curl: add --no-clobber 2022-03-11 08:38:01 +01:00
tool_cfgable.h English: use American spelling consistently 2022-04-05 14:55:47 +02:00
tool_dirhie.c checksrc: detect more kinds of NULL comparisons we avoid 2021-12-27 23:39:26 +01:00
tool_dirhie.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_doswin.c lib: fix some misuse of curlx_convert_wchar_to_UTF8 2022-03-18 03:20:03 -04:00
tool_doswin.h curl.se: new home 2020-11-04 23:59:47 +01:00
tool_easysrc.c English: use American spelling consistently 2022-04-05 14:55:47 +02:00
tool_easysrc.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_filetime.c Revert "src/tool_filetime: disable -Wformat on mingw for this file" 2021-11-03 08:44:41 +01:00
tool_filetime.h curl: move fprintf outputs to warnf 2021-01-27 08:45:02 +01:00
tool_findfile.c tool_findfile: free mem properly 2022-01-07 16:39:19 +01:00
tool_findfile.h tool_findfile: check ~/.config/curlrc too 2022-01-07 10:10:49 +01:00
tool_formparse.c tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3) 2022-04-17 11:36:28 +02:00
tool_formparse.h curl.se: new home 2020-11-04 23:59:47 +01:00
tool_getparam.c curl: fix segmentation fault for empty output file names. 2022-03-28 09:39:09 +02:00
tool_getparam.h tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3) 2022-04-17 11:36:28 +02:00
tool_getpass.c curl.se: new home 2020-11-04 23:59:47 +01:00
tool_getpass.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_help.c curl: add --no-clobber 2022-03-11 08:38:01 +01:00
tool_help.h tool_listhelp: easier to generate with gen.pl 2021-09-30 17:50:48 +02:00
tool_helpers.c tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3) 2022-04-17 11:36:28 +02:00
tool_helpers.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_hugehelp.c.cvs curl.se: new home 2020-11-04 23:59:47 +01:00
tool_hugehelp.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_libinfo.c tool_operate: only set SSH related libcurl options for SSH URLs 2021-11-21 23:12:48 +01:00
tool_libinfo.h tool_operate: only set SSH related libcurl options for SSH URLs 2021-11-21 23:12:48 +01:00
tool_listhelp.c fail.d: tweak the description 2022-04-15 23:50:35 +02:00
tool_main.c tool and tests: force flush of all buffers at end of program 2022-03-13 13:29:28 +01:00
tool_main.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_msgs.c copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_msgs.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_operate.c tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3) 2022-04-17 11:36:28 +02:00
tool_operate.h curl: remove "separators" (when using globbed URLs) 2022-01-15 23:41:28 +01:00
tool_operhlp.c lib: remove support for CURL_DOES_CONVERSIONS 2022-02-04 08:05:35 +01:00
tool_operhlp.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_panykey.c curl.se: new home 2020-11-04 23:59:47 +01:00
tool_panykey.h curl.se: new home 2020-11-04 23:59:47 +01:00
tool_paramhlp.c tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3) 2022-04-17 11:36:28 +02:00
tool_paramhlp.h curl: add --create-file-mode [mode] 2020-12-21 10:52:41 +01:00
tool_parsecfg.c tool_findfile: check ~/.config/curlrc too 2022-01-07 10:10:49 +01:00
tool_parsecfg.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_progress.c tool_operate: Fix --fail-early with parallel transfers 2021-08-17 03:21:29 -04:00
tool_progress.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_sdecls.h curl: error out if -T and -d are used for the same URL 2022-04-17 11:18:17 +02:00
tool_setopt.c curl: error out when options need features not present in libcurl 2022-03-10 08:30:45 +01:00
tool_setopt.h curl: error out when options need features not present in libcurl 2022-03-10 08:30:45 +01:00
tool_setup.h TPF: drop support 2022-02-04 08:05:35 +01:00
tool_sleep.c curl.se: new home 2020-11-04 23:59:47 +01:00
tool_sleep.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_strdup.c curl.se: new home 2020-11-04 23:59:47 +01:00
tool_strdup.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_urlglob.c curl: fix segmentation fault for empty output file names. 2022-03-28 09:39:09 +02:00
tool_urlglob.h curl.se: new home 2020-11-04 23:59:47 +01:00
tool_util.c misc: update incorrect copyright year ranges 2021-08-16 11:21:47 +02:00
tool_util.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_version.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_vms.c cleanup: constify unmodified static structs 2021-09-23 12:54:35 +02:00
tool_vms.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00
tool_writeout.c curl/header_json: output the header names in lowercase 2022-03-25 11:24:27 +01:00
tool_writeout.h curl: add %{header_json} support in -w handling 2022-03-22 08:24:25 +01:00
tool_writeout_json.c curl/header_json: output the header names in lowercase 2022-03-25 11:24:27 +01:00
tool_writeout_json.h curl/header_json: output the header names in lowercase 2022-03-25 11:24:27 +01:00
tool_xattr.c checksrc: detect more kinds of NULL comparisons we avoid 2021-12-27 23:39:26 +01:00
tool_xattr.h copyright: fix year ranges 2020-11-05 08:22:10 +01:00