From: "cron2 (Code Review)" Date: Wed, 4 Mar 2026 14:24:50 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="kevqqwLLPZ0="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: buffer: Add checked_snprintf function and use it in the code List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: plaisthos , openvpn-devel --kevqqwLLPZ0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/14= 89?usp=3Demail ) Change subject: buffer: Add checked_snprintf function and use it in the code ...................................................................... buffer: Add checked_snprintf function and use it in the code This reintroduces a function that converts the result of snprintf to a boolean since the check is always the same but annoyingly verbose. And it gets worse when you add -Wsign-compare. So in preparation of introducing -Wsign-compare wrap this check in the function. This somewhat reverts the removal of openvpn_snprintf. But note that that was originally introduced to work around the broken snprintf of Windows. So this is not exactly the same. For this reason I also classified this as a buffer function and not a compat function. Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489 Message-Id: <20260304110455.15859-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35= 872.html Signed-off-by: Gert Doering --- M src/openvpn/buffer.c M src/openvpn/buffer.h M src/openvpn/crypto_mbedtls.c M src/openvpn/crypto_mbedtls_legacy.c M src/openvpn/dns.c M src/openvpn/env_set.c M src/openvpn/platform.c M src/openvpn/proxy.c M src/openvpn/socks.c M src/openvpn/ssl_ncp.c M src/openvpn/ssl_verify.c M src/openvpn/ssl_verify_mbedtls.c M src/openvpn/tun.c M src/openvpn/win32.c M tests/unit_tests/openvpn/test_buffer.c 15 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 280cbc8..745b1c8 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -1134,6 +1134,17 @@ } } +bool +checked_snprintf(char *str, size_t size, const char *format, ...) +{ + va_list arglist; + va_start(arglist, format); + ASSERT(size < INT_MAX); + int len =3D vsnprintf(str, size, format, arglist); + va_end(arglist); + return (len >=3D 0 && len < (ssize_t)size); +} + #ifdef VERIFY_ALIGNMENT void valign4(const struct buffer *buf, const char *file, const int line) diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 7bb491d..040f752 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -972,6 +972,29 @@ return 0 =3D=3D strncmp(str, prefix, strlen(prefix)); } +/** + * Like snprintf() but returns an boolean. + * + * To check the return value of snprintf() one needs to + * do multiple comparisons of the \p size parameter + * against the return value. Doesn't get prettier by + * them being different types with different signedness + * and size. + * + * So this function allows to wrap all of that into one + * boolean return value. + * + * @return true if snprintf() was successful and not truncated. + */ +bool checked_snprintf(char *str, size_t size, const char *format, ...) +#ifdef __GNUC__ +#if __USE_MINGW_ANSI_STDIO + __attribute__((format(gnu_printf, 3, 4))) +#else + __attribute__((format(__printf__, 3, 4))) +#endif +#endif + ; /* * Verify that a pointer is correctly aligned diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c index e8931d7..7aaea3d 100644 --- a/src/openvpn/crypto_mbedtls.c +++ b/src/openvpn/crypto_mbedtls.c @@ -1014,7 +1014,7 @@ { char prefix[256]; - if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >=3D sizeof(= prefix)) + if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d", func, line)) { return mbed_log_err(flags, errval, func); } @@ -1104,11 +1104,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= =3D sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----\n", = name)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >=3D = sizeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n", na= me)) { return false; } @@ -1142,11 +1142,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >=3D = sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----", na= me)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >=3D si= zeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----", name= )) { return false; } diff --git a/src/openvpn/crypto_mbedtls_legacy.c b/src/openvpn/crypto_mbedt= ls_legacy.c index 00fe542..237564c 100644 --- a/src/openvpn/crypto_mbedtls_legacy.c +++ b/src/openvpn/crypto_mbedtls_legacy.c @@ -130,7 +130,7 @@ { char prefix[256]; - if (snprintf(prefix, sizeof(prefix), "%s:%d", func, line) >=3D sizeof(= prefix)) + if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d", func, line)) { return mbed_log_err(flags, errval, func); } @@ -246,11 +246,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", name) >= =3D sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----\n", = name)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >=3D = sizeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n", na= me)) { return false; } @@ -283,11 +283,11 @@ char header[1000 + 1] =3D { 0 }; char footer[1000 + 1] =3D { 0 }; - if (snprintf(header, sizeof(header), "-----BEGIN %s-----", name) >=3D = sizeof(header)) + if (!checked_snprintf(header, sizeof(header), "-----BEGIN %s-----", na= me)) { return false; } - if (snprintf(footer, sizeof(footer), "-----END %s-----", name) >=3D si= zeof(footer)) + if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----", name= )) { return false; } diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index f4f7779..3a294ec 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -485,13 +485,11 @@ if (j < 0) { - const int ret =3D snprintf(name, sizeof(name), format, i); - name_ok =3D (ret > 0 && ret < sizeof(name)); + name_ok =3D checked_snprintf(name, sizeof(name), format, i); } else { - const int ret =3D snprintf(name, sizeof(name), format, i, j); - name_ok =3D (ret > 0 && ret < sizeof(name)); + name_ok =3D checked_snprintf(name, sizeof(name), format, i, j); } if (!name_ok) diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.c index 99ac45c..d992097 100644 --- a/src/openvpn/env_set.c +++ b/src/openvpn/env_set.c @@ -334,7 +334,7 @@ strcpy(tmpname, name); while (NULL !=3D env_set_get(es, tmpname) && counter < 1000) { - ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, counter) < tm= pname_len); + ASSERT(checked_snprintf(tmpname, tmpname_len, "%s_%u", name, count= er)); counter++; } if (counter < 1000) diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index 4f0eddf..3a6b272 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -550,9 +550,8 @@ { ++attempts; - const int ret =3D snprintf(fname, sizeof(fname), fname_fmt, max_pr= efix_len, prefix, - (unsigned long)get_random(), (unsigned lo= ng)get_random()); - if (ret < 0 || ret >=3D sizeof(fname)) + if (!checked_snprintf(fname, sizeof(fname), fname_fmt, max_prefix_= len, prefix, + (unsigned long)get_random(), (unsigned long)= get_random())) { msg(M_WARN, "ERROR: temporary filename too long"); return NULL; diff --git a/src/openvpn/proxy.c b/src/openvpn/proxy.c index b355827..9f3ec93 100644 --- a/src/openvpn/proxy.c +++ b/src/openvpn/proxy.c @@ -771,11 +771,10 @@ } /* send digest response */ - int sret =3D snprintf( - buf, sizeof(buf), - "Proxy-Authorization: Digest username=3D\"%s\", realm= =3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\", qop=3D%s, nc=3D%s, cnonce=3D\"%s\"= , response=3D\"%s\"%s", - username, realm, nonce, uri, qop, nonce_count, cnonce,= response, opaque_kv); - if (sret >=3D sizeof(buf)) + if (!checked_snprintf( + buf, sizeof(buf), + "Proxy-Authorization: Digest username=3D\"%s\", re= alm=3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\", qop=3D%s, nc=3D%s, cnonce=3D\"%= s\", response=3D\"%s\"%s", + username, realm, nonce, uri, qop, nonce_count, cno= nce, response, opaque_kv)) { goto error; } diff --git a/src/openvpn/socks.c b/src/openvpn/socks.c index 5cb5912..19f3d54 100644 --- a/src/openvpn/socks.c +++ b/src/openvpn/socks.c @@ -119,10 +119,9 @@ goto cleanup; } - int sret =3D snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s", (int)s= trlen(creds.username), - creds.username, (int)strlen(creds.password), creds= .password); - ASSERT(sret >=3D 0 && sret <=3D sizeof(to_send)); - + ASSERT(checked_snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s", + (int)strlen(creds.username), creds.username, + (int)strlen(creds.password), creds.password)); if (!proxy_send(sd, to_send, strlen(to_send))) { goto cleanup; diff --git a/src/openvpn/ssl_ncp.c b/src/openvpn/ssl_ncp.c index 20fd2f0..686f823 100644 --- a/src/openvpn/ssl_ncp.c +++ b/src/openvpn/ssl_ncp.c @@ -198,7 +198,7 @@ size_t newlen =3D strlen(o->ncp_ciphers) + 1 + strlen(ciphername) + 1; char *ncp_ciphers =3D gc_malloc(newlen, false, &o->gc); - ASSERT(snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, cipherna= me) < newlen); + ASSERT(checked_snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers, = ciphername)); o->ncp_ciphers =3D ncp_ciphers; } diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index 8bd8e96..d44f25f 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -548,7 +548,7 @@ goto cleanup; } - if (snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial= ) >=3D sizeof(fn)) + if (!checked_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATO= R, serial)) { msg(D_HANDSHAKE, "VERIFY CRL: filename overflow"); goto cleanup; diff --git a/src/openvpn/ssl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbed= tls.c index adeaa13..ad5479c 100644 --- a/src/openvpn/ssl_verify_mbedtls.c +++ b/src/openvpn/ssl_verify_mbedtls.c @@ -93,9 +93,7 @@ ret =3D mbedtls_x509_crt_verify_info(errstr, sizeof(errstr) - 1, "= ", *flags); if (ret <=3D 0 - && snprintf(errstr, sizeof(errstr), "Could not retrieve error = string, flags=3D%" PRIx32, - *flags) - >=3D sizeof(errstr)) + && !checked_snprintf(errstr, sizeof(errstr), "Could not retrie= ve error string, flags=3D%" PRIx32, *flags)) { errstr[0] =3D '\0'; } diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 3f9ee5d..ecebff7 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -3558,9 +3558,7 @@ msg(M_FATAL, "Error enumerating registry subkeys of key: %s", = ADAPTER_KEY); } - int ret =3D snprintf(unit_string, sizeof(unit_string), "%s\\%s", A= DAPTER_KEY, enum_name); - - if (ret < 0 || ret >=3D sizeof(unit_string)) + if (!checked_snprintf(unit_string, sizeof(unit_string), "%s\\%s", = ADAPTER_KEY, enum_name)) { msg(M_WARN, "Error constructing unit string for %s", enum_name= ); continue; @@ -3673,10 +3671,9 @@ msg(M_FATAL, "Error enumerating registry subkeys of key: %s", = NETWORK_CONNECTIONS_KEY); } - int ret =3D snprintf(connection_string, sizeof(connection_string),= "%s\\%s\\Connection", - NETWORK_CONNECTIONS_KEY, enum_name); - - if (ret < 0 || ret >=3D sizeof(connection_string)) + if (!checked_snprintf(connection_string, sizeof(connection_string), + "%s\\%s\\Connection", + NETWORK_CONNECTIONS_KEY, enum_name)) { msg(M_WARN, "Error constructing connection string for %s", enu= m_name); continue; diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index b938d7b..ac449fd 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -881,9 +881,8 @@ char force_path[256]; char *sysroot =3D get_win_sys_path(); - if (snprintf(force_path, sizeof(force_path), "PATH=3D%s\\System32;%s;%= s\\System32\\Wbem", - sysroot, sysroot, sysroot) - >=3D sizeof(force_path)) + if (!checked_snprintf(force_path, sizeof(force_path), "PATH=3D%s\\Syst= em32;%s;%s\\System32\\Wbem", + sysroot, sysroot, sysroot)) { msg(M_WARN, "env_block: default path truncated to %s", force_path); } diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/open= vpn/test_buffer.c index 1c78cf0..5f43e0d 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -424,6 +424,16 @@ #endif } +static void +test_checked_snprintf(void **state) +{ + char buf[10]; + assert_true(checked_snprintf(buf, sizeof(buf), "%s", "Hello")); + assert_true(checked_snprintf(buf, sizeof(buf), "%s", "Hello Foo")); + assert_false(checked_snprintf(buf, sizeof(buf), "%s", "Hello Foo!")); + assert_false(checked_snprintf(buf, sizeof(buf), "%s", "Hello World!")); +} + void test_buffer_chomp(void **state) { @@ -528,6 +538,7 @@ cmocka_unit_test(test_character_class), cmocka_unit_test(test_character_string_mod_buf), cmocka_unit_test(test_snprintf), + cmocka_unit_test(test_checked_snprintf), cmocka_unit_test(test_buffer_chomp), cmocka_unit_test(test_buffer_parse) }; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1489?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5 Gerrit-Change-Number: 1489 Gerrit-PatchSet: 7 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --kevqqwLLPZ0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

