]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/memcmp.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / memcmp.3
CommitLineData
a1eaacb1 1'\" t
fea681da
MK
2.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
5.\"
6.\" References consulted:
7.\" Linux libc source code
8.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9.\" 386BSD man pages
10.\" Modified Sat Jul 24 18:55:27 1993 by Rik Faith (faith@cs.unc.edu)
4c1c5274 11.TH memcmp 3 (date) "Linux man-pages (unreleased)"
fea681da
MK
12.SH NAME
13memcmp \- compare memory areas
2f2d3b46
AC
14.SH LIBRARY
15Standard C library
8fc3b2cf 16.RI ( libc ", " \-lc )
fea681da
MK
17.SH SYNOPSIS
18.nf
19.B #include <string.h>
c6d039a3 20.P
c64cd13e 21.BI "int memcmp(const void " s1 [. n "], const void " s2 [. n "], size_t " n );
fea681da
MK
22.fi
23.SH DESCRIPTION
60a90ecd
MK
24The
25.BR memcmp ()
4c4d76cd
MK
26function compares the first \fIn\fP bytes (each interpreted as
27.IR "unsigned char" )
28of the memory areas \fIs1\fP and \fIs2\fP.
47297adb 29.SH RETURN VALUE
60a90ecd
MK
30The
31.BR memcmp ()
32function returns an integer less than, equal to, or
c13182ef 33greater than zero if the first \fIn\fP bytes of \fIs1\fP is found,
fea681da
MK
34respectively, to be less than, to match, or be greater than the first
35\fIn\fP bytes of \fIs2\fP.
c6d039a3 36.P
3b5b9fd8
MK
37For a nonzero return value, the sign is determined by the sign of
38the difference between the first pair of bytes (interpreted as
39.IR "unsigned char" )
40that differ in
41.I s1
42and
43.IR s2 .
c6d039a3 44.P
7f139a45
MH
45If
46.I n
47is zero, the return value is zero.
c4313c2c 48.SH ATTRIBUTES
d2c36c04
PH
49For an explanation of the terms used in this section, see
50.BR attributes (7).
51.TS
52allbox;
c466875e 53lbx lb lb
d2c36c04
PH
54l l l.
55Interface Attribute Value
56T{
9e54434e
BR
57.na
58.nh
c4313c2c 59.BR memcmp ()
d2c36c04
PH
60T} Thread safety MT-Safe
61.TE
3113c7f3 62.SH STANDARDS
4131356c
AC
63C11, POSIX.1-2008.
64.SH HISTORY
65POSIX.1-2001, C89, SVr4, 4.3BSD.
66.SH CAVEATS
f70fe9d7
MH
67Do not use
68.BR memcmp ()
96507853
BR
69to compare confidential data,
70such as cryptographic secrets,
71because the CPU time required for the comparison
72depends on the contents of the addresses compared,
73this function is subject to timing-based side-channel attacks.
74In such cases,
75a function that performs comparisons in deterministic time,
76depending only on
77.I n
78(the quantity of bytes compared)
79is required.
8b201be5 80Some operating systems provide such a function (e.g., NetBSD's
8d476c39 81.BR consttime_memequal ()),
8b201be5 82but no such function is specified in POSIX.
27473422 83On Linux, you may need to implement such a function yourself.
47297adb 84.SH SEE ALSO
879091c9 85.BR bstring (3),
fea681da
MK
86.BR strcasecmp (3),
87.BR strcmp (3),
88.BR strcoll (3),
89.BR strncasecmp (3),
90.BR strncmp (3),
91.BR wmemcmp (3)