]> git.feebdaed.xyz Git - 0xmirror/curl.git/commitdiff
tests: replace `strcpy()` with `curlx_strcopy()`
authorViktor Szakats <commit@vsz.me>
Tue, 23 Dec 2025 10:59:59 +0000 (11:59 +0100)
committerViktor Szakats <commit@vsz.me>
Tue, 23 Dec 2025 21:25:39 +0000 (22:25 +0100)
Also:
- examples/hsts-preload: apply the same change as it's based on lib1915
  in tests. Make a local clone of `curlx_strcopy()`. Then drop the
  `_CRT_SECURE_NO_WARNINGS` hack, that's no longer necessary.
- curl_setup.h: delete `strcpy()` from the `_CRT_SECURE_NO_WARNINGS`
  list.

Closes #20076

docs/examples/hsts-preload.c
lib/curl_setup.h
tests/libtest/lib1901.c
tests/libtest/lib1915.c
tests/libtest/lib544.c
tests/server/getpart.c
tests/server/socksd.c
tests/server/util.c
tests/unit/unit3205.c

index 9ae1ff5b0fa7b4a615472b2bc91940cfe56e0ffb..8e73b79cdaf33d980ae833e11b2204a251e16b1b 100644 (file)
  * Preload domains to HSTS
  * </DESC>
  */
-#ifdef _MSC_VER
-#ifndef _CRT_SECURE_NO_WARNINGS
-#define _CRT_SECURE_NO_WARNINGS  /* for strcpy() */
-#endif
-#endif
-
 #include <stdio.h>
 #include <string.h>
 
@@ -51,6 +45,16 @@ struct state {
   int index;
 };
 
+static void strcopy(char *dest, size_t dsize, const char *src, size_t slen)
+{
+  if(slen < dsize) {
+    memcpy(dest, src, slen);
+    dest[slen] = 0;
+  }
+  else if(dsize)
+    dest[0] = 0;
+}
+
 /* "read" is from the point of the library, it wants data from us. One domain
    entry per invoke. */
 static CURLSTScode hstsread(CURL *curl, struct curl_hstsentry *e, void *userp)
@@ -62,10 +66,10 @@ static CURLSTScode hstsread(CURL *curl, struct curl_hstsentry *e, void *userp)
   host = preload_hosts[s->index].name;
   expire = preload_hosts[s->index++].exp;
 
-  if(host && (strlen(host) < e->namelen)) {
-    strcpy(e->name, host);
+  if(host) {
+    strcopy(e->name, e->namelen, host, strlen(host));
     e->includeSubDomains = 0;
-    strcpy(e->expire, expire);
+    strcopy(e->expire, sizeof(e->expire), expire, strlen(expire));
     fprintf(stderr, "HSTS preload '%s' until '%s'\n", host, expire);
   }
   else
index e3903f475acf829c9256187e2a07a75f9bfa1436..9027e30dcf83618d8dd3fa8c0b8d51ea574ffb46 100644 (file)
@@ -94,7 +94,7 @@
 #define _CRT_NONSTDC_NO_DEPRECATE  /* for close(), fileno(), unlink(), etc. */
 #endif
 #ifndef _CRT_SECURE_NO_WARNINGS
-#define _CRT_SECURE_NO_WARNINGS  /* for getenv(), strcpy(), tests: sscanf() */
+#define _CRT_SECURE_NO_WARNINGS  /* for getenv(), tests: sscanf() */
 #endif
 #endif /* _MSC_VER */
 
index 638e012b5ddcdaffcfe80e66f4012ac8b300f929..ba88719b0ae28aaed238c7dbac2fa174919d3eda 100644 (file)
@@ -33,12 +33,11 @@ static size_t t1901_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
     NULL
   };
   static int ix = 0;
-  (void)size;
   (void)nmemb;
   (void)stream;
   if(chunks[ix]) {
     size_t len = strlen(chunks[ix]);
-    strcpy(ptr, chunks[ix]);
+    curlx_strcopy(ptr, size * nmemb, chunks[ix], len);
     ix++;
     return len;
   }
index 6a5be3874a47e3af581e795c21350e67acf465cb..393d6edcb7366d6a9547c3eb1db3879cc4d41af2 100644 (file)
@@ -58,10 +58,10 @@ static CURLSTScode hstsread(CURL *curl, struct curl_hstsentry *e, void *userp)
   host = preload_hosts[s->index].name;
   expire = preload_hosts[s->index++].exp;
 