buffer: Add checked_snprintf function and use it=
 in the code

This reintroduces a function that converts the resultof snprintf to a boolean since the check is always the
same but annoyi= ngly verbose. And it gets worse when you add
-Wsign-compare.

So i= n preparation of introducing -Wsign-compare wrap this
check in the funct= ion.

This somewhat reverts the removal of openvpn_snprintf.
But n= ote that that was originally introduced to work
around the broken snprin= tf of Windows. So this is not
exactly the same. For this reason I also c= lassified this
as a buffer function and not a compat function.

Ch= ange-Id: Ia3477b8ee7a637c15aad7f285144280595cda5d5
Signed-off-by: Frank = Lichtenheld <frank@...2641...>
Acked-by: Gert Doering <gert@...493...= 1296...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1489
M= essage-Id: <20260304110455.15859-1-gert@...1296...>
URL: https://w= ww.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35872.html
Si= gned-off-by: Gert Doering <gert@...1296...>
---
M src/openvpn/b= uffer.c
M src/openvpn/buffer.h
M src/openvpn/crypto_mbedtls.c
M sr= c/openvpn/crypto_mbedtls_legacy.c
M src/openvpn/dns.c
M src/openvpn/e= nv_set.c
M src/openvpn/platform.c
M src/openvpn/proxy.c
M src/open= vpn/socks.c
M src/openvpn/ssl_ncp.c
M src/openvpn/ssl_verify.c
M s= rc/openvpn/ssl_verify_mbedtls.c
M src/openvpn/tun.c
M src/openvpn/win= 32.c
M tests/unit_tests/openvpn/test_buffer.c
15 files changed, 76 in= sertions(+), 42 deletions(-)

diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c
index 280cbc8..745b1c8 100644
--- a/src/openvpn/buffer.c
+++ b/src/openvpn/buffer.c
@@ -1134,6 +1134,= 17 @@
}
}
+bool
+checked_snprintf(char *str, size_t size, con= st char *format, ...)
+{
+ va_list argl= ist;
+ va_start(ar= glist, format);
+ = ASSERT(size < INT_MAX);
+ int len =3D vsnprintf(str, size, format, arglist);
+ va_end(arglist);

+ return (len >=3D 0 &&= amp; len < (ssize_t)size);
+}
+<= br> #ifdef VERIFY_ALIGNMENT
void
va= lign4(const struct buffer *buf, const char *file, const int line)diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h
<= span>index 7bb491d..040f752 100644
--- a/src/openvpn/buffer= .h
+++ b/src/openvpn/buffer.h
@@ -972,6 +97= 2,29 @@
return 0 =3D=3D strncmp(str, prefix, strlen(pr= efix));
}

+/**
+ * Like snprintf() but returns an boolean.
+ *
+ * To check the return value of snprintf() one needs to

+ * do multiple compari= sons of the \p size parameter
+ * against the return value. Doesn't get prettier by+ * them being different type= s with different signedness
+ * and size.
= + *
+ * So this funct= ion allows to wrap all of that into one
+ * boolean return value.
+ *
+ * @return true if snprintf() was successful and not truncated.

+ */
+bool checked_snprintf(char *str, size_t= size, const char *format, ...)
+#ifdef __GNUC__
+#if __USE_MINGW_ANSI_STDIO
+ __attribute__((format(gnu_printf, 3, 4)))
+#else
+ __attribute__((format(__printf__, 3, 4)))
+#endif
+#endif

