1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" Modified Sat Jul 24 18:55:27 1993 by Rik Faith (faith@cs.unc.edu)
10 .TH MEMCMP 3 2021-03-22 "Linux man-pages (unreleased)"
12 memcmp \- compare memory areas
15 .RI ( libc ", " \-lc )
18 .B #include <string.h>
20 .BI "int memcmp(const void *" s1 ", const void *" s2 ", size_t " n );
25 function compares the first \fIn\fP bytes (each interpreted as
27 of the memory areas \fIs1\fP and \fIs2\fP.
31 function returns an integer less than, equal to, or
32 greater than zero if the first \fIn\fP bytes of \fIs1\fP is found,
33 respectively, to be less than, to match, or be greater than the first
34 \fIn\fP bytes of \fIs2\fP.
36 For a nonzero return value, the sign is determined by the sign of
37 the difference between the first pair of bytes (interpreted as
46 is zero, the return value is zero.
48 For an explanation of the terms used in this section, see
56 Interface Attribute Value
59 T} Thread safety MT-Safe
65 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
69 to compare security critical data, such as cryptographic secrets,
70 because the required CPU time depends on the number of equal bytes.
71 Instead, a function that performs comparisons in constant time is required.
72 Some operating systems provide such a function (e.g., NetBSD's
73 .BR consttime_memequal ()),
74 but no such function is specified in POSIX.
75 On Linux, it may be necessary to implement such a function oneself.