<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From f17a6562174ccf658eb35ba7a425d3ac340c1607 Mon Sep 17 00:00:00 2001
From: Robin Gareus &lt;robin@gareus.org&gt;
Date: Mon, 4 Nov 2024 18:46:21 +0100
Subject: [PATCH] use modern C++ for snprintf (#9841)

This replaces vector&lt;char&gt;::operator[] (which now
a constexpr since C++20). We could use &amp;vector::data(),
but a unique_ptr seems more appropriate for the case at hand.
--- a/libs/ardour/broadcast_info.cc
+++ b/libs/ardour/broadcast_info.cc
@@ -39,15 +39,15 @@ namespace ARDOUR
 static void
 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
 {
-	std::vector&lt;char&gt; buf(target_size+1);
+	std::unique_ptr&lt;char[]&gt; buf (new char [target_size + 1]);
 	va_list ap;
 
 	va_start (ap, fmt);
-	vsnprintf (&amp;buf[0], target_size+1, fmt, ap);
+	vsnprintf (buf.get (), target_size+1, fmt, ap);
 	va_end (ap);
 
 	memset (target, 0, target_size);
-	memcpy (target, &amp;buf[0], target_size);
+	memcpy (target, buf.get (), target_size);
 
 }
 
--- a/libs/ardour/export_graph_builder.cc
+++ b/libs/ardour/export_graph_builder.cc
@@ -796,9 +796,9 @@ ExportGraphBuilder::Intermediate::Intermediate (ExportGraphBuilder &amp; parent, Fil
 	int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
 
 	if (parent._realtime) {
-		tmp_file.reset (new TmpFileRt&lt;float&gt; (&amp;tmpfile_path_buf[0], format, channels, config.format-&gt;sample_rate()));
+		tmp_file.reset (new TmpFileRt&lt;float&gt; (tmpfile_path_buf.data (), format, channels, config.format-&gt;sample_rate()));
 	} else {
-		tmp_file.reset (new TmpFileSync&lt;float&gt; (&amp;tmpfile_path_buf[0], format, channels, config.format-&gt;sample_rate()));
+		tmp_file.reset (new TmpFileSync&lt;float&gt; (tmpfile_path_buf.data (), format, channels, config.format-&gt;sample_rate()));
 	}
 
 	tmp_file-&gt;FileWritten.connect_same_thread (post_processing_connection,
--- a/libs/ardour/io.cc
+++ b/libs/ardour/io.cc
@@ -1338,7 +1338,6 @@ IO::bundle_changed (Bundle::Change /*c*/)
 string
 IO::build_legal_port_name (std::shared_ptr&lt;PortSet const&gt; ports, DataType type)
 {
-	const int name_size = AudioEngine::instance()-&gt;port_name_size();
 	int limit;
 	string suffix;
 
@@ -1372,21 +1371,24 @@ IO::build_legal_port_name (std::shared_ptr&lt;PortSet const&gt; ports, DataType type)
 
 	// allow up to 4 digits for the output port number, plus the slash, suffix and extra space
 
+	uint32_t name_size = AudioEngine::instance()-&gt;port_name_size();
 	limit = name_size - AudioEngine::instance()-&gt;my_name().length() - (suffix.length() + 5);
 
-	std::vector&lt;char&gt; buf1(name_size+1);
-	std::vector&lt;char&gt; buf2(name_size+1);
+	++name_size; // allow for \0
+
+	std::unique_ptr&lt;char[]&gt; buf1 (new char[name_size]);
+	std::unique_ptr&lt;char[]&gt; buf2 (new char[name_size]);
 
 	/* colons are illegal in port names, so fix that */
 
 	string nom = legalize_io_name (_name.val());
 
-	snprintf (&amp;buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
+	std::snprintf (buf1.get(), name_size, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
 
-	int port_number = find_port_hole (ports, &amp;buf1[0]);
-	snprintf (&amp;buf2[0], name_size+1, "%s %d", &amp;buf1[0], port_number);
+	int port_number = find_port_hole (ports, buf1.get ());
+	std::snprintf (buf2.get(), name_size, "%s %d", buf1.get (), port_number);
 
-	return string (&amp;buf2[0]);
+	return string (buf2.get ());
 }
 
 int32_t
@@ -1400,17 +1402,17 @@ IO::find_port_hole (std::shared_ptr&lt;PortSet const&gt; ports, const char* base)
 		return 1;
 	}
 
-	/* we only allow up to 4 characters for the port number
-	 */
+	uint32_t const name_size = AudioEngine::instance()-&gt;port_name_size() + 1;
 
+	/* we only allow up to 4 characters for the port number */
 	for (n = 1; n &lt; 9999; ++n) {
-		std::vector&lt;char&gt; buf (AudioEngine::instance()-&gt;port_name_size());
 		PortSet::const_iterator i = ports-&gt;begin ();
 
-		snprintf (&amp;buf[0], buf.size()+1, _("%s %u"), base, n);
+		std::unique_ptr&lt;char[]&gt; buf (new char[name_size]);
+		std::snprintf (buf.get (), name_size, "%s %u", base, n);
 
 		for ( ; i != ports-&gt;end (); ++i) {
-			if (string (i-&gt;name()) == string (&amp;buf[0])) {
+			if (string (i-&gt;name()) == string (buf.get ())) {
 				break;
 			}
 		}
@@ -1526,13 +1528,13 @@ IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
 		case 2:
 			return c == 0 ? _("L") : _("R");
 		default:
-			snprintf (buf, sizeof(buf), "%d", (c + 1));
+			std::snprintf (buf, sizeof(buf), "%d", (c + 1));
 			return buf;
 		}
 
 	} else {
 
-		snprintf (buf, sizeof(buf), "%d", (c + 1));
+		std::snprintf (buf, sizeof(buf), "%d", (c + 1));
 		return buf;
 
 	}
</pre></body></html>