+ ;

/*

* Verify that a pointer is correctly aligned
= diff --git a/src/openvpn/crypto_mbedtls.c b/src/openvpn/crypto_mbedtls.c
index e8931d7..7aaea3d 100644
--- a/src/openv= pn/crypto_mbedtls.c
+++ b/src/openvpn/crypto_mbedtls.c
@@ -1014,7 +1014,7 @@
{
c= har prefix[256];

- if (snprintf(prefix, sizeof(prefix), "%s:%d", fu= nc, line) >=3D sizeof(prefix))
+ if (!checked_snprintf(prefix, sizeof(prefix), "%s:%d= ", func, line))
{
return= mbed_log_err(flags, errval, func);
}
= @@ -1104,11 +1104,11 @@
char header[1000 + 1] =3D { 0 = };
char footer[1000 + 1] =3D { 0 };
<= /span>
- if (snprintf(heade= r, sizeof(header), "-----BEGIN %s-----\n", name) >=3D sizeof(h= eader))
+ if (!che= cked_snprintf(header, sizeof(header), "-----BEGIN %s-----\n", nam= e))
{
return false; }
- if (= snprintf(footer, sizeof(footer), "-----END %s-----\n", name) >= =3D sizeof(footer))
+= if (!checked_snprintf(footer, sizeof(footer), "-----END %s-----\n&= quot;, name))
{
return false;=
}
@@ -1142,11 +1142,11 @@
<= span> char header[1000 + 1] =3D { 0 };

char footer= [1000 + 1] =3D { 0 };

- if (snprintf(header, sizeof(header), "-----BEGIN= %s-----", name) >=3D sizeof(header))
+ if (!checked_snprintf(header, sizeof(header),= "-----BEGIN %s-----", name))
{
return false;

}
- if (snprintf(footer, sizeof(footer), "= -----END %s-----", name) >=3D sizeof(footer))
+ if (!checked_snprintf(footer, sizeof(= footer), "-----END %s-----", name))
{=
return false;
}
dif= f --git a/src/openvpn/crypto_mbedtls_legacy.c b/src/openvpn/crypto_mbedtls_= legacy.c
index 00fe542..237564c 100644
--- = a/src/openvpn/crypto_mbedtls_legacy.c
+++ b/src/openvpn/cry= pto_mbedtls_legacy.c
@@ -130,7 +130,7 @@
{=
char prefix[256];

- if (snprintf(prefix, sizeof(prefix)= , "%s:%d", func, line) >=3D sizeof(prefix))
+ if (!checked_snprintf(prefix, size= of(prefix), "%s:%d", func, line))
{ return mbed_log_err(flags, errval, func);
= }
@@ -246,11 +246,11 @@
char head= er[1000 + 1] =3D { 0 };
char footer[1000 + 1] =3D { 0 = };

- = if (snprintf(header, sizeof(header), "-----BEGIN %s-----\n", n= ame) >=3D sizeof(header))
+ if (!checked_snprintf(header, sizeof(header), "-----BEGIN= %s-----\n", name))
{
re= turn false;
}
- if (snprintf(footer, sizeof(footer), "-----END %s----= -\n", name) >=3D sizeof(footer))
+ if (!checked_snprintf(footer, sizeof(footer), &quo= t;-----END %s-----\n", name))
{
= return false;
}
@@ -283,11 +28= 3,11 @@
char header[1000 + 1] =3D { 0 };
char footer[1000 + 1] =3D { 0 };


- if (snprintf(header, sizeof(header= ), "-----BEGIN %s-----", name) >=3D sizeof(header))
= + if (!checked_snprintf(head= er, sizeof(header), "-----BEGIN %s-----", name))
= {
return false;
}=
- if (snprintf(footer, siz= eof(footer), "-----END %s-----", name) >=3D sizeof(footer))
+ if (!checked_snpri= ntf(footer, sizeof(footer), "-----END %s-----", name))
= {
return false;
}<= /span>
diff --git a/src/openvpn/dns.c b/src/openvpn/dns.cindex f4f7779..3a294ec 100644
--- a/src/openvpn/dns= .c
+++ b/src/openvpn/dns.c
@@ -485,13 +485,= 11 @@

if (j < 0)
= {
- const i= nt ret =3D snprintf(name, sizeof(name), format, i);
- name_ok =3D (ret > 0 && = ret < sizeof(name));
+ name_ok =3D checked_snprintf(name, sizeof(name), format, i);
}
else
{
- const int ret =3D s= nprintf(name, sizeof(name), format, i, j);
- name_ok =3D (ret > 0 && ret < si= zeof(name));
+ = name_ok =3D checked_snprintf(name, sizeof(name), format, i, j);
= }

if (!name_ok)diff --git a/src/openvpn/env_set.c b/src/openvpn/env_set.cindex 99ac45c..d992097 100644
--- a/src/openvpn/env_= set.c
+++ b/src/openvpn/env_set.c
@@ -334,7= +334,7 @@
strcpy(tmpname, name);
= while (NULL !=3D env_set_get(es, tmpname) && counter < 1000)
{
- = ASSERT(snprintf(tmpname, tmpname_len, "%s_%u", name, count= er) < tmpname_len);
- const int ret = =3D snprintf(fname, sizeof(fname), fname_fmt, max_prefix_len, prefix,
- = (unsigned long)get_random(), (unsigned long)get_random());
= - if (ret < 0 || ret &g= t;=3D sizeof(fname))
= + if (!checked_snprintf(fname, sizeof(fname), fname_fmt, max_prefix_= len, prefix,
+ = (unsigned long)get_random(), (unsigned long)get_rand= om()))
{
msg(M_WARN, = "ERROR: temporary filename too long");
= return NULL;
diff --git a/src/openvpn/proxy.c b/src/open= vpn/proxy.c
index b355827..9f3ec93 100644
-= -- a/src/openvpn/proxy.c
+++ b/src/openvpn/proxy.c@@ -771,11 +771,10 @@
}
=
/* send digest response */<= br>- int sret =3D = snprintf(
- = buf, sizeof(buf),
- "Proxy-Authorization: Digest username=3D\"= ;%s\", realm=3D\"%s\", nonce=3D\"%s\", uri=3D\&quo= t;%s\", qop=3D%s, nc=3D%s, cnonce=3D\"%s\", response=3D\&quo= t;%s\"%s",
- = username, realm, nonce, uri, qop, nonce_count, cnonce, r= esponse, opaque_kv);
- = if (sret >=3D sizeof(buf))
+ if (!checked_snprintf(
<= span style=3D"color: hsl(120, 100%, 40%);">+ buf, si= zeof(buf),

+ = "Proxy-Authorization: Digest username=3D\"%s\"= ;, realm=3D\"%s\", nonce=3D\"%s\", uri=3D\"%s\&quo= t;, qop=3D%s, nc=3D%s, cnonce=3D\"%s\", response=3D\"%s\&quo= t;%s",
+ = username, realm, nonce, uri, qop, nonce_count, cnonce, resp= onse, opaque_kv))
{
= goto error;
}
diff --git a/src/openvpn/socks.c b/src/openvpn/socks.c

