git: 6983b8c39ff3 - stable/13 - login_cap.c: Don't set errno to ERANGE on memory allocation failure

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 21 Dec 2023 13:43:48 UTC
The branch stable/13 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=6983b8c39ff36b3a4c1090b5cca97d450dca7b34

commit 6983b8c39ff36b3a4c1090b5cca97d450dca7b34
Author:     Olivier Certner <olce.freebsd@certner.fr>
AuthorDate: 2023-05-25 11:48:40 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2023-12-21 13:38:54 +0000

    login_cap.c: Don't set errno to ERANGE on memory allocation failure
    
    Modified functions: login_getcaptime(), login_getcapnum(),
    login_getcapsize().
    
    They all call cgetstr(), which returns -2 on such conditions and already
    sets errno to ENOMEM, arguably the appropriate value for these functions
    as well.
    
    No in-tree consumer currently checks for errno on error reported by
    these functions, so this change has no other code impact.
    
    Reviewed by:            kib
    Sponsored by:           Kumacom SAS
    Differential Revision:  https://reviews.freebsd.org/D40342
    
    (cherry picked from commit b8c1aadef9d80786daf731300c33d3a001261422)
    
    Approved by:    markj (mentor)
---
 lib/libutil/login_cap.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/lib/libutil/login_cap.c b/lib/libutil/login_cap.c
index dd7bcefabea6..2b19218a8aaa 100644
--- a/lib/libutil/login_cap.c
+++ b/lib/libutil/login_cap.c
@@ -653,10 +653,8 @@ login_getcaptime(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error)
 
     if ((r = cgetstr(lc->lc_cap, cap, &res)) == -1)
 	return def;
-    else if (r < 0) {
-	errno = ERANGE;
+    else if (r < 0)
 	return error;
-    }
 
     /* "inf" and "infinity" are special cases */
     if (isinfinite(res))
@@ -738,19 +736,18 @@ login_getcapnum(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error)
     /*
      * For BSDI compatibility, try for the tag=<val> first
      */
-    if ((r = cgetstr(lc->lc_cap, cap, &res)) == -1) {
+    r = cgetstr(lc->lc_cap, cap, &res);
+    if (r == -1) {
 	long	lval;
 	/* string capability not present, so try for tag#<val> as numeric */
 	if ((r = cgetnum(lc->lc_cap, cap, &lval)) == -1)
 	    return def; /* Not there, so return default */
-	else if (r >= 0)
+	else if (r < 0)
+	    return error;
+	else
 	    return (rlim_t)lval;
-    }
-
-    if (r < 0) {
-	errno = ERANGE;
+    } else if (r < 0)
 	return error;
-    }
 
     if (isinfinite(res))
 	return RLIM_INFINITY;
@@ -789,10 +786,8 @@ login_getcapsize(login_cap_t *lc, const char *cap, rlim_t def, rlim_t error)
 
     if ((r = cgetstr(lc->lc_cap, cap, &res)) == -1)
 	return def;
-    else if (r < 0) {
-	errno = ERANGE;
+    else if (r < 0)
 	return error;
-    }
 
     if (isinfinite(res))
 	return RLIM_INFINITY;