[libc-commits] [libc] [libc] Make socklen_t 32 bits wide (PR #235761)
Mike Lothian via libc-commits
libc-commits at lists.llvm.org
Wed Oct 28 03:00:18 PDT 2026
https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/235761
>From b22b4a02d54cbb8bcb00069cadbca853fb236558 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Mon, 26 Oct 2026 07:17:41 +0000
Subject: [PATCH] [libc] Make socklen_t 32 bits wide
socklen_t was unsigned long. POSIX only asks for an integer type of at
least 32 bits, but the kernel takes and writes back a 32 bit int through
socklen_t pointers, and every supported operating system treats it as a
32 bit type. With the 64 bit type, a length the kernel wrote back filled only
half of the object, and code that declares its own
"typedef unsigned int socklen_t" for portability, such as iptables,
failed to compile.
The layouts of struct msghdr and struct addrinfo do not change, since
the 32 bit member is followed by padding up to the next pointer.
Assisted-by: Claude
---
libc/include/llvm-libc-types/socklen_t.h | 9 ++++-----
libc/test/include/CMakeLists.txt | 10 ++++++++++
libc/test/include/sys_socket_test.cpp | 20 ++++++++++++++++++++
3 files changed, 34 insertions(+), 5 deletions(-)
create mode 100644 libc/test/include/sys_socket_test.cpp
diff --git a/libc/include/llvm-libc-types/socklen_t.h b/libc/include/llvm-libc-types/socklen_t.h
index 5357747f5b836..edfef4d4c845d 100644
--- a/libc/include/llvm-libc-types/socklen_t.h
+++ b/libc/include/llvm-libc-types/socklen_t.h
@@ -9,10 +9,9 @@
#ifndef LLVM_LIBC_TYPES_SOCKLEN_T_H
#define LLVM_LIBC_TYPES_SOCKLEN_T_H
-// The posix standard only says of socklen_t that it must be an integer type of
-// width of at least 32 bits. The long type is defined as being at least 32
-// bits, so an unsigned long should be fine.
-
-typedef unsigned long socklen_t;
+// POSIX only requires an integer type of at least 32 bits, but socklen_t is
+// effectively 32 bits wide in all supported operating systems, and system
+// calls read and write it through a pointer to a 32 bit integer.
+typedef unsigned int socklen_t;
#endif // LLVM_LIBC_TYPES_SOCKLEN_T_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index fb52c23f64bff..f1ca7d3c6c7db 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -115,6 +115,16 @@ add_libc_test(
libc.src.sched.sched_getscheduler
)
+add_libc_test(
+ sys_socket_test
+ SUITE
+ libc-include-tests
+ SRCS
+ sys_socket_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-types.socklen_t
+)
+
add_libc_test(
issubnormal_test
SUITE
diff --git a/libc/test/include/sys_socket_test.cpp b/libc/test/include/sys_socket_test.cpp
new file mode 100644
index 0000000000000..0d87a20ebc975
--- /dev/null
+++ b/libc/test/include/sys_socket_test.cpp
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for the types in sys/socket.h.
+///
+//===----------------------------------------------------------------------===//
+
+#include "include/llvm-libc-types/socklen_t.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSysSocketTest, SocklenT) {
+ static_assert(sizeof(socklen_t) == 4, "");
+ static_assert(static_cast<socklen_t>(-1) > 0, "");
+}
More information about the libc-commits
mailing list