curl-curl/docs/examples
Viktor Szakats 6d87eb2878
cmake: add CURL_GCC_ANALYZER option, enable in CI, fix/silence
Enable in one existing Linux, macOS and Windows job.

Cost:
- Linux: +1.3 minutes.
- macOS: +1.5 minutes.
- Windows: +2.5 minutes.

Fix or silence issues found:
- conncache: silence NULL deref warning.
  ```
  lib/conncache.c:564:18: warning: dereference of NULL '*data.multi' [CWE-476] [-Wanalyzer-null-dereference]
  ```
  Ref: ede6a8e087 #19378
- http2: check pointer for NULL.
  ```
  lib/http2.c:388:7: error: dereference of NULL ‘data’ [CWE-476] [-Wanalyzer-null-dereference]
  ```
- http2: silence potential NULL deref in `cf_h2_recv`.
  ```
  lib/http2.c: In function 'cf_h2_recv':
  lib/curl_trc.h:62:15: warning: dereference of NULL 'data' [CWE-476] [-Wanalyzer-null-dereference]
  ```
- openldap: silence deref before NULL check.
  Seen in GHA/Linux.
  ```
  lib/openldap.c: In function ‘oldap_state_mechs_resp’:
  lib/curl_trc.h:140:7: warning: check of ‘data’ for NULL after already dereferencing it [-Wanalyzer-deref-before-check]
  ```
- sendf: silence NULL deref false positive in `Curl_creader_set_fread`.
  It looks impossible to happen.
  ```
  lib/sendf.c:1133:7: warning: dereference of NULL 'r' [CWE-476] [-Wanalyzer-null-dereference]
  ```
- ws: silence deref before NULL check.
  ```
  lib/ws.c: In function 'ws_send_raw_blocking':
  lib/curl_trc.h:205:7: warning: check of 'data' for NULL after already dereferencing it [-Wanalyzer-deref-before-check]
  ```
- var: fix potential NULL deref
  ```
  src/var.c:216:29: warning: dereference of NULL 'envp' [CWE-476] [-Wanalyzer-null-dereference]
  ```
- cli_hx_upload.c: fix NULL check after dereference.
  ```
  tests/libtest/cli_hx_upload.c:170:7: warning: check of '*t.method' for NULL after already dereferencing it [-Wanalyzer-deref-before-check]
  ```
- unit1607, unit1609: fix theoretical NULL ptr dereference.
  ```
  tests/unit/unit1607.c:211:12: warning: dereference of NULL 'addr' [CWE-476] [-Wanalyzer-null-dereference]
  tests/unit/unit1609.c:193:12: warning: dereference of NULL 'addr' [CWE-476] [-Wanalyzer-null-dereference]
  ```
- globally disable checks triggering false positives only:
  ```
  docs/examples/externalsocket.c:135:8: warning: 'connect' on possibly invalid file descriptor 'sockfd' [-Wanalyzer-fd-use-without-check]
  lib/bufq.c:465:16: warning: infinite loop [CWE-835] [-Wanalyzer-infinite-loop] (gcc-15 Windows)
  lib/doh.c:1035:34: warning: stack-based buffer over-read [CWE-126] [-Wanalyzer-out-of-bounds] (gcc-15 macOS)
  lib/ftp.c:4022:20: warning: infinite loop [CWE-835] [-Wanalyzer-infinite-loop] (gcc-15 macOS)
  lib/http2.c:689:28: warning: buffer over-read [CWE-126] [-Wanalyzer-out-of-bounds] (gcc-15 macOS)
  lib/socketpair.c:195:5: warning: leak of file descriptor 'curl_dbg_socket(2, 1, 0, 192, "D:/a/curl/curl/lib/socketpair.c")' [CWE-775] [-Wanalyzer-fd-leak]
  src/tool_doswin.c:810:7: warning: leak of file descriptor '*tdata.socket_l' [CWE-775] [-Wanalyzer-fd-leak]
  src/tool_doswin.c:816:9: warning: leak of file descriptor '*tdata.socket_l' [CWE-775] [-Wanalyzer-fd-leak]
  src/tool_main.c:96:1: warning: leak of file descriptor 'fd[0]' [CWE-775] [-Wanalyzer-fd-leak]
  src/tool_main.c:96:1: warning: leak of file descriptor 'fd[1]' [CWE-775] [-Wanalyzer-fd-leak]
  src/tool_urlglob.c:48:17: warning: leak of 'malloc(8)' [CWE-401] [-Wanalyzer-malloc-leak]
  src/tool_writeout.c:870:3: warning: leak of FILE 'stream2' [CWE-775] [-Wanalyzer-file-leak]
  tests/libtest/lib518.c:90:1: warning: leak of FILE [CWE-775] [-Wanalyzer-file-leak]
  tests/libtest/lib537.c:87:1: warning: leak of FILE [CWE-775] [-Wanalyzer-file-leak]
  tests/server/tftpd.c:1147:10: warning: 'bind' on possibly invalid file descriptor 'sock' [-Wanalyzer-fd-use-without-check]
  tests/server/tftpd.c:1155:10: warning: 'bind' on possibly invalid file descriptor 'sock' [-Wanalyzer-fd-use-without-check]
  tests/server/tftpd.c:1259:10: warning: 'connect' on possibly invalid file descriptor '4294967295' [-Wanalyzer-fd-use-without-check]
  ```

Also:
- cmake: update clang-tidy typecheck comment.

Ref: https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html

