File: git-xpg_strerror.diff

package info (click to toggle)
glibc 2.36-9%2Bdeb12u13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 300,148 kB
  • sloc: ansic: 1,055,755; asm: 324,942; makefile: 15,198; python: 12,603; sh: 10,884; cpp: 5,685; awk: 1,883; perl: 518; yacc: 292; pascal: 182; sed: 39
file content (47 lines) | stat: -rw-r--r-- 1,488 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
commit cb033e6b0ca7b8873cd00687ffd1828038a595d3
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Sat Aug 27 14:46:23 2022 +0200

    mach: Make xpg_strerror_r set a message on error
    
    posix advises to have strerror_r fill a message even when we are returning
    an error.
    
    This makes mach's xpg_strerror_r do this, like the generic version does.
    
    Spotted by the libunistring testsuite test-strerror_r

diff --git a/sysdeps/mach/xpg-strerror.c b/sysdeps/mach/xpg-strerror.c
index 92bb67e2bc..de75cc84ae 100644
--- a/sysdeps/mach/xpg-strerror.c
+++ b/sysdeps/mach/xpg-strerror.c
@@ -51,7 +51,11 @@ __xpg_strerror_r (int errnum, char *buf, size_t buflen)
   code = err_get_code (errnum);
 
   if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
-    return EINVAL;
+    {
+      __snprintf (buf, buflen, "%s%X", _("Error in unknown error system: "),
+		  errnum);
+      return EINVAL;
+    }
 
   es = &__mach_error_systems[system];
 
@@ -62,11 +66,11 @@ __xpg_strerror_r (int errnum, char *buf, size_t buflen)
   else
     estr = (const char *) _(es->subsystem[sub].codes[code]);
 
-  size_t estrlen = strlen (estr) + 1;
+  size_t estrlen = strlen (estr);
 
-  if (buflen < estrlen)
-    return ERANGE;
+  /* Terminate the string in any case.  */
+  if (buflen > 0)
+    *((char *) __mempcpy (buf, estr, MIN (buflen - 1, estrlen))) = '\0';
 
-  memcpy (buf, estr, estrlen);
-  return 0;
+  return buflen <= estrlen ? ERANGE : 0;
 }