]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
examples: fix build issues in 'complicated' examples
authorViktor Szakats <commit@vsz.me>
Tue, 7 Oct 2025 11:54:17 +0000 (13:54 +0200)
committerViktor Szakats <commit@vsz.me>
Tue, 7 Oct 2025 15:15:10 +0000 (17:15 +0200)
- cacertinmem: build cleanly with BoringSSL/AWS-LC.
- cacertinmem: silence `-Wcast-function-type-strict`.
- multi-uv: fix callback prototypes.
- multithread, threaded-ssl: do not pass const as thread arg.
- sessioninfo: fix suppressing deprecated feature warning.
- usercertinmem: sync formatting with cacertinmem.

Follow-up to 4a6bdd5899005c25ce222dc21dcfd1a779544330 #18908
Cherry-picked from #18909
Closes #18914

docs/examples/cacertinmem.c
docs/examples/multi-uv.c
docs/examples/multithread.c
docs/examples/sessioninfo.c
docs/examples/threaded-ssl.c
docs/examples/usercertinmem.c

index ec12df58bf18074cc4a4a5ae446b6067a8e5b444..6a2649b29b79866eea4901f35e7b6f4891599eb7 100644 (file)
 #if defined(__GNUC__) || defined(__clang__)
 #pragma GCC diagnostic ignored "-Woverlength-strings"
 #endif
+/* Silence warning when calling sk_X509_INFO_pop_free() */
+#if defined(__clang__) && __clang_major__ >= 16
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wcast-function-type-strict"
+#endif
+
+#if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
+typedef size_t ossl_valsize_t;
+#else
+typedef int ossl_valsize_t;
+#endif
 
 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
 {
@@ -68,8 +79,8 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
     "-----END CERTIFICATE-----\n";
 
   BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
-  X509_STORE  *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
-  int i;
+  X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
+  ossl_valsize_t i;
   STACK_OF(X509_INFO) *inf;
 
   (void)curl;
index ee0ac0e1b33fd9ed205e5e49f7bb036630339356..07774c93eb9034faa5d03fc5a9e733e210c36a06 100644 (file)
@@ -167,9 +167,9 @@ static void on_uv_timeout(uv_timer_t *req)
 }
 
 /* callback from libcurl to update the timeout expiry */
-static int cb_timeout(CURLM *multi, long timeout_ms,
-                      struct datauv *uv)
+static int cb_timeout(CURLM *multi, long timeout_ms, void *userp)
 {
+  struct datauv *uv = (struct datauv *)userp;
   (void)multi;
   if(timeout_ms < 0)
     uv_timer_stop(&uv->timeout);
@@ -185,9 +185,9 @@ static int cb_timeout(CURLM *multi, long timeout_ms,
 
 /* callback from libcurl to update socket activity to wait for */
 static int cb_socket(CURL *easy, curl_socket_t s, int action,
-                     struct datauv *uv,
-                     void *socketp)
+                     void *userp, void *socketp)
 {
+  struct datauv *uv = (struct datauv *)userp;
   struct curl_context *curl_context;
   int events = 0;
   (void)easy;
index ceee94022a394d6bd9cb83f5df369f33130c4218..4cdb74fbe44408ae953f3375c594972fb0a26112 100644 (file)
@@ -48,12 +48,13 @@ static const char * const urls[NUMT]= {
   "www.example"
 };
 
-static void *pull_one_url(void *url)
+static void *pull_one_url(void *pindex)
 {
+  int i = *(int *)pindex;
   CURL *curl;
 
   curl = curl_easy_init();
-  curl_easy_setopt(curl, CURLOPT_URL, url);
+  curl_easy_setopt(curl, CURLOPT_URL, urls[i]);
   curl_easy_perform(curl); /* ignores error */
   curl_easy_cleanup(curl);
 
@@ -79,7 +80,7 @@ int main(void)
     int error = pthread_create(&tid[i],
                                NULL, /* default attributes please */
                                pull_one_url,
-                               (void *)urls[i]);
+                               (void *)&i);
     if(error)
       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
     else
index 225b1ae5a516e73a5d768d0f727f83c94a151735..5fbc1cc4df7a9a72fc8b78cb327c6ad41d305b63 100644 (file)
 /* Note that this example currently requires curl to be linked against
    GnuTLS (and this program must also be linked against -lgnutls). */
 
+#ifndef CURL_DISABLE_DEPRECATION
+#define CURL_DISABLE_DEPRECATION
+#endif
+
 #include <stdio.h>
 
 #include <curl/curl.h>
@@ -47,8 +51,7 @@ static size_t wrfu(void *ptr, size_t size, size_t nmemb, void *stream)
   (void)stream;
   (void)ptr;
 
-  res = CURL_IGNORE_DEPRECATION(
-    curl_easy_getinfo(curl, CURLINFO_TLS_SESSION, &info));
+  res = curl_easy_getinfo(curl, CURLINFO_TLS_SESSION, &info);
 
   if(!res) {
     switch(info->backend) {
index 161182eec1e19bb0aa706b976c96ff13f1ef193d..6590e8b202b9b4173b0826e6d34be9c5e21b5baf 100644 (file)
@@ -52,12 +52,13 @@ static const char * const urls[]= {
   "https://www4.example.com/",
 };
 
-static void *pull_one_url(void *url)
+static void *pull_one_url(void *pindex)
 {
+  int i = *(int *)pindex;
   CURL *curl;
 
   curl = curl_easy_init();
-  curl_easy_setopt(curl, CURLOPT_URL, url);
+  curl_easy_setopt(curl, CURLOPT_URL, urls[i]);
   /* this example does not verify the server's certificate, which means we
      might be downloading stuff from an impostor */
   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
@@ -82,7 +83,7 @@ int main(int argc, char **argv)
     int error = pthread_create(&tid[i],
                                NULL, /* default attributes please */
                                pull_one_url,
-                               (void *)urls[i]);
+                               (void *)&i);
     if(error)
       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
     else
index 50537ae25fdc86d7d9eb07fdf91017df924ca913..899895592e040c8aeaf079b01ecababf6c24ac96 100644 (file)
@@ -22,7 +22,7 @@
  *
  ***************************************************************************/
 /* <DESC>
- * Use an in-memory user certificate and RSA key and retrieve an https page.
+ * Use an in-memory user certificate and RSA key and retrieve an HTTPS page.
  * </DESC>
  */
 /* Written by Ishan SinghLevett, based on Theo Borm's cacertinmem.c.
@@ -47,7 +47,7 @@
 
 static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
 {
-  fwrite(ptr, size, nmemb, stream);
+  fwrite(ptr, size, nmemb, (FILE *)stream);
   return nmemb * size;
 }
 
@@ -179,12 +179,10 @@ int main(void)
 
   /* first try: retrieve page without user certificate and key -> fails */
   rv = curl_easy_perform(ch);
-  if(rv == CURLE_OK) {
+  if(rv == CURLE_OK)
     printf("*** transfer succeeded ***\n");
-  }
-  else {
+  else
     printf("*** transfer failed ***\n");
-  }
 
   /* second try: retrieve page using user certificate and key -> succeeds
    * load the certificate and key by installing a function doing the necessary
@@ -192,12 +190,10 @@ int main(void)
    */
   curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
   rv = curl_easy_perform(ch);
-  if(rv == CURLE_OK) {
+  if(rv == CURLE_OK)
     printf("*** transfer succeeded ***\n");
-  }
-  else {
+  else
     printf("*** transfer failed ***\n");
-  }
 
   curl_easy_cleanup(ch);
   curl_global_cleanup();