i= ndex 5cb5912..19f3d54 100644
--- a/src/openvpn/socks.c
+++ b/src/openvpn/socks.c
@@ -119,10 +119,9 @@<= /span>
goto cleanup;
}

- int sret =3D = snprintf(to_send, sizeof(to_send), "\x01%c%s%c%s", (int)strlen(cr= eds.username),
- = creds.username, (int)strlen(creds.password), creds.passwor= d);
- ASSERT(sret &g= t;=3D 0 && sret <=3D sizeof(to_send));
-
+ ASSERT(checked_snprintf(to_send, sizeof(to_send), "\x01%c= %s%c%s",
+ = (int)strlen(creds.username), creds.username,+ = (int)strlen(creds.password), creds.password));
if (!pr= oxy_send(sd, to_send, strlen(to_send)))
{
goto cleanup;

diff --git a/src/openvpn/ssl_ncp= .c b/src/openvpn/ssl_ncp.c
index 20fd2f0..686f823 100644
--- a/src/openvpn/ssl_ncp.c
+++ b/src/openvpn= /ssl_ncp.c
@@ -198,7 +198,7 @@
size_t = newlen =3D strlen(o->ncp_ciphers) + 1 + strlen(ciphername) + 1; char *ncp_ciphers =3D gc_malloc(newlen, false, &o->gc);=

- = ASSERT(snprintf(ncp_ciphers, newlen, "%s:%s", o->ncp_ciphers,= ciphername) < newlen);
+ ASSERT(checked_snprintf(ncp_ciphers, newlen, "%s:%s", = o->ncp_ciphers, ciphername));
o->ncp_ciphers =3D= ncp_ciphers;
}