-  if(host && (strlen(host) < e->namelen)) {
-    strcpy(e->name, host);
+  if(host) {
+    curlx_strcopy(e->name, e->namelen, host, strlen(host));
     e->includeSubDomains = FALSE;
-    strcpy(e->expire, expire);
+    curlx_strcopy(e->expire, sizeof(e->expire), expire, strlen(expire));
     curl_mfprintf(stderr, "add '%s'\n", host);
   }
   else
index 4740911666cf45f7c364980dfd9f8595c6923a8a..6ceac095d72f8bb82e16ea6be2a5173339b4a931 100644 (file)
@@ -63,7 +63,7 @@ static CURLcode test_lib544(const char *URL)
   test_setopt(curl, CURLOPT_HEADER, 1L); /* include header */
 
   /* Update the original data to detect non-copy. */
-  strcpy(teststring, "FAIL");
+  curlx_strcopy(teststring, sizeof(teststring), "FAIL", strlen("FAIL"));
 
   {
     CURL *curl2;
index d76368e016a4b788ec702c95ef1bf0c69df591f5..94e4b202e0bcfdefb73b052d9959ae9e9c23723d 100644 (file)
@@ -391,19 +391,19 @@ int getpart(char **outbuf, size_t *outlen,
 
       if(STATE_OUTSIDE == state) {
         /* outermost element (<testcase>) */
-        strcpy(curouter, ptag);
+        curlx_strcopy(curouter, sizeof(curouter), ptag, strlen(ptag));
         state = STATE_OUTER;
         continue;
       }
       else if(STATE_OUTER == state) {
         /* start of a main section */
-        strcpy(curmain, ptag);
+        curlx_strcopy(curmain, sizeof(curmain), ptag, strlen(ptag));
         state = STATE_INMAIN;
         continue;
       }
       else if(STATE_INMAIN == state) {
         /* start of a sub section */
-        strcpy(cursub, ptag);
+        curlx_strcopy(cursub, sizeof(cursub), ptag, strlen(ptag));
         state = STATE_INSUB;
         if(!strcmp(curmain, main) && !strcmp(cursub, sub)) {
           /* start of wanted part */
index fdde648d3d288d732e5671a05abc3e23fcb0a767..556fb47c62e30dee4ea014366ff7fdc8816dd0b0 100644 (file)
@@ -101,9 +101,12 @@ static void socksd_resetdefaults(void)
   s_config.reqcmd = CONFIG_REQCMD;
   s_config.connectrep = CONFIG_CONNECTREP;
   s_config.port = CONFIG_PORT;
-  strcpy(s_config.addr, CONFIG_ADDR);
-  strcpy(s_config.user, "user");
-  strcpy(s_config.password, "password");
+  curlx_strcopy(s_config.addr, sizeof(s_config.addr),
+                CONFIG_ADDR, strlen(CONFIG_ADDR));
+  curlx_strcopy(s_config.user, sizeof(s_config.user),
+                "user", strlen("user"));
+  curlx_strcopy(s_config.password, sizeof(s_config.password),
+                "password", strlen("password"));
 }
 
 static void socksd_getconfig(void)
@@ -141,7 +144,8 @@ static void socksd_getconfig(void)
           }
         }
         else if(!strcmp(key, "backend")) {
-          strcpy(s_config.addr, value);
+          curlx_strcopy(s_config.addr, sizeof(s_config.addr),
+                        value, strlen(value));
           logmsg("backend [%s] set", s_config.addr);
         }
         else if(!strcmp(key, "backendport")) {
@@ -152,11 +156,13 @@ static void socksd_getconfig(void)
           }
         }
         else if(!strcmp(key, "user")) {
-          strcpy(s_config.user, value);
+          curlx_strcopy(s_config.user, sizeof(s_config.user),
+                        value, strlen(value));
           logmsg("user [%s] set", s_config.user);
         }
         else if(!strcmp(key, "password")) {
-          strcpy(s_config.password, value);
+          curlx_strcopy(s_config.password, sizeof(s_config.password),
+                        value, strlen(value));
           logmsg("password [%s] set", s_config.password);
         }
         /* Methods:
index 529ead7b86bea3cf2878039a93657a518fec34ae..276c54dba50b71ff2f386b9d88aec4bea2c35f2c 100644 (file)
@@ -694,7 +694,7 @@ int bind_unix_socket(curl_socket_t sock, const char *unix_socket,
     logmsg("Too long unix socket domain path (%zd)", len);
     return -1;
   }
-  strcpy(sau->sun_path, unix_socket);
+  curlx_strcopy(sau->sun_path, sizeof(sau->sun_path), unix_socket, len);
   rc = bind(sock, (struct sockaddr *)sau, sizeof(struct sockaddr_un));
   if(rc && SOCKERRNO == SOCKEADDRINUSE) {
     struct_stat statbuf;
index df0ea7f8ddf891a9bfcf9e1f8301f7a124b07bae..f4d9ab82dae1da84e4164190182da00b21c24742 100644 (file)
@@ -560,10 +560,14 @@ static CURLcode test_unit3205(const char *arg)
 
     /* suites matched by EDH alias will return the DHE name */
     if(test->id >= 0x0011 && test->id < 0x0017) {
-      if(expect && memcmp(expect, "EDH-", 4) == 0)
-        expect = (char *)memcpy(strcpy(alt, expect), "DHE-", 4);
-      if(expect && memcmp(expect + 4, "EDH-", 4) == 0)
-        expect = (char *)memcpy(strcpy(alt, expect) + 4, "DHE-", 4) - 4;
+      if(expect && memcmp(expect, "EDH-", 4) == 0) {
+        curlx_strcopy(alt, sizeof(alt), expect, strlen(expect));
+        expect = (char *)memcpy(alt, "DHE-", 4);
+      }
+      if(expect && memcmp(expect + 4, "EDH-", 4) == 0) {
+        curlx_strcopy(alt, sizeof(alt), expect, strlen(expect));
+        expect = (char *)memcpy(alt + 4, "DHE-", 4) - 4;
+      }
     }
 
     if(expect && strcmp(buf, expect) != 0) {