Skip to contentSkip to conversation

eal: add asprintf() function for Windows

PATCH In review Andre Muezerie 2 participants 4 messages May 6, 2025 – Jun 27, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: add asprintf() function for Windows

The asprintf function is not part of the C standard library but is a GNU extension commonly available in Unix-like systems. It dynamically allocates memory to store the formatted output string, similar to sprintf, but avoids buffer overflow issues by automatically sizing the buffer.

Instead of rewriting it or coming up with some other replacement, this patch makes use of the implementation provided by Neved4.

---
 lib/eal/windows/asprintf.c         | 48 ++++++++++++++++++++++++++++++
 lib/eal/windows/include/asprintf.h | 21 +++++++++++++
 lib/eal/windows/meson.build        |  1 +
 license/exceptions.txt             |  2 ++
 4 files changed, 72 insertions(+)
 create mode 100644 lib/eal/windows/asprintf.c
 create mode 100644 lib/eal/windows/include/asprintf.h
lib/eal/windows/asprintf.c new file +48 -0
new file mode 100644
index 0000000000..6ebc9d0f0a
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: MIT
+ * Copyright(C) 2023 - 2025 Neved4
+ * https://github.com/Neved4/asprintf/tree/main
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "asprintf.h"
+
+#ifndef RTE_TOOLCHAIN_GCC
+int vasprintf(char **strp, const char *fmt, va_list ap)
+{
+	int size, res;
+	va_list cp;
+
+	va_copy(cp, ap);
+	size = vsnprintf(NULL, 0, fmt, cp);
+	va_end(cp);
+
+	if (size < 0)
+		return -1;
+
+	*strp = malloc(size + 1);
+	if (*strp == NULL)
+		return -1;
+
+	res = vsnprintf(*strp, size + 1, fmt, ap);
+	if (res < 0) {
+		free(*strp);
+		return -1;
+	}
+
+	return res;
+}
+
+int asprintf(char **s, const char *fmt, ...)
+{
+	int ret;
+	va_list ap;
+
+	va_start(ap, fmt);
+	ret = vasprintf(s, fmt, ap);
+	va_end(ap);
+
+	return ret;
+}
+#endif
lib/eal/windows/include/asprintf.h new file +21 -0
new file mode 100644
index 0000000000..36d052b808
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: MIT
+ * Copyright(C) 2023 - 2025 Neved4
+ * https://github.com/Neved4/asprintf/tree/main
+ */
+
+/**
+ * @file
+ * asprintf compat.
+ *
+ * This module provides asprintf() and vasprintf().
+ */
+
+#pragma once
+
+#include <stdarg.h>
+
+#ifndef RTE_TOOLCHAIN_GCC
+int vasprintf(char **strp, const char *fmt, va_list ap);
+
+int asprintf(char **s, const char *fmt, ...);
+#endif
lib/eal/windows/meson.build +1 -0
index 7756d417be..06b25a3a09 100644
@@ -4,6 +4,7 @@
 subdir('include')
 
 sources += files(
+        'asprintf.c',
         'eal.c',
         'eal_alarm.c',
         'eal_debug.c',
license/exceptions.txt +2 -0
index d12fac2034..5ea0de5308 100644
@@ -16,4 +16,6 @@ MIT                  | 10/23/2019       | 02/10/2020       | lib/eal/windows/inc
 BSD-2-Clause         | 10/23/2019       | 12/18/2019       | lib/eal/windows/include/getopt.h
 ISC AND BSD-2-Clause | 10/23/2019       | 12/18/2019       | lib/eal/windows/getopt.c
 MIT                  | 10/19/2022       | 10/18/2022       | drivers/net/gve/base/*
+MIT                  | XX/XX/XXXX       | XX/XX/XXXX       | lib/eal/windows/include/asprintf.h
+MIT                  | XX/XX/XXXX       | XX/XX/XXXX       | lib/eal/windows/asprintf.c
 ---------------------------------------------------------------------------------------------------
Signature · 1 line
2.49.0.vfs.0.2
#1 lore raw

06/05/2025 03:43, Andre Muezerie:

Quoted 8 lines

The asprintf function is not part of the C standard library but is a GNU extension commonly available in Unix-like systems. It dynamically allocates memory to store the formatted output string, similar to sprintf, but avoids buffer overflow issues by automatically sizing the buffer.

Instead of rewriting it or coming up with some other replacement, this patch makes use of the implementation provided by Neved4.

Why not using eal_asprintf()?

#2 lore raw
Quoted 7 lines · Thomas Monjalon

06/05/2025 03:43, Andre Muezerie:

Quoted 6 lines

The asprintf function is not part of the C standard library but is a GNU extension commonly available in Unix-like systems. It dynamically allocates memory to store the formatted output string, similar to sprintf, but avoids buffer overflow issues by automatically sizing the buffer.

Quoted 5 lines
Quoted 2 lines

Instead of rewriting it or coming up with some other replacement, this patch makes use of the implementation provided by Neved4.

Why not using eal_asprintf()?

There are many calls being made directly to asprintf() which made me believe there wasn’t an equivalent available for Windows yet.

Thanks for pointing out that is not the case.

I’ll make the necessary changes to call eal_asprintf() instead.

#3 lore raw

12/06/2025 16:46, Andre Muezerie:

Quoted 14 lines · Thomas Monjalon
Quoted 7 lines

06/05/2025 03:43, Andre Muezerie:

Quoted 6 lines

The asprintf function is not part of the C standard library but is a GNU extension commonly available in Unix-like systems. It dynamically allocates memory to store the formatted output string, similar to sprintf, but avoids buffer overflow issues by automatically sizing the buffer.

Quoted 5 lines
Quoted 2 lines

Instead of rewriting it or coming up with some other replacement, this patch makes use of the implementation provided by Neved4.

Why not using eal_asprintf()?

Quoted 6 lines

There are many calls being made directly to asprintf() which made me believe there wasn’t an equivalent available for Windows yet.

Thanks for pointing out that is not the case.

I’ll make the necessary changes to call eal_asprintf() instead.

Thanks to the define in lib/eal/common/eal_private.h, asprintf should automatically call eal_asprintf on Windows.