diff --g= it a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c
in= dex 8bd8e96..d44f25f 100644
--- a/src/openvpn/ssl_verify.c<= /span>
+++ b/src/openvpn/ssl_verify.c
@@ -548,7 +5= 48,7 @@
goto cleanup;
}

- if (s= nprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PATH_SEPARATOR, serial= ) >=3D sizeof(fn))
+ if (!checked_snprintf(fn, sizeof(fn), "%s%c%s", crl_dir, PA= TH_SEPARATOR, serial))
{
msg(= D_HANDSHAKE, "VERIFY CRL: filename overflow");
= goto cleanup;
diff --git a/src/openvpn/ssl_verify_mb= edtls.c b/src/openvpn/ssl_verify_mbedtls.c
index adeaa13..a= d5479c 100644
--- a/src/openvpn/ssl_verify_mbedtls.c=
+++ b/src/openvpn/ssl_verify_mbedtls.c
@@ -93,9 += 93,7 @@

ret =3D mbedtls_x509_crt= _verify_info(errstr, sizeof(errstr) - 1, "", *flags);
<= span> if (ret <=3D 0

- && snprintf(errstr, sizeof(errstr), "Cou= ld not retrieve error string, flags=3D%" PRIx32,
- *flags)
<= span style=3D"color: hsl(0, 100%, 40%);">- >=3D sizeof= (errstr))

+ = && !checked_snprintf(errstr, sizeof(errstr), "Could not retr= ieve error string, flags=3D%" PRIx32, *flags))
= {
errstr[0] =3D '\0';
}

