A command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP, WS and WSS. libcurl offers a myriad of powerful features
Go to file
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
.circleci CI: Do not use buildconf. Instead, just use: autoreconf -fi 2022-03-15 14:15:35 +01:00
.github GHA: move bearssl jobs over from zuul 2022-04-07 08:36:38 +02:00
.lift CI: Do not use buildconf. Instead, just use: autoreconf -fi 2022-03-15 14:15:35 +01:00
CMake msh3: add support for QUIC and HTTP/3 using msh3 2022-04-10 18:23:04 +02:00
docs gen.pl: change wording for mutexed options 2022-04-17 11:21:16 +02:00
include English: use American spelling consistently 2022-04-05 14:55:47 +02:00
lib lib: remove exclamation marks 2022-04-16 11:55:05 +02:00
m4 configure: fix '--enable-code-coverage' typo 2022-02-10 16:13:43 +01:00
packages English: use American spelling consistently 2022-04-05 14:55:47 +02:00
plan9 copyright: fix year ranges 2020-11-05 08:22:10 +01:00
projects projects/README: converted to markdown 2022-03-30 23:21:03 +02:00
scripts perl: removed a double semicolon at end of line 2022-04-14 13:55:32 +02:00
src tool_paramhlp: use feof(3) to identify EOF correctly when using fread(3) 2022-04-17 11:36:28 +02:00
tests curl: error out if -T and -d are used for the same URL 2022-04-17 11:18:17 +02:00
winbuild msh3: add support for QUIC and HTTP/3 using msh3 2022-04-10 18:23:04 +02:00
zuul.d GHA: move bearssl jobs over from zuul 2022-04-07 08:36:38 +02:00
.azure-pipelines.yml CI: Do not use buildconf. Instead, just use: autoreconf -fi 2022-03-15 14:15:35 +01:00
.cirrus.yml cirrus: add --enable-headers-api for some windows builds 2022-03-22 08:24:26 +01:00
.dcignore .dcignore: ignore tests and docs directories 2020-06-27 00:07:37 +02:00
.dir-locals.el curl.se: new home 2020-11-04 23:59:47 +01:00
.gitattributes gitattributes: Set batch files to CRLF line endings on checkout 2021-01-14 15:59:56 -05:00
.gitignore msh3: add support for QUIC and HTTP/3 using msh3 2022-04-10 18:23:04 +02:00
.lgtm.yml CI: Do not use buildconf. Instead, just use: autoreconf -fi 2022-03-15 14:15:35 +01:00
.mailmap strcase.h: add comment about the return code 2022-03-31 11:35:27 +02:00
acinclude.m4 misc: fix a few issues on MidnightBSD 2021-10-05 08:35:24 +02:00
appveyor.yml appveyor: add Cygwin build 2022-04-11 08:27:31 +02:00
buildconf buildconf: exec autoreconf to avoid additional process 2020-08-29 21:43:49 +02:00
buildconf.bat curl.se: new home 2020-11-04 23:59:47 +01:00
CHANGES curl.se: new home 2020-11-04 23:59:47 +01:00
CMakeLists.txt msh3: add support for QUIC and HTTP/3 using msh3 2022-04-10 18:23:04 +02:00
configure.ac msh3: add support for QUIC and HTTP/3 using msh3 2022-04-10 18:23:04 +02:00
COPYING COPYING: bump copyright year range 2022-01-05 23:14:15 +01:00
curl-config.in curl.se: new home 2020-11-04 23:59:47 +01:00
GIT-INFO GIT-INFO: rephrase to adapt to s/buildconf/autoreconf 2021-09-24 13:14:52 +02:00
libcurl.pc.in Revert "libcurl.pc: make it relocatable" 2020-11-09 09:13:06 +01:00
MacOSX-Framework MacOSX-Framework: remove redundant ';' 2021-10-15 08:08:14 +02:00
Makefile.am msh3: add support for QUIC and HTTP/3 using msh3 2022-04-10 18:23:04 +02:00
Makefile.dist netware: remove support 2022-01-31 13:40:26 +01:00
maketgz maketgz: return error if 'make dist' fails 2022-02-08 18:00:47 +01:00
mlc_config.json mlc_config.json: add file to ignore known troublesome URLs 2022-03-18 09:13:09 +01:00
README docs: reduce/avoid English contractions 2021-11-07 23:16:27 +01:00
README.md README: disable linkchecks for the sponsor links 2022-02-21 10:27:45 +01:00
RELEASE-NOTES RELEASE-NOTES: synced 2022-04-14 14:12:17 +02:00
SECURITY.md SECURITY.md: minor rephrase 2020-03-30 08:53:25 +02:00

curl logo

CII Best Practices Coverity passed AppVeyor Build Status Azure DevOps Build Status Cirrus Build Status Backers on Open Collective Sponsors on Open Collective Language Grade: C/C++ Fuzzing Status

Curl is a command-line tool for transferring data specified with URL syntax. Find out how to use curl by reading the curl.1 man page or the MANUAL document. Find out how to install Curl by reading the INSTALL document.

libcurl is the library curl is using to do its job. It is readily available to be used by your software. Read the libcurl.3 man page to learn how.

You can find answers to the most frequent questions we get in the FAQ document.

Study the COPYING file for distribution terms.

Contact

If you have problems, questions, ideas or suggestions, please contact us by posting to a suitable mailing list.

All contributors to the project are listed in the THANKS document.

Commercial support

For commercial support, maybe private and dedicated help with your problems or applications using (lib)curl visit the support page.

Website

Visit the curl website for the latest news and downloads.

Git

To download the latest source from the Git server do this:

git clone https://github.com/curl/curl.git

(you will get a directory named curl created, filled with the source code)

Security problems

Report suspected security problems via our HackerOne page and not in public!

Notice

Curl contains pieces of source code that is Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan. This notice is included here to comply with the distribution terms.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Open Collective Backers

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Open Collective Sponsor 0 Open Collective Sponsor 1 Open Collective Sponsor 2 Open Collective Sponsor 3 Open Collective Sponsor 4 Open Collective Sponsor 5 Open Collective Sponsor 6 Open Collective Sponsor 7 Open Collective Sponsor 8 Open Collective Sponsor 9