Closes #20921
2026-03-16 11:49:34 +01:00
..
.checksrc build: tidy up curl-specific fstat calls and stat struct type 2026-02-02 18:57:13 +01:00
.gitignore examples/threaded-ssl: delete in favor of examples/threaded 2025-12-17 00:32:20 +01:00
10-at-a-time.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
adddocsref.pl stop using the word 'just' 2026-03-03 15:30:22 +01:00
address-scope.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
altsvc.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
anyauthput.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
block_ip.c badwords: rework exceptions, fix many of them 2026-03-12 01:01:16 +01:00
cacertinmem.c examples: drop warning silencers no longer hit 2026-03-12 01:01:16 +01:00
certinfo.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
chkspeed.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
CMakeLists.txt cmake: add CURL_GCC_ANALYZER option, enable in CI, fix/silence 2026-03-16 11:49:34 +01:00
connect-to.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
cookie_interface.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
crawler.c tidy-up: miscellaneous 2026-02-12 14:52:16 +01:00
debug.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
default-scheme.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ephiperfifo.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
evhiperfifo.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
externalsocket.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
fileupload.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ftp-delete.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ftp-wildcard.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ftpget.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ftpgetinfo.c clang-tidy: drop redundant casts 2026-02-19 15:27:17 +01:00
ftpgetresp.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ftpsget.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ftpupload.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
ftpuploadfrommem.c docs: rename CURLcode variables to 'result' 2025-12-17 08:55:12 +01:00
ftpuploadresume.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
getinfo.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
getinmemory.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
getredirect.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
getreferrer.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ghiper.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
headerapi.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
hiperfifo.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
hsts-preload.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
htmltidy.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
htmltitle.cpp docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
http2-download.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
http2-pushinmemory.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
http2-serverpush.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
http2-upload.c badwords: rework exceptions, fix many of them 2026-03-12 01:01:16 +01:00
http3-present.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
http3.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
http-options.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
http-post.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
httpcustomheader.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
httpput-postfields.c badwords: rework exceptions, fix many of them 2026-03-12 01:01:16 +01:00
httpput.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
https.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-append.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
imap-authzid.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-copy.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-create.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
imap-delete.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
imap-examine.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
imap-fetch.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-list.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-lsub.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
imap-multi.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-noop.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
imap-search.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
imap-ssl.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-store.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
imap-tls.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
interface.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
ipv6.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
keepalive.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
localport.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
log_failed_transfers.c build: fix -Wunused-macros warnings, and related tidy-ups 2026-02-19 00:00:48 +01:00
Makefile.am cmake: support building some complicated examples, build them in CI 2025-10-07 19:09:02 +02:00
Makefile.example Makefile.example: make default options more likely to work 2025-10-20 13:52:58 +02:00
Makefile.inc examples/threaded-ssl: delete in favor of examples/threaded 2025-12-17 00:32:20 +01:00
maxconnects.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
multi-app.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
multi-debugcallback.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
multi-double.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
multi-event.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
multi-formadd.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
multi-legacy.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
multi-post.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
multi-single.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
multi-uv.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
netrc.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
parseurl.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
persistent.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-authzid.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-dele.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-list.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-multi.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-noop.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
pop3-retr.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-ssl.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-stat.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
pop3-tls.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
pop3-top.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
pop3-uidl.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
post-callback.c docs: rename CURLcode variables to 'result' 2025-12-17 08:55:12 +01:00
postinmemory.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
postit2-formadd.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
postit2.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
progressfunc.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
protofeats.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
range.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
README.md stop using the word 'just' 2026-03-03 15:30:22 +01:00
resolve.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
rtsp-options.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
sendrecv.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
sepheaders.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
sessioninfo.c tidy-up: miscellaneous 2026-02-12 14:52:16 +01:00
sftpget.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
sftpuploadresume.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
shared-connection-cache.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
simple.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
simplepost.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
simplessl.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
smooth-gtk-thread.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
smtp-authzid.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
smtp-expn.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
smtp-mail.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
smtp-mime.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
smtp-multi.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
smtp-ssl.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
smtp-tls.c stop using the word 'just' 2026-03-03 15:30:22 +01:00
smtp-vrfy.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
sslbackend.c build: use more const 2026-02-02 12:32:49 +01:00
synctime.c badwords: avoid 'simply' 2026-03-10 19:34:06 +01:00
threaded.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
unixsocket.c tidy-up: miscellaneous 2026-02-12 14:52:16 +01:00
url2file.c tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
urlapi.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
usercertinmem.c examples: drop warning silencers no longer hit 2026-03-12 01:01:16 +01:00
version-check.pl tidy-up: miscellaneous 2026-02-25 14:44:56 +01:00
websocket-cb.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00
websocket-updown.c tidy-up: miscellaneous 2026-02-12 14:52:16 +01:00
websocket.c badwords: rework exceptions, fix many of them 2026-03-12 01:01:16 +01:00
xmlstream.c docs: compare result against literal CURLE_OK (where missing) 2026-02-07 19:40:45 +01:00

libcurl examples

This directory is for libcurl programming examples. They are meant to show some simple steps on how you can build your own application to take full advantage of libcurl.

If you end up with other small but still useful example sources, please mail them for submission in future packages and on the website.

Building

The Makefile.example is an example Makefile that could be used to build these examples. Edit the file according to your system and requirements first.

Most examples should build fine using a command line like this:

`curl-config --cc --cflags --libs` -o example-my example.c

Some compilers do not like having the arguments in this order but instead want you do reorganize them like:

`curl-config --cc` -o example-my example.c `curl-config --cflags --libs`

Please do not use the curl.se site as a test target for your libcurl applications/experiments. Even if some of the examples use that site as a URL at some places, it does not mean that the URLs work or that we expect you to actually torture our website with your tests. Thanks.

Examples

Each example source code file is designed to be and work stand-alone and rather self-explanatory. The examples may at times lack the level of error checks you need in a real world, but that is then only for the sake of readability: to make the code smaller and easier to follow.