Collin Funk <collin.fu...@gmail.com> writes:

> I was going to check OmniOS again anyways since on my virtual machine
> test-ptsname_r failed (it is skipped on CI).
>
> It is because OmniOS will fail and put a NUL byte at the first character
> in the buffer. So hopefully that should be an easy fix as well.
I have pushed the attached patch to fix this as well.

For NetBSD, we use a work around to call the systems ptsname_r with a
temporary buffer and then copy that into the result buffer if
successful. This is because the pointer given to the systems ptsname_r
is strlcpy'd into directly. If strlcpy's return value indicates an
error, ERANGE is returned and the buffer is clobbered.

Illumos does something similar except with snprintf instead of
strlcpy. The same workaround applies in this case.

Collin

>From f52b2a3d465e9e7c533726cc3b2f61cbc3e586a2 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Fri, 24 Jan 2025 21:36:11 -0800
Subject: [PATCH] ptsname_r: Work around ptsname_r bug on Solaris 11 OmniOS.

* lib/ptsname_r.c (ptsname_r): Ensure the resulting buffer is not
clobbered if it is too small on Solaris 11 OmniOS.
* m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): On Solaris 11 OmniOS, arrange to
override ptsname_r.
* doc/posix-functions/ptsname_r.texi: Document the bug.
---
 ChangeLog                          | 7 +++++++
 doc/posix-functions/ptsname_r.texi | 2 +-
 lib/ptsname_r.c                    | 2 +-
 m4/ptsname_r.m4                    | 6 ++++--
 4 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6f3cd5d7f0..e909e9abcf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2025-01-24  Collin Funk  <collin.fu...@gmail.com>
 
+	ptsname_r: Work around ptsname_r bug on Solaris 11 OmniOS.
+	* lib/ptsname_r.c (ptsname_r): Ensure the resulting buffer is not
+	clobbered if it is too small on Solaris 11 OmniOS.
+	* m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): On Solaris 11 OmniOS, arrange to
+	override ptsname_r.
+	* doc/posix-functions/ptsname_r.texi: Document the bug.
+
 	strerrorname_np: Work around a bug on Solaris 11 OmniOS.
 	* m4/strerrorname_np.m4 (gl_CHECK_STRERRORNAME_NP): Test if
 	strerrorname_np returns NULL when given ERESTART or ESTRPIPE.
diff --git a/doc/posix-functions/ptsname_r.texi b/doc/posix-functions/ptsname_r.texi
index 1b50cc1378..5237f6a378 100644
--- a/doc/posix-functions/ptsname_r.texi
+++ b/doc/posix-functions/ptsname_r.texi
@@ -34,7 +34,7 @@ @node ptsname_r
 @item
 When this function fails with ERANGE, it stores an empty string as result
 on some platforms:
-NetBSD 10.0.
+NetBSD 10.0, Solaris 11 OmniOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/lib/ptsname_r.c b/lib/ptsname_r.c
index e2d9059088..3452afb844 100644
--- a/lib/ptsname_r.c
+++ b/lib/ptsname_r.c
@@ -68,7 +68,7 @@ ptsname_r (int fd, char *buf, size_t buflen)
 #undef ptsname_r
 {
 #if HAVE_ESSENTIALLY_WORKING_PTSNAME_R
-# if defined __NetBSD__
+# if defined __NetBSD__ || defined __sun
   char tmpbuf[32];
   if (buf == NULL)
     return EINVAL;
diff --git a/m4/ptsname_r.m4 b/m4/ptsname_r.m4
index 36409fddf7..5cd6f91a07 100644
--- a/m4/ptsname_r.m4
+++ b/m4/ptsname_r.m4
@@ -1,5 +1,5 @@
 # ptsname_r.m4
-# serial 10
+# serial 11
 dnl Copyright (C) 2010-2025 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -79,8 +79,10 @@ AC_DEFUN([gl_FUNC_PTSNAME_R]
       dnl result buffer. We don't use an AC_RUN_IFELSE test here, because
       dnl while the bug exists on all platforms, only NetBSD/i386 has the
       dnl files /dev/ptyp[01] on which the bug becomes apparent.
+      dnl
+      dnl On Solaris 11 OmniOS the result buffer is clobbered as well.
       case "$host_os" in
-        netbsd*) REPLACE_PTSNAME_R=1 ;;
+        netbsd* | solaris*) REPLACE_PTSNAME_R=1 ;;
       esac
     fi
   fi
-- 
2.48.1

Reply via email to