public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Re: [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rti...
@ 2023-01-06 15:53 Wilco Dijkstra
  2023-01-06 16:20 ` Alejandro Colomar
  0 siblings, 1 reply; 11+ messages in thread
From: Wilco Dijkstra @ 2023-01-06 15:53 UTC (permalink / raw)
  To: Alejandro Colomar, Paul Eggert, Adhemerval Zanella Netto, linux-man
  Cc: Alejandro Colomar, libc-alpha, G. Branden Robinson

Hi Alex,

> Which C libraries never supported bzero(3)?  It was in POSIX once, so I guess 
> it's supported everywhere in Unix(-like) systems (you can see that I don't care 
> at all about other systems).  Even if only for backwards compatibility, the 
> removal from POSIX will have not affected the portability of bzero(3) in source 
> code (even where libc has removed it, the compiler will provide support).

These functions have caused portability issues since many UNIX systems didn't
support them, neither did Windows nor most of the embedded world. So they
always required extra work - if you do the codesearch on bzero you will find
many examples of those hacks in existing code.

You may not care about anything outside Linux, but many libcs that support
bzero are not optimized. Even GLIBC used a slow C implementation for bzero
until we changed it to call memset. I have no idea what all other libs do (and
given bzero is dead, it doesn't even matter), but bad performance is also a
portability issue.

> So, I don't think that's a real problem yet.  We're not yet (or I believe so) in 
> a point where bzero(3) is non-portable in source code.

It never was portable or well optimized, which were reasons to deprecate it.

> Even simpler: it is unconditionally defined to memcpy() + len in a macro.
> The reason (I guess) is that they didn't even know that mempcpy() exists.

But that means it will never use mempcpy - not in the source, not in the binary.
So they have done the right thing, and there is no argument that adding or
optimizing mempcpy in C libraries will improve nginx.

> Actually, gcc optimizes differently.  When you call mempcpy(3), since it knows 
> it exists, it calls it or replaces it by memcpy(3)+len, depending on 
> optimization flags.  When you call memcpy(3)+len, since it doesn't know if 
> mempcpy(3) is available, it keeps the memcpy(3) call always.

I don't care what -O0 does, what matters is that in almost all cases mempcpy
gets optimized into memcpy, and that is the right thing to do.

> src/nxt_h1proto.c:2290:    p = nxt_cpymem(p, " HTTP/1.1\r\n", 11);
> src/nxt_h1proto.c:2291:    p = nxt_cpymem(p, "Connection: close\r\n", 19);

Sure but one could equally argue that returning src + len is useful too:

p = memscpy (dest1, p, size1);
p = memscpy (dest2, p, size2);

Or return the size so you can easily keep track of how much bytes were copied:

copied += memncpy (dest1, src1, size1);
copied += memncpy (dest2, src2, size2);

And there are lots of other possibilities. So who is to say that mempcpy is better
than all these options?

> From a source code point of view, they let programmers write better/simpler 
> source code than memcpy(3) or memset(3).  That's sugar... yes.  IMO, it's worth it.

Exactly, it's an opinion/personal preference. As I showed, there are other
possible return values, so should we add all of these interfaces just because
some people might like them?

> Having it in libc rather than an external library has the benefit that it will 
> have support from the compiler (better warnings and optimizations).

No. Compiler and libc support are totally different things. If your library is
deemed useful and used in lots of projects, it may be reasonable to add the
headers to GLIBC. But this will not affect compiler optimization - it would
use the same header and produce the same code.

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rti...
@ 2023-01-06  2:26 Wilco Dijkstra
  2023-01-06 13:49 ` Alejandro Colomar
  0 siblings, 1 reply; 11+ messages in thread
From: Wilco Dijkstra @ 2023-01-06  2:26 UTC (permalink / raw)
  To: Alejandro Colomar, Paul Eggert, Adhemerval Zanella Netto, linux-man
  Cc: Alejandro Colomar, libc-alpha, G. Branden Robinson

Hi Alex,

> Many projects redefine those functions themselves, with alternative names, so 
> it's hard to really count how much is the intention of projects to use it, 
> rather than actual use.  Since the standards don't guarantee such functions, 
> projects that care a lot, use a portable name (one that isn't reserved; 
> sometimes they don't even know that there's a GNU extension with that name and 
> use a weird one, such as cpymem() by nginx).

Yeah portability is a big issue with these non-standard functions. So even if you
aren't considering the large cost of supporting these functions in C libraries, there
are also costs in making applications portable, precisely because not all C libraries
will support it...

> The thing is that those APIs are better (imagine that they were all standard, 
> and were all equally known by programmers; which ones would you use?).  Some 
> programmers will want to use the better APIs, independently of libc providing it 
> or not.  In some cases, for high performance programs, good APIs are even more 
> relevant.  Not implementing them in libc, will only mean that projects will roll 
> their own.

No, the use of non-standard functions is the problem here. bzero was deprecated
more than 20 years ago, do you think C libraries will add support and optimize it
even if they never supported it before? If it's non-standard, it's never going to
happen.

If we continue with the mempcpy vs memcpy example of nginx, I presume
nginx implements cpymem() similar to this:

#if HAVE_MEMPCPY_SUPPORT
  return mempcpy (p, q, n);
#else
  return memcpy (p, q, n) + n;
#endif

The define would be set by a special configure check.

Now if nginx got say 10% faster from using mempcpy then that would
be great and it would be worth the trouble. However there is no difference
since compilers typically generate identical code for these cases. So what's
the point of mempcpy exactly?

By all means, create your own special copy interface function - it's just sugar.
But deciding that mempcpy is great and then being forced to do extra work
to make it portable for no gain is what I find insane...

> Where do you suggest that we put such function?  In or out of libc?

Well you mentioned that nginx and many other programs already define their
own memcpy variants. It's perfectly reasonable to do what you proposed and
create a library of inline string functions using standard calls as primitives.
If it is a freely usable and portable, any project that likes it could just add it.

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rti...
@ 2023-01-06  0:02 Wilco Dijkstra
  2023-01-06  0:22 ` Alejandro Colomar
  0 siblings, 1 reply; 11+ messages in thread
From: Wilco Dijkstra @ 2023-01-06  0:02 UTC (permalink / raw)
  To: Alejandro Colomar, Paul Eggert, Adhemerval Zanella Netto, linux-man
  Cc: Alejandro Colomar, libc-alpha, G. Branden Robinson

Hi Alex,

> There are many users of bzero(3) in the wild, and it is a fine API from a 
> usability point of view. 

Since you repeatedly claim lots of use of these functions, I did a quick search
on https://codesearch.debian.net/

bzero: 21440
memset: 563054

mempcpy: 4489
memcpy: 692873

I used "memcpy(" and "memcpy (" and added the results. These overestimate
usage due to prototypes and comments, and don't include memcpy and memset
calls emitted by compilers so in reality the results are even more skewed.

There may be other repositories which can be easily searched, but these results
are clear enough to conclude these functions are dead.

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 11+ messages in thread
* [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rtime.3, rtnetlink.3, strptime.3, NULL.3const, size_t.3type, void.3type, aio.7, netlink.7, unix.7: Prefer bzero(3) over memset(3)
@ 2023-01-05 19:37 Alejandro Colomar
  2023-01-05 20:48 ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 11+ messages in thread
From: Alejandro Colomar @ 2023-01-05 19:37 UTC (permalink / raw)
  To: linux-man
  Cc: Alejandro Colomar, libc-alpha, Wilco Dijkstra, G. Branden Robinson

bzero(3) is simpler to use, and can avoid silly mistakes that are hard
to spot.  memset(3), while it is necessary in a few very-specific cases,
should be avoided when the memory is to be zeroed.

POSIX and ISO can say otherwise, but it doesn't make any sense to
recommend using memset(3) over bzero(3).

Link: <https://stackoverflow.com/a/17097978/6872717>
Cc: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man2/bind.2              | 2 +-
 man2/mount_setattr.2     | 4 ++--
 man2/openat2.2           | 4 ++--
 man2/perf_event_open.2   | 2 +-
 man2/pidfd_send_signal.2 | 2 +-
 man2/recvmmsg.2          | 2 +-
 man2/seccomp_unotify.2   | 2 +-
 man2/select_tut.2        | 6 +++---
 man2/sendmmsg.2          | 6 +++---
 man2/set_thread_area.2   | 2 +-
 man2/sysctl.2            | 2 +-
 man3/bzero.3             | 4 +---
 man3/getaddrinfo.3       | 4 ++--
 man3/getaddrinfo_a.3     | 2 +-
 man3/getutent.3          | 6 +++---
 man3/mbrtowc.3           | 2 +-
 man3/mbsinit.3           | 2 +-
 man3/rtime.3             | 2 +-
 man3/rtnetlink.3         | 2 +-
 man3/strptime.3          | 2 +-
 man3const/NULL.3const    | 2 +-
 man3type/size_t.3type    | 1 +
 man3type/void.3type      | 1 +
 man7/aio.7               | 2 +-
 man7/netlink.7           | 4 ++--
 man7/unix.7              | 6 +++---
 26 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/man2/bind.2 b/man2/bind.2
index 350a2f7e1..33d580f90 100644
--- a/man2/bind.2
+++ b/man2/bind.2
@@ -243,7 +243,7 @@ .SH EXAMPLES
     if (sfd == \-1)
         handle_error("socket");
 
-    memset(&my_addr, 0, sizeof(my_addr));
+    bzero(&my_addr, sizeof(my_addr));
     my_addr.sun_family = AF_UNIX;
     strncpy(my_addr.sun_path, MY_SOCK_PATH,
             sizeof(my_addr.sun_path) \- 1);
diff --git a/man2/mount_setattr.2 b/man2/mount_setattr.2
index cfa1a6e5a..d3c786609 100644
--- a/man2/mount_setattr.2
+++ b/man2/mount_setattr.2
@@ -878,13 +878,13 @@ .SS Extensibility
 .in
 .PP
 Alternatively, the structure can be zero-filled using
-.BR memset (3)
+.BR bzero (3)
 or similar functions:
 .PP
 .in +4n
 .EX
 struct mount_attr attr;
-memset(&attr, 0, sizeof(attr));
+bzero(&attr, sizeof(attr));
 attr.attr_set = MOUNT_ATTR_RDONLY;
 attr.attr_clr = MOUNT_ATTR_NODEV;
 .EE
diff --git a/man2/openat2.2 b/man2/openat2.2
index 19693cd8c..59d515d89 100644
--- a/man2/openat2.2
+++ b/man2/openat2.2
@@ -558,13 +558,13 @@ .SS Extensibility
 .in
 .PP
 or explicitly using
-.BR memset (3)
+.BR bzero (3)
 or similar:
 .PP
 .in +4n
 .EX
 struct open_how how;
-memset(&how, 0, sizeof(how));
+bzero(&how, sizeof(how));
 how.flags = O_RDWR;
 how.resolve = RESOLVE_IN_ROOT;
 .EE
diff --git a/man2/perf_event_open.2 b/man2/perf_event_open.2
index 8e94fb4ac..29af28bf1 100644
--- a/man2/perf_event_open.2
+++ b/man2/perf_event_open.2
@@ -3761,7 +3761,7 @@ .SH EXAMPLES
     long long               count;
     struct perf_event_attr  pe;
 
-    memset(&pe, 0, sizeof(pe));
+    bzero(&pe, sizeof(pe));
     pe.type = PERF_TYPE_HARDWARE;
     pe.size = sizeof(pe);
     pe.config = PERF_COUNT_HW_INSTRUCTIONS;
diff --git a/man2/pidfd_send_signal.2 b/man2/pidfd_send_signal.2
index 2a60ced93..76544888b 100644
--- a/man2/pidfd_send_signal.2
+++ b/man2/pidfd_send_signal.2
@@ -213,7 +213,7 @@ .SH EXAMPLES
     /* Populate a \(aqsiginfo_t\(aq structure for use with
        pidfd_send_signal(). */
 
-    memset(&info, 0, sizeof(info));
+    bzero(&info, sizeof(info));
     info.si_code = SI_QUEUE;
     info.si_signo = sig;
     info.si_errno = 0;
diff --git a/man2/recvmmsg.2 b/man2/recvmmsg.2
index 63a03bd39..d5d94dc86 100644
--- a/man2/recvmmsg.2
+++ b/man2/recvmmsg.2
@@ -244,7 +244,7 @@ .SS Program source
         exit(EXIT_FAILURE);
     }
 
-    memset(msgs, 0, sizeof(msgs));
+    bzero(msgs, sizeof(msgs));
     for (size_t i = 0; i < VLEN; i++) {
         iovecs[i].iov_base         = bufs[i];
         iovecs[i].iov_len          = BUFSIZE;
diff --git a/man2/seccomp_unotify.2 b/man2/seccomp_unotify.2
index 157fbb94a..9e0d83d4a 100644
--- a/man2/seccomp_unotify.2
+++ b/man2/seccomp_unotify.2
@@ -1838,7 +1838,7 @@ .SS Program source
 
         /* Wait for next notification, returning info in \(aq*req\(aq */
 
-        memset(req, 0, sizes.seccomp_notif);
+        bzero(req, sizes.seccomp_notif);
         if (ioctl(notifyFd, SECCOMP_IOCTL_NOTIF_RECV, req) == \-1) {
             if (errno == EINTR)
                 continue;
diff --git a/man2/select_tut.2 b/man2/select_tut.2
index 9d62db023..75480368c 100644
--- a/man2/select_tut.2
+++ b/man2/select_tut.2
@@ -373,7 +373,7 @@ .SH EXAMPLES
         return \-1;
     }
 
-    memset(&addr, 0, sizeof(addr));
+    bzero(&addr, sizeof(addr));
     addr.sin_port = htons(listen_port);
     addr.sin_family = AF_INET;
     if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) {
@@ -399,7 +399,7 @@ .SH EXAMPLES
         return \-1;
     }
 
-    memset(&addr, 0, sizeof(addr));
+    bzero(&addr, sizeof(addr));
     addr.sin_port = htons(connect_port);
     addr.sin_family = AF_INET;
 
@@ -508,7 +508,7 @@ .SH EXAMPLES
             int fd;
 
             addrlen = sizeof(client_addr);
-            memset(&client_addr, 0, addrlen);
+            bzero(&client_addr, addrlen);
             fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);
             if (fd == \-1) {
                 perror("accept()");
diff --git a/man2/sendmmsg.2 b/man2/sendmmsg.2
index 4e5475c45..4b284ab34 100644
--- a/man2/sendmmsg.2
+++ b/man2/sendmmsg.2
@@ -201,17 +201,17 @@ .SH EXAMPLES
         exit(EXIT_FAILURE);
     }
 
-    memset(msg1, 0, sizeof(msg1));
+    bzero(msg1, sizeof(msg1));
     msg1[0].iov_base = "one";
     msg1[0].iov_len = 3;
     msg1[1].iov_base = "two";
     msg1[1].iov_len = 3;
 
-    memset(&msg2, 0, sizeof(msg2));
+    bzero(&msg2, sizeof(msg2));
     msg2.iov_base = "three";
     msg2.iov_len = 5;
 
-    memset(msg, 0, sizeof(msg));
+    bzero(msg, sizeof(msg));
     msg[0].msg_hdr.msg_iov = msg1;
     msg[0].msg_hdr.msg_iovlen = 2;
 
diff --git a/man2/set_thread_area.2 b/man2/set_thread_area.2
index ccfacc70d..263bda38c 100644
--- a/man2/set_thread_area.2
+++ b/man2/set_thread_area.2
@@ -207,7 +207,7 @@ .SH BUGS
 if set, would prevent the descriptor from being considered empty (see
 .BR modify_ldt (2)).
 As a result, the only reliable way to clear a TLS entry is to use
-.BR memset (3)
+.BR bzero (3)
 to zero the entire
 .I user_desc
 structure, including padding bits, and then to set the
diff --git a/man2/sysctl.2 b/man2/sysctl.2
index 679b20a74..8f15fea03 100644
--- a/man2/sysctl.2
+++ b/man2/sysctl.2
@@ -137,7 +137,7 @@ .SH EXAMPLES
     size_t                osnamelth;
     struct __sysctl_args  args;
 
-    memset(&args, 0, sizeof(args));
+    bzero(&args, sizeof(args));
     args.name = name;
     args.nlen = ARRAY_SIZE(name);
     args.oldval = osname;
diff --git a/man3/bzero.3 b/man3/bzero.3
index 1cd2fa664..503ad6c98 100644
--- a/man3/bzero.3
+++ b/man3/bzero.3
@@ -62,9 +62,7 @@ .SH ATTRIBUTES
 .SH STANDARDS
 The
 .BR bzero ()
-function is deprecated (marked as LEGACY in POSIX.1-2001); use
-.BR memset (3)
-in new programs.
+function is marked as LEGACY in POSIX.1-2001;
 POSIX.1-2008 removes the specification of
 .BR bzero ().
 The
diff --git a/man3/getaddrinfo.3 b/man3/getaddrinfo.3
index 1248ae34d..67b049fe2 100644
--- a/man3/getaddrinfo.3
+++ b/man3/getaddrinfo.3
@@ -671,7 +671,7 @@ .SS Server program
         exit(EXIT_FAILURE);
     }
 
-    memset(&hints, 0, sizeof(hints));
+    bzero(&hints, sizeof(hints));
     hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
     hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
     hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
@@ -770,7 +770,7 @@ .SS Client program
 
     /* Obtain address(es) matching host/port. */
 
-    memset(&hints, 0, sizeof(hints));
+    bzero(&hints, sizeof(hints));
     hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
     hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
     hints.ai_flags = 0;
diff --git a/man3/getaddrinfo_a.3 b/man3/getaddrinfo_a.3
index 3ca5d9858..98617ee82 100644
--- a/man3/getaddrinfo_a.3
+++ b/man3/getaddrinfo_a.3
@@ -360,7 +360,7 @@ .SS Synchronous example
             perror("malloc");
             exit(EXIT_FAILURE);
         }
-        memset(reqs[i], 0, sizeof(*reqs[0]));
+        bzero(reqs[i], sizeof(*reqs[0]));
         reqs[i]\->ar_name = argv[i + 1];
     }
 
diff --git a/man3/getutent.3 b/man3/getutent.3
index 32a87b4a9..55961b460 100644
--- a/man3/getutent.3
+++ b/man3/getutent.3
@@ -317,7 +317,7 @@ .SH EXAMPLES
     strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
     time(&entry.ut_time);
     strcpy(entry.ut_user, getpwuid(getuid())\->pw_name);
-    memset(entry.ut_host, 0, UT_HOSTSIZE);
+    bzero(entry.ut_host, UT_HOSTSIZE);
     entry.ut_addr = 0;
     setutent();
     pututline(&entry);
@@ -325,9 +325,9 @@ .SH EXAMPLES
     system("echo after adding entry:;who");
 
     entry.ut_type = DEAD_PROCESS;
-    memset(entry.ut_line, 0, UT_LINESIZE);
+    bzero(entry.ut_line, UT_LINESIZE);
     entry.ut_time = 0;
-    memset(entry.ut_user, 0, UT_NAMESIZE);
+    bzero(entry.ut_user, UT_NAMESIZE);
     setutent();
     pututline(&entry);
 
diff --git a/man3/mbrtowc.3 b/man3/mbrtowc.3
index cf401c9fa..a62e06885 100644
--- a/man3/mbrtowc.3
+++ b/man3/mbrtowc.3
@@ -145,7 +145,7 @@ .SH DESCRIPTION
 .PP
 .in +4n
 .EX
-memset(&a, 0, sizeof(a));
+bzero(&a, sizeof(a));
 .EE
 .in
 .SH RETURN VALUE
diff --git a/man3/mbsinit.3 b/man3/mbsinit.3
index f93d48132..9d6182cab 100644
--- a/man3/mbsinit.3
+++ b/man3/mbsinit.3
@@ -58,7 +58,7 @@ .SH DESCRIPTION
 .in +4n
 .EX
 mbstate_t state;
-memset(&state, 0, sizeof(state));
+bzero(&state, sizeof(state));
 .EE
 .in
 .PP
diff --git a/man3/rtime.3 b/man3/rtime.3
index 0abcf6fce..1cba00162 100644
--- a/man3/rtime.3
+++ b/man3/rtime.3
@@ -131,7 +131,7 @@ .SH EXAMPLES
     struct rpc_timeval  timeout = {1, 0};
     struct sockaddr_in  name;
 
-    memset(&name, 0, sizeof(name));
+    bzero(&name, sizeof(name));
     sethostent(1);
     hent = gethostbyname(servername);
     memcpy(&name.sin_addr, hent\->h_addr, hent\->h_length);
diff --git a/man3/rtnetlink.3 b/man3/rtnetlink.3
index 460b2b5a5..f045facf5 100644
--- a/man3/rtnetlink.3
+++ b/man3/rtnetlink.3
@@ -104,7 +104,7 @@ .SH EXAMPLES
 
 int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
 
-memset(&req, 0, sizeof(req));
+bzero(&req, sizeof(req));
 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.if));
 req.nh.nlmsg_flags = NLM_F_REQUEST;
 req.nh.nlmsg_type = RTM_NEWLINK;
diff --git a/man3/strptime.3 b/man3/strptime.3
index 8c3a569c0..6d451c5c8 100644
--- a/man3/strptime.3
+++ b/man3/strptime.3
@@ -398,7 +398,7 @@ .SH EXAMPLES
     struct tm tm;
     char buf[255];
 
-    memset(&tm, 0, sizeof(tm));
+    bzero(&tm, sizeof(tm));
     strptime("2001\-11\-12 18:31:01", "%Y\-%m\-%d %H:%M:%S", &tm);
     strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
     puts(buf);
diff --git a/man3const/NULL.3const b/man3const/NULL.3const
index 1b60b2b4c..d30b053a6 100644
--- a/man3const/NULL.3const
+++ b/man3const/NULL.3const
@@ -62,7 +62,7 @@ .SH CAVEATS
 .SH BUGS
 When it is necessary to set a pointer variable to a null pointer,
 it is not enough to use
-.BR memset (3)
+.BR bzero (3)
 to zero the pointer
 (this is usually done when zeroing a struct that contains pointers),
 since ISO C and POSIX don't guarantee that a bit pattern of all 0s
diff --git a/man3type/size_t.3type b/man3type/size_t.3type
index ba02993a2..8a10a6634 100644
--- a/man3type/size_t.3type
+++ b/man3type/size_t.3type
@@ -173,6 +173,7 @@ .SH SEE ALSO
 .BR fwrite (3),
 .BR memcmp (3),
 .BR memcpy (3),
+.BR bzero (3),
 .BR memset (3),
 .BR offsetof (3),
 .BR ptrdiff_t (3type)
diff --git a/man3type/void.3type b/man3type/void.3type
index ddcbf482f..df7339377 100644
--- a/man3type/void.3type
+++ b/man3type/void.3type
@@ -71,5 +71,6 @@ .SH SEE ALSO
 .BR malloc (3),
 .BR memcmp (3),
 .BR memcpy (3),
+.BR bzero (3),
 .BR memset (3),
 .BR intptr_t (3type)
diff --git a/man7/aio.7 b/man7/aio.7
index d0d814cb2..c539f3a43 100644
--- a/man7/aio.7
+++ b/man7/aio.7
@@ -145,7 +145,7 @@ .SH STANDARDS
 POSIX.1-2001, POSIX.1-2008.
 .SH NOTES
 It is a good idea to zero out the control block buffer before use (see
-.BR memset (3)).
+.BR bzero (3)).
 The control block buffer and the buffer pointed to by
 .I aio_buf
 must not be changed while the I/O operation is in progress.
diff --git a/man7/netlink.7 b/man7/netlink.7
index 83f7bee13..6e04d9fc5 100644
--- a/man7/netlink.7
+++ b/man7/netlink.7
@@ -539,7 +539,7 @@ .SH EXAMPLES
 .EX
 struct sockaddr_nl sa;
 
-memset(&sa, 0, sizeof(sa));
+bzero(&sa, sizeof(sa));
 sa.nl_family = AF_NETLINK;
 sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
 
@@ -561,7 +561,7 @@ .SH EXAMPLES
 struct msghdr msg;
 
 msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
-memset(&sa, 0, sizeof(sa));
+bzero(&sa, sizeof(sa));
 sa.nl_family = AF_NETLINK;
 nh\->nlmsg_pid = 0;
 nh\->nlmsg_seq = ++sequence_number;
diff --git a/man7/unix.7 b/man7/unix.7
index b290117fc..03f5cad5c 100644
--- a/man7/unix.7
+++ b/man7/unix.7
@@ -912,7 +912,7 @@ .SH BUGS
 addrp = malloc(addrlen + 1);
 if (addrp == NULL)
     /* Handle error */ ;
-memset(addrp, 0, addrlen + 1);
+bzero(addrp, addrlen + 1);
 
 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1)
     /* handle error */ ;
@@ -1004,7 +1004,7 @@ .SS Program source
      * the structure.
      */
 
-    memset(&name, 0, sizeof(name));
+    bzero(&name, sizeof(name));
 
     /* Bind socket to socket name. */
 
@@ -1137,7 +1137,7 @@ .SS Program source
      * the structure.
      */
 
-    memset(&addr, 0, sizeof(addr));
+    bzero(&addr, sizeof(addr));
 
     /* Connect socket to socket address. */
 
-- 
2.39.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-01-06 17:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-06 15:53 [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rti Wilco Dijkstra
2023-01-06 16:20 ` Alejandro Colomar
2023-01-06 17:01   ` Joseph Myers
  -- strict thread matches above, loose matches on Subject: below --
2023-01-06  2:26 Wilco Dijkstra
2023-01-06 13:49 ` Alejandro Colomar
2023-01-06  0:02 Wilco Dijkstra
2023-01-06  0:22 ` Alejandro Colomar
2023-01-06  0:57   ` Alejandro Colomar
2023-01-05 19:37 [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rtime.3, rtnetlink.3, strptime.3, NULL.3const, size_t.3type, void.3type, aio.7, netlink.7, unix.7: Prefer bzero(3) over memset(3) Alejandro Colomar
2023-01-05 20:48 ` Adhemerval Zanella Netto
2023-01-05 20:55   ` Paul Eggert
2023-01-05 21:12     ` [PATCH] bind.2, mount_setattr.2, openat2.2, perf_event_open.2, pidfd_send_signal.2, recvmmsg.2, seccomp_unotify.2, select_tut.2, sendmmsg.2, set_thread_area.2, sysctl.2, bzero.3, getaddrinfo.3, getaddrinfo_a.3, getutent.3, mbrtowc.3, mbsinit.3, rti Wilco Dijkstra
2023-01-05 21:33       ` Alejandro Colomar
2023-01-05 23:30       ` Wilco Dijkstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).