diff --git a/src/openvpn/tun.c b/src/openvpn/t= un.c
index 3f9ee5d..ecebff7 100644
--- a/sr= c/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ = -3558,9 +3558,7 @@
msg(M_FATAL, "Error en= umerating registry subkeys of key: %s", ADAPTER_KEY);
= }

- int ret =3D snprintf(unit_string, sizeof(unit_string), "= ;%s\\%s", ADAPTER_KEY, enum_name);
-
- = if (ret < 0 || ret >=3D sizeof(unit_string))
+ if (!checked_snprintf(unit_stri= ng, sizeof(unit_string), "%s\\%s", ADAPTER_KEY, enum_name))
{
msg(M_WARN, "Error = constructing unit string for %s", enum_name);
= continue;
@@ -3673,10 +3671,9 @@
= msg(M_FATAL, "Error enumerating registry subkeys of key: %s&qu= ot;, NETWORK_CONNECTIONS_KEY);
}
=
- int ret =3D s= nprintf(connection_string, sizeof(connection_string), "%s\\%s\\Connect= ion",
- = NETWORK_CONNECTIONS_KEY, enum_name);
-
- if (ret < 0 || ret >=3D sizeof(connection_string))=
+ if (!checke= d_snprintf(connection_string, sizeof(connection_string),
+ "%s= \\%s\\Connection",
+ NETWORK_CONNECTIONS_KEY, enum_name))
{
msg(M_WARN, "Error= constructing connection string for %s", enum_name);
= continue;
diff --git a/src/openvpn/win32.c b/sr= c/openvpn/win32.c
index b938d7b..ac449fd 100644
<= span>--- a/src/openvpn/win32.c

+++ b/src/openvpn/win32.c
@@ -881,9 +881,8 @@
char force_path[256]= ;
char *sysroot =3D get_win_sys_path();

- if (snprintf(f= orce_path, sizeof(force_path), "PATH=3D%s\\System32;%s;%s\\System32\\W= bem",
- = sysroot, sysroot, sysroot)
- >=3D sizeof(force_path))
+ if (!checked_snprintf(force_path, sizeof(for= ce_path), "PATH=3D%s\\System32;%s;%s\\System32\\Wbem",
= + sysr= oot, sysroot, sysroot))
{
msg= (M_WARN, "env_block: default path truncated to %s", force_path);<= /span>
}
diff --git a/tests/unit_tests/openvp= n/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c
in= dex 1c78cf0..5f43e0d 100644
--- a/tests/unit_tests/openvpn/= test_buffer.c
+++ b/tests/unit_tests/openvpn/test_buffer.c<= /span>
@@ -424,6 +424,16 @@
#endif
}


+test_= checked_snprintf(void **state)
+{
+ cha= r buf[10];
+ asser= t_true(checked_snprintf(buf, sizeof(buf), "%s", "Hello"= ));
+ assert_true(= checked_snprintf(buf, sizeof(buf), "%s", "Hello Foo"));=
+ assert_false(ch= ecked_snprintf(buf, sizeof(buf), "%s", "Hello Foo!"));<= /span>
+ assert_false(che= cked_snprintf(buf, sizeof(buf), "%s", "Hello World!"));=
+}
+
void
test_buffer_chomp(void **state)

{
@@ -5= 28,6 +538,7 @@
cmocka_unit_test(test_character_cla= ss),
cmocka_unit_test(test_character_string_mod_bu= f),
cmocka_unit_test(test_snprintf),
+ cmocka_unit_test(test_che= cked_snprintf),

cmocka_unit_test(test_buffer_chomp= ),
cmocka_unit_test(test_buffer_parse)
<= span> };


To view, visit change 1489. To= unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ia3477b8ee7a637c15aad7f28514= 4280595cda5d5
Gerrit-Change-Number: 1489
Gerrit-PatchSet: 7
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --kevqqwLLPZ0=--