Debian Codegraph @

upstream/debian-unstable/man3/memcmp.3

raw
1 '\" t
2 .\" Copyright 1993, David Metcalfe <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH memcmp 3 2026-02-08 "Linux man-pages 6.18"
8 .SH NAME
9 memcmp \- compare memory areas
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <string.h>
16 .P
17 .BR "int memcmp(" "size_t n;"
18 .BI " const void " s1 [ n "], const void " s2 [ n "], size_t " n );
19 .fi
20 .SH DESCRIPTION
21 The
22 .BR memcmp ()
23 function compares the first
24 .I n
25 bytes (each interpreted as
26 .IR "unsigned\ char" )
27 of the memory areas
28 .I s1
29 and
30 .IR s2 .
31 .SH RETURN VALUE
32 The
33 .BR memcmp ()
34 function returns an integer less than, equal to, or
35 greater than zero if the first
36 .I n
37 bytes of
38 .I s1
39 is found,
40 respectively,
41 to be less than, to match, or be greater than the first
42 .I n
43 bytes of
44 .IR s2 .
45 .P
46 For a nonzero return value, the sign is determined by the sign of
47 the difference between the first pair of bytes (interpreted as
48 .IR "unsigned char" )
49 that differ in
50 .I s1
51 and
52 .IR s2 .
53 .P
54 If
55 .I n
56 is zero, the return value is zero.
57 .SH ATTRIBUTES
58 For an explanation of the terms used in this section, see
59 .BR attributes (7).
60 .TS
61 allbox;
62 lbx lb lb
63 l l l.
64 Interface Attribute Value
65 T{
66 .na
67 .nh
68 .BR memcmp ()
69 T} Thread safety MT-Safe
70 .TE
71 .SH STANDARDS
72 C11, POSIX.1-2008.
73 .SH HISTORY
74 POSIX.1-2001, C89, SVr4, 4.3BSD.
75 .SH CAVEATS
76 Do not use
77 .BR memcmp ()
78 to compare confidential data,
79 such as cryptographic secrets,
80 because the CPU time required for the comparison
81 depends on the contents of the addresses compared,
82 this function is subject to timing-based side-channel attacks.
83 In such cases,
84 a function that performs comparisons in deterministic time,
85 depending only on
86 .I n
87 (the quantity of bytes compared)
88 is required.
89 Some operating systems provide such a function (e.g., NetBSD's
90 .BR consttime_memequal ()),
91 but no such function is specified in POSIX.
92 On Linux, you may need to implement such a function yourself.
93 .SH SEE ALSO
94 .BR bstring (3),
95 .BR strcasecmp (3),
96 .BR strcmp (3),
97 .BR strcoll (3),
98 .BR strncasecmp (3),
99 .BR strncmp (3),
100 .BR wmemcmp (3)