License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* linux/lib/string.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-06-18 17:57:38 +00:00
|
|
|
* This file should be used only for "library" routines that may have
|
|
|
|
* alternative implementations on specific architectures (generally
|
|
|
|
* found in <asm-xx/string.h>), or get overloaded by FORTIFY_SOURCE.
|
|
|
|
* (Specifically, this file is built with __NO_FORTIFY.)
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2021-06-18 17:57:38 +00:00
|
|
|
* Other helper functions should live in string_helpers.c.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-18 17:57:38 +00:00
|
|
|
#define __NO_FORTIFY
|
2023-12-26 18:00:01 +00:00
|
|
|
#include <linux/bits.h>
|
2012-01-20 23:35:53 +00:00
|
|
|
#include <linux/bug.h>
|
2023-12-26 18:00:01 +00:00
|
|
|
#include <linux/ctype.h>
|
2011-11-17 02:29:17 +00:00
|
|
|
#include <linux/errno.h>
|
2023-12-26 18:00:01 +00:00
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/types.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2023-12-26 18:00:01 +00:00
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/rwonce.h>
|
2024-10-01 19:35:57 +00:00
|
|
|
#include <linux/unaligned.h>
|
2015-04-29 16:52:04 +00:00
|
|
|
#include <asm/word-at-a-time.h>
|
|
|
|
|
2014-10-13 22:54:25 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRNCASECMP
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2014-10-13 22:54:25 +00:00
|
|
|
* strncasecmp - Case insensitive, length-limited string comparison
|
2005-04-16 22:20:36 +00:00
|
|
|
* @s1: One string
|
|
|
|
* @s2: The other string
|
|
|
|
* @len: the maximum number of characters to compare
|
|
|
|
*/
|
2014-10-13 22:54:25 +00:00
|
|
|
int strncasecmp(const char *s1, const char *s2, size_t len)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
/* Yes, Virginia, it had better be unsigned */
|
|
|
|
unsigned char c1, c2;
|
|
|
|
|
2010-03-05 21:43:11 +00:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
c1 = *s1++;
|
|
|
|
c2 = *s2++;
|
|
|
|
if (!c1 || !c2)
|
|
|
|
break;
|
|
|
|
if (c1 == c2)
|
|
|
|
continue;
|
|
|
|
c1 = tolower(c1);
|
|
|
|
c2 = tolower(c2);
|
|
|
|
if (c1 != c2)
|
|
|
|
break;
|
|
|
|
} while (--len);
|
2005-04-16 22:20:36 +00:00
|
|
|
return (int)c1 - (int)c2;
|
|
|
|
}
|
2014-10-13 22:54:25 +00:00
|
|
|
EXPORT_SYMBOL(strncasecmp);
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-03-29 08:18:42 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRCASECMP
|
|
|
|
int strcasecmp(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
int c1, c2;
|
|
|
|
|
|
|
|
do {
|
|
|
|
c1 = tolower(*s1++);
|
|
|
|
c2 = tolower(*s2++);
|
|
|
|
} while (c1 == c2 && c1 != 0);
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strcasecmp);
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRCPY
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strcpy(char *dest, const char *src)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
|
|
|
|
while ((*dest++ = *src++) != '\0')
|
|
|
|
/* nothing */;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strcpy);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRNCPY
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strncpy(char *dest, const char *src, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
|
|
|
|
while (count) {
|
2005-10-30 23:02:11 +00:00
|
|
|
if ((*tmp = *src) != 0)
|
|
|
|
src++;
|
2005-04-16 22:20:36 +00:00
|
|
|
tmp++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strncpy);
|
|
|
|
#endif
|
|
|
|
|
strscpy: write destination buffer only once
The point behind strscpy() was to once and for all avoid all the
problems with 'strncpy()' and later broken "fixed" versions like
strlcpy() that just made things worse.
So strscpy not only guarantees NUL-termination (unlike strncpy), it also
doesn't do unnecessary padding at the destination. But at the same time
also avoids byte-at-a-time reads and writes by _allowing_ some extra NUL
writes - within the size, of course - so that the whole copy can be done
with word operations.
It is also stable in the face of a mutable source string: it explicitly
does not read the source buffer multiple times (so an implementation
using "strnlen()+memcpy()" would be wrong), and does not read the source
buffer past the size (like the mis-design that is strlcpy does).
Finally, the return value is designed to be simple and unambiguous: if
the string cannot be copied fully, it returns an actual negative error,
making error handling clearer and simpler (and the caller already knows
the size of the buffer). Otherwise it returns the string length of the
result.
However, there was one final stability issue that can be important to
callers: the stability of the destination buffer.
In particular, the same way we shouldn't read the source buffer more
than once, we should avoid doing multiple writes to the destination
buffer: first writing a potentially non-terminated string, and then
terminating it with NUL at the end does not result in a stable result
buffer.
Yes, it gives the right result in the end, but if the rule for the
destination buffer was that it is _always_ NUL-terminated even when
accessed concurrently with updates, the final byte of the buffer needs
to always _stay_ as a NUL byte.
[ Note that "final byte is NUL" here is literally about the final byte
in the destination array, not the terminating NUL at the end of the
string itself. There is no attempt to try to make concurrent reads and
writes give any kind of consistent string length or contents, but we
do want to guarantee that there is always at least that final
terminating NUL character at the end of the destination array if it
existed before ]
This is relevant in the kernel for the tsk->comm[] array, for example.
Even without locking (for either readers or writers), we want to know
that while the buffer contents may be garbled, it is always a valid C
string and always has a NUL character at 'comm[TASK_COMM_LEN-1]' (and
never has any "out of thin air" data).
So avoid any "copy possibly non-terminated string, and terminate later"
behavior, and write the destination buffer only once.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-01 17:23:33 +00:00
|
|
|
#ifdef __BIG_ENDIAN
|
|
|
|
# define ALLBUTLAST_BYTE_MASK (~255ul)
|
|
|
|
#else
|
|
|
|
# define ALLBUTLAST_BYTE_MASK (~0ul >> 8)
|
|
|
|
#endif
|
|
|
|
|
string: Allow 2-argument strscpy()
Using sizeof(dst) for the "size" argument in strscpy() is the
overwhelmingly common case. Instead of requiring this everywhere, allow a
2-argument version to be used that will use the sizeof() internally. There
are other functions in the kernel with optional arguments[1], so this
isn't unprecedented, and improves readability. Update and relocate the
kern-doc for strscpy() too, and drop __HAVE_ARCH_STRSCPY as it is unused.
Adjust ARCH=um build to notice the changed export name, as it doesn't
do full header includes for the string helpers.
This could additionally let us save a few hundred lines of code:
1177 files changed, 2455 insertions(+), 3026 deletions(-)
with a treewide cleanup using Coccinelle:
@needless_arg@
expression DST, SRC;
@@
strscpy(DST, SRC
-, sizeof(DST)
)
Link: https://elixir.bootlin.com/linux/v6.7/source/include/linux/pci.h#L1517 [1]
Reviewed-by: Justin Stitt <justinstitt@google.com>
Cc: Andy Shevchenko <andy@kernel.org>
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
2023-09-20 19:38:14 +00:00
|
|
|
ssize_t sized_strscpy(char *dest, const char *src, size_t count)
|
2015-04-29 16:52:04 +00:00
|
|
|
{
|
|
|
|
const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
|
|
|
|
size_t max = count;
|
|
|
|
long res = 0;
|
|
|
|
|
2019-09-25 23:46:20 +00:00
|
|
|
if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
|
2015-04-29 16:52:04 +00:00
|
|
|
return -E2BIG;
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
|
|
|
|
/*
|
|
|
|
* If src is unaligned, don't cross a page boundary,
|
|
|
|
* since we don't know if the next page is mapped.
|
|
|
|
*/
|
|
|
|
if ((long)src & (sizeof(long) - 1)) {
|
|
|
|
size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
|
|
|
|
if (limit < max)
|
|
|
|
max = limit;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* If src or dest is unaligned, don't do word-at-a-time. */
|
|
|
|
if (((long) dest | (long) src) & (sizeof(long) - 1))
|
|
|
|
max = 0;
|
|
|
|
#endif
|
|
|
|
|
2022-09-15 15:03:59 +00:00
|
|
|
/*
|
|
|
|
* read_word_at_a_time() below may read uninitialized bytes after the
|
|
|
|
* trailing zero and use them in comparisons. Disable this optimization
|
|
|
|
* under KMSAN to prevent false positive reports.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_KMSAN))
|
|
|
|
max = 0;
|
|
|
|
|
2015-04-29 16:52:04 +00:00
|
|
|
while (max >= sizeof(unsigned long)) {
|
|
|
|
unsigned long c, data;
|
|
|
|
|
2018-02-01 18:00:50 +00:00
|
|
|
c = read_word_at_a_time(src+res);
|
2015-04-29 16:52:04 +00:00
|
|
|
if (has_zero(c, &data, &constants)) {
|
|
|
|
data = prep_zero_mask(c, data, &constants);
|
|
|
|
data = create_zero_mask(data);
|
2015-10-06 16:37:41 +00:00
|
|
|
*(unsigned long *)(dest+res) = c & zero_bytemask(data);
|
2015-04-29 16:52:04 +00:00
|
|
|
return res + find_zero(data);
|
|
|
|
}
|
strscpy: write destination buffer only once
The point behind strscpy() was to once and for all avoid all the
problems with 'strncpy()' and later broken "fixed" versions like
strlcpy() that just made things worse.
So strscpy not only guarantees NUL-termination (unlike strncpy), it also
doesn't do unnecessary padding at the destination. But at the same time
also avoids byte-at-a-time reads and writes by _allowing_ some extra NUL
writes - within the size, of course - so that the whole copy can be done
with word operations.
It is also stable in the face of a mutable source string: it explicitly
does not read the source buffer multiple times (so an implementation
using "strnlen()+memcpy()" would be wrong), and does not read the source
buffer past the size (like the mis-design that is strlcpy does).
Finally, the return value is designed to be simple and unambiguous: if
the string cannot be copied fully, it returns an actual negative error,
making error handling clearer and simpler (and the caller already knows
the size of the buffer). Otherwise it returns the string length of the
result.
However, there was one final stability issue that can be important to
callers: the stability of the destination buffer.
In particular, the same way we shouldn't read the source buffer more
than once, we should avoid doing multiple writes to the destination
buffer: first writing a potentially non-terminated string, and then
terminating it with NUL at the end does not result in a stable result
buffer.
Yes, it gives the right result in the end, but if the rule for the
destination buffer was that it is _always_ NUL-terminated even when
accessed concurrently with updates, the final byte of the buffer needs
to always _stay_ as a NUL byte.
[ Note that "final byte is NUL" here is literally about the final byte
in the destination array, not the terminating NUL at the end of the
string itself. There is no attempt to try to make concurrent reads and
writes give any kind of consistent string length or contents, but we
do want to guarantee that there is always at least that final
terminating NUL character at the end of the destination array if it
existed before ]
This is relevant in the kernel for the tsk->comm[] array, for example.
Even without locking (for either readers or writers), we want to know
that while the buffer contents may be garbled, it is always a valid C
string and always has a NUL character at 'comm[TASK_COMM_LEN-1]' (and
never has any "out of thin air" data).
So avoid any "copy possibly non-terminated string, and terminate later"
behavior, and write the destination buffer only once.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-01 17:23:33 +00:00
|
|
|
count -= sizeof(unsigned long);
|
|
|
|
if (unlikely(!count)) {
|
|
|
|
c &= ALLBUTLAST_BYTE_MASK;
|
|
|
|
*(unsigned long *)(dest+res) = c;
|
|
|
|
return -E2BIG;
|
|
|
|
}
|
2015-10-06 16:37:41 +00:00
|
|
|
*(unsigned long *)(dest+res) = c;
|
2015-04-29 16:52:04 +00:00
|
|
|
res += sizeof(unsigned long);
|
|
|
|
max -= sizeof(unsigned long);
|
|
|
|
}
|
|
|
|
|
strscpy: write destination buffer only once
The point behind strscpy() was to once and for all avoid all the
problems with 'strncpy()' and later broken "fixed" versions like
strlcpy() that just made things worse.
So strscpy not only guarantees NUL-termination (unlike strncpy), it also
doesn't do unnecessary padding at the destination. But at the same time
also avoids byte-at-a-time reads and writes by _allowing_ some extra NUL
writes - within the size, of course - so that the whole copy can be done
with word operations.
It is also stable in the face of a mutable source string: it explicitly
does not read the source buffer multiple times (so an implementation
using "strnlen()+memcpy()" would be wrong), and does not read the source
buffer past the size (like the mis-design that is strlcpy does).
Finally, the return value is designed to be simple and unambiguous: if
the string cannot be copied fully, it returns an actual negative error,
making error handling clearer and simpler (and the caller already knows
the size of the buffer). Otherwise it returns the string length of the
result.
However, there was one final stability issue that can be important to
callers: the stability of the destination buffer.
In particular, the same way we shouldn't read the source buffer more
than once, we should avoid doing multiple writes to the destination
buffer: first writing a potentially non-terminated string, and then
terminating it with NUL at the end does not result in a stable result
buffer.
Yes, it gives the right result in the end, but if the rule for the
destination buffer was that it is _always_ NUL-terminated even when
accessed concurrently with updates, the final byte of the buffer needs
to always _stay_ as a NUL byte.
[ Note that "final byte is NUL" here is literally about the final byte
in the destination array, not the terminating NUL at the end of the
string itself. There is no attempt to try to make concurrent reads and
writes give any kind of consistent string length or contents, but we
do want to guarantee that there is always at least that final
terminating NUL character at the end of the destination array if it
existed before ]
This is relevant in the kernel for the tsk->comm[] array, for example.
Even without locking (for either readers or writers), we want to know
that while the buffer contents may be garbled, it is always a valid C
string and always has a NUL character at 'comm[TASK_COMM_LEN-1]' (and
never has any "out of thin air" data).
So avoid any "copy possibly non-terminated string, and terminate later"
behavior, and write the destination buffer only once.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-01 17:23:33 +00:00
|
|
|
while (count > 1) {
|
2015-04-29 16:52:04 +00:00
|
|
|
char c;
|
|
|
|
|
|
|
|
c = src[res];
|
|
|
|
dest[res] = c;
|
|
|
|
if (!c)
|
|
|
|
return res;
|
|
|
|
res++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
strscpy: write destination buffer only once
The point behind strscpy() was to once and for all avoid all the
problems with 'strncpy()' and later broken "fixed" versions like
strlcpy() that just made things worse.
So strscpy not only guarantees NUL-termination (unlike strncpy), it also
doesn't do unnecessary padding at the destination. But at the same time
also avoids byte-at-a-time reads and writes by _allowing_ some extra NUL
writes - within the size, of course - so that the whole copy can be done
with word operations.
It is also stable in the face of a mutable source string: it explicitly
does not read the source buffer multiple times (so an implementation
using "strnlen()+memcpy()" would be wrong), and does not read the source
buffer past the size (like the mis-design that is strlcpy does).
Finally, the return value is designed to be simple and unambiguous: if
the string cannot be copied fully, it returns an actual negative error,
making error handling clearer and simpler (and the caller already knows
the size of the buffer). Otherwise it returns the string length of the
result.
However, there was one final stability issue that can be important to
callers: the stability of the destination buffer.
In particular, the same way we shouldn't read the source buffer more
than once, we should avoid doing multiple writes to the destination
buffer: first writing a potentially non-terminated string, and then
terminating it with NUL at the end does not result in a stable result
buffer.
Yes, it gives the right result in the end, but if the rule for the
destination buffer was that it is _always_ NUL-terminated even when
accessed concurrently with updates, the final byte of the buffer needs
to always _stay_ as a NUL byte.
[ Note that "final byte is NUL" here is literally about the final byte
in the destination array, not the terminating NUL at the end of the
string itself. There is no attempt to try to make concurrent reads and
writes give any kind of consistent string length or contents, but we
do want to guarantee that there is always at least that final
terminating NUL character at the end of the destination array if it
existed before ]
This is relevant in the kernel for the tsk->comm[] array, for example.
Even without locking (for either readers or writers), we want to know
that while the buffer contents may be garbled, it is always a valid C
string and always has a NUL character at 'comm[TASK_COMM_LEN-1]' (and
never has any "out of thin air" data).
So avoid any "copy possibly non-terminated string, and terminate later"
behavior, and write the destination buffer only once.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-01 17:23:33 +00:00
|
|
|
/* Force NUL-termination. */
|
|
|
|
dest[res] = '\0';
|
2015-04-29 16:52:04 +00:00
|
|
|
|
strscpy: write destination buffer only once
The point behind strscpy() was to once and for all avoid all the
problems with 'strncpy()' and later broken "fixed" versions like
strlcpy() that just made things worse.
So strscpy not only guarantees NUL-termination (unlike strncpy), it also
doesn't do unnecessary padding at the destination. But at the same time
also avoids byte-at-a-time reads and writes by _allowing_ some extra NUL
writes - within the size, of course - so that the whole copy can be done
with word operations.
It is also stable in the face of a mutable source string: it explicitly
does not read the source buffer multiple times (so an implementation
using "strnlen()+memcpy()" would be wrong), and does not read the source
buffer past the size (like the mis-design that is strlcpy does).
Finally, the return value is designed to be simple and unambiguous: if
the string cannot be copied fully, it returns an actual negative error,
making error handling clearer and simpler (and the caller already knows
the size of the buffer). Otherwise it returns the string length of the
result.
However, there was one final stability issue that can be important to
callers: the stability of the destination buffer.
In particular, the same way we shouldn't read the source buffer more
than once, we should avoid doing multiple writes to the destination
buffer: first writing a potentially non-terminated string, and then
terminating it with NUL at the end does not result in a stable result
buffer.
Yes, it gives the right result in the end, but if the rule for the
destination buffer was that it is _always_ NUL-terminated even when
accessed concurrently with updates, the final byte of the buffer needs
to always _stay_ as a NUL byte.
[ Note that "final byte is NUL" here is literally about the final byte
in the destination array, not the terminating NUL at the end of the
string itself. There is no attempt to try to make concurrent reads and
writes give any kind of consistent string length or contents, but we
do want to guarantee that there is always at least that final
terminating NUL character at the end of the destination array if it
existed before ]
This is relevant in the kernel for the tsk->comm[] array, for example.
Even without locking (for either readers or writers), we want to know
that while the buffer contents may be garbled, it is always a valid C
string and always has a NUL character at 'comm[TASK_COMM_LEN-1]' (and
never has any "out of thin air" data).
So avoid any "copy possibly non-terminated string, and terminate later"
behavior, and write the destination buffer only once.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-01 17:23:33 +00:00
|
|
|
/* Return E2BIG if the source didn't stop */
|
|
|
|
return src[res] ? -E2BIG : res;
|
2015-04-29 16:52:04 +00:00
|
|
|
}
|
string: Allow 2-argument strscpy()
Using sizeof(dst) for the "size" argument in strscpy() is the
overwhelmingly common case. Instead of requiring this everywhere, allow a
2-argument version to be used that will use the sizeof() internally. There
are other functions in the kernel with optional arguments[1], so this
isn't unprecedented, and improves readability. Update and relocate the
kern-doc for strscpy() too, and drop __HAVE_ARCH_STRSCPY as it is unused.
Adjust ARCH=um build to notice the changed export name, as it doesn't
do full header includes for the string helpers.
This could additionally let us save a few hundred lines of code:
1177 files changed, 2455 insertions(+), 3026 deletions(-)
with a treewide cleanup using Coccinelle:
@needless_arg@
expression DST, SRC;
@@
strscpy(DST, SRC
-, sizeof(DST)
)
Link: https://elixir.bootlin.com/linux/v6.7/source/include/linux/pci.h#L1517 [1]
Reviewed-by: Justin Stitt <justinstitt@google.com>
Cc: Andy Shevchenko <andy@kernel.org>
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
2023-09-20 19:38:14 +00:00
|
|
|
EXPORT_SYMBOL(sized_strscpy);
|
2015-04-29 16:52:04 +00:00
|
|
|
|
lib/string.c: implement stpcpy
LLVM implemented a recent "libcall optimization" that lowers calls to
`sprintf(dest, "%s", str)` where the return value is used to
`stpcpy(dest, str) - dest`.
This generally avoids the machinery involved in parsing format strings.
`stpcpy` is just like `strcpy` except it returns the pointer to the new
tail of `dest`. This optimization was introduced into clang-12.
Implement this so that we don't observe linkage failures due to missing
symbol definitions for `stpcpy`.
Similar to last year's fire drill with: commit 5f074f3e192f
("lib/string.c: implement a basic bcmp")
The kernel is somewhere between a "freestanding" environment (no full
libc) and "hosted" environment (many symbols from libc exist with the
same type, function signature, and semantics).
As Peter Anvin notes, there's not really a great way to inform the
compiler that you're targeting a freestanding environment but would like
to opt-in to some libcall optimizations (see pr/47280 below), rather
than opt-out.
Arvind notes, -fno-builtin-* behaves slightly differently between GCC
and Clang, and Clang is missing many __builtin_* definitions, which I
consider a bug in Clang and am working on fixing.
Masahiro summarizes the subtle distinction between compilers justly:
To prevent transformation from foo() into bar(), there are two ways in
Clang to do that; -fno-builtin-foo, and -fno-builtin-bar. There is
only one in GCC; -fno-buitin-foo.
(Any difference in that behavior in Clang is likely a bug from a missing
__builtin_* definition.)
Masahiro also notes:
We want to disable optimization from foo() to bar(),
but we may still benefit from the optimization from
foo() into something else. If GCC implements the same transform, we
would run into a problem because it is not -fno-builtin-bar, but
-fno-builtin-foo that disables that optimization.
In this regard, -fno-builtin-foo would be more future-proof than
-fno-built-bar, but -fno-builtin-foo is still potentially overkill. We
may want to prevent calls from foo() being optimized into calls to
bar(), but we still may want other optimization on calls to foo().
It seems that compilers today don't quite provide the fine grain control
over which libcall optimizations pseudo-freestanding environments would
prefer.
Finally, Kees notes that this interface is unsafe, so we should not
encourage its use. As such, I've removed the declaration from any
header, but it still needs to be exported to avoid linkage errors in
modules.
Reported-by: Sami Tolvanen <samitolvanen@google.com>
Suggested-by: Andy Lavr <andy.lavr@gmail.com>
Suggested-by: Arvind Sankar <nivedita@alum.mit.edu>
Suggested-by: Joe Perches <joe@perches.com>
Suggested-by: Kees Cook <keescook@chromium.org>
Suggested-by: Masahiro Yamada <masahiroy@kernel.org>
Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Tested-by: Nathan Chancellor <natechancellor@gmail.com>
Cc: <stable@vger.kernel.org>
Link: https://lkml.kernel.org/r/20200914161643.938408-1-ndesaulniers@google.com
Link: https://bugs.llvm.org/show_bug.cgi?id=47162
Link: https://bugs.llvm.org/show_bug.cgi?id=47280
Link: https://github.com/ClangBuiltLinux/linux/issues/1126
Link: https://man7.org/linux/man-pages/man3/stpcpy.3.html
Link: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stpcpy.html
Link: https://reviews.llvm.org/D85963
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2020-09-26 04:19:18 +00:00
|
|
|
/**
|
|
|
|
* stpcpy - copy a string from src to dest returning a pointer to the new end
|
|
|
|
* of dest, including src's %NUL-terminator. May overrun dest.
|
|
|
|
* @dest: pointer to end of string being copied into. Must be large enough
|
|
|
|
* to receive copy.
|
|
|
|
* @src: pointer to the beginning of string being copied from. Must not overlap
|
|
|
|
* dest.
|
|
|
|
*
|
|
|
|
* stpcpy differs from strcpy in a key way: the return value is a pointer
|
|
|
|
* to the new %NUL-terminating character in @dest. (For strcpy, the return
|
|
|
|
* value is a pointer to the start of @dest). This interface is considered
|
|
|
|
* unsafe as it doesn't perform bounds checking of the inputs. As such it's
|
|
|
|
* not recommended for usage. Instead, its definition is provided in case
|
|
|
|
* the compiler lowers other libcalls to stpcpy.
|
|
|
|
*/
|
|
|
|
char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
|
|
|
|
char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
|
|
|
|
{
|
|
|
|
while ((*dest++ = *src++) != '\0')
|
|
|
|
/* nothing */;
|
|
|
|
return --dest;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(stpcpy);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRCAT
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strcat(char *dest, const char *src)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
|
|
|
|
while (*dest)
|
|
|
|
dest++;
|
|
|
|
while ((*dest++ = *src++) != '\0')
|
|
|
|
;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strcat);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRNCAT
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strncat(char *dest, const char *src, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
while (*dest)
|
|
|
|
dest++;
|
|
|
|
while ((*dest++ = *src++) != 0) {
|
|
|
|
if (--count == 0) {
|
|
|
|
*dest = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strncat);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRLCAT
|
|
|
|
size_t strlcat(char *dest, const char *src, size_t count)
|
|
|
|
{
|
|
|
|
size_t dsize = strlen(dest);
|
|
|
|
size_t len = strlen(src);
|
|
|
|
size_t res = dsize + len;
|
|
|
|
|
|
|
|
/* This would be a bug */
|
|
|
|
BUG_ON(dsize >= count);
|
|
|
|
|
|
|
|
dest += dsize;
|
|
|
|
count -= dsize;
|
|
|
|
if (len >= count)
|
|
|
|
len = count-1;
|
2023-05-30 08:39:11 +00:00
|
|
|
__builtin_memcpy(dest, src, len);
|
2005-04-16 22:20:36 +00:00
|
|
|
dest[len] = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strlcat);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRCMP
|
|
|
|
/**
|
|
|
|
* strcmp - Compare two strings
|
|
|
|
* @cs: One string
|
|
|
|
* @ct: Another string
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
int strcmp(const char *cs, const char *ct)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2009-11-18 21:31:52 +00:00
|
|
|
unsigned char c1, c2;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
while (1) {
|
2009-11-18 21:31:52 +00:00
|
|
|
c1 = *cs++;
|
|
|
|
c2 = *ct++;
|
|
|
|
if (c1 != c2)
|
|
|
|
return c1 < c2 ? -1 : 1;
|
|
|
|
if (!c1)
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-11-18 21:31:52 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strcmp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRNCMP
|
|
|
|
/**
|
|
|
|
* strncmp - Compare two length-limited strings
|
|
|
|
* @cs: One string
|
|
|
|
* @ct: Another string
|
|
|
|
* @count: The maximum number of bytes to compare
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
int strncmp(const char *cs, const char *ct, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2009-11-18 21:31:52 +00:00
|
|
|
unsigned char c1, c2;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
while (count) {
|
2009-11-18 21:31:52 +00:00
|
|
|
c1 = *cs++;
|
|
|
|
c2 = *ct++;
|
|
|
|
if (c1 != c2)
|
|
|
|
return c1 < c2 ? -1 : 1;
|
|
|
|
if (!c1)
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
count--;
|
|
|
|
}
|
2009-11-18 21:31:52 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strncmp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRCHR
|
|
|
|
/**
|
|
|
|
* strchr - Find the first occurrence of a character in a string
|
|
|
|
* @s: The string to be searched
|
|
|
|
* @c: The character to search for
|
2019-07-16 23:27:15 +00:00
|
|
|
*
|
|
|
|
* Note that the %NUL-terminator is considered part of the string, and can
|
|
|
|
* be searched for.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strchr(const char *s, int c)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-10-30 23:02:11 +00:00
|
|
|
for (; *s != (char)c; ++s)
|
2005-04-16 22:20:36 +00:00
|
|
|
if (*s == '\0')
|
|
|
|
return NULL;
|
2005-10-30 23:02:11 +00:00
|
|
|
return (char *)s;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strchr);
|
|
|
|
#endif
|
|
|
|
|
2014-03-14 17:00:14 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRCHRNUL
|
|
|
|
/**
|
|
|
|
* strchrnul - Find and return a character in a string, or end of string
|
|
|
|
* @s: The string to be searched
|
|
|
|
* @c: The character to search for
|
|
|
|
*
|
|
|
|
* Returns pointer to first occurrence of 'c' in s. If c is not found, then
|
|
|
|
* return a pointer to the null byte at the end of s.
|
|
|
|
*/
|
|
|
|
char *strchrnul(const char *s, int c)
|
|
|
|
{
|
|
|
|
while (*s && *s != (char)c)
|
|
|
|
s++;
|
|
|
|
return (char *)s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strchrnul);
|
|
|
|
#endif
|
|
|
|
|
2020-02-04 01:37:20 +00:00
|
|
|
/**
|
|
|
|
* strnchrnul - Find and return a character in a length limited string,
|
|
|
|
* or end of string
|
|
|
|
* @s: The string to be searched
|
|
|
|
* @count: The number of characters to be searched
|
|
|
|
* @c: The character to search for
|
|
|
|
*
|
|
|
|
* Returns pointer to the first occurrence of 'c' in s. If c is not found,
|
|
|
|
* then return a pointer to the last character of the string.
|
|
|
|
*/
|
|
|
|
char *strnchrnul(const char *s, size_t count, int c)
|
|
|
|
{
|
|
|
|
while (count-- && *s && *s != (char)c)
|
|
|
|
s++;
|
|
|
|
return (char *)s;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRRCHR
|
|
|
|
/**
|
|
|
|
* strrchr - Find the last occurrence of a character in a string
|
|
|
|
* @s: The string to be searched
|
|
|
|
* @c: The character to search for
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strrchr(const char *s, int c)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2015-02-13 22:36:44 +00:00
|
|
|
const char *last = NULL;
|
|
|
|
do {
|
|
|
|
if (*s == (char)c)
|
|
|
|
last = s;
|
|
|
|
} while (*s++);
|
|
|
|
return (char *)last;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strrchr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRNCHR
|
|
|
|
/**
|
|
|
|
* strnchr - Find a character in a length limited string
|
|
|
|
* @s: The string to be searched
|
|
|
|
* @count: The number of characters to be searched
|
|
|
|
* @c: The character to search for
|
2019-07-16 23:27:15 +00:00
|
|
|
*
|
|
|
|
* Note that the %NUL-terminator is considered part of the string, and can
|
|
|
|
* be searched for.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
char *strnchr(const char *s, size_t count, int c)
|
|
|
|
{
|
2019-07-16 23:27:15 +00:00
|
|
|
while (count--) {
|
2005-10-30 23:02:11 +00:00
|
|
|
if (*s == (char)c)
|
|
|
|
return (char *)s;
|
2019-07-16 23:27:15 +00:00
|
|
|
if (*s++ == '\0')
|
|
|
|
break;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strnchr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRLEN
|
2005-10-30 23:02:11 +00:00
|
|
|
size_t strlen(const char *s)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
const char *sc;
|
|
|
|
|
|
|
|
for (sc = s; *sc != '\0'; ++sc)
|
|
|
|
/* nothing */;
|
|
|
|
return sc - s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strlen);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRNLEN
|
2005-10-30 23:02:11 +00:00
|
|
|
size_t strnlen(const char *s, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
const char *sc;
|
|
|
|
|
|
|
|
for (sc = s; count-- && *sc != '\0'; ++sc)
|
|
|
|
/* nothing */;
|
|
|
|
return sc - s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strnlen);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRSPN
|
|
|
|
/**
|
2007-02-10 09:45:59 +00:00
|
|
|
* strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
|
2005-04-16 22:20:36 +00:00
|
|
|
* @s: The string to be searched
|
|
|
|
* @accept: The string to search for
|
|
|
|
*/
|
|
|
|
size_t strspn(const char *s, const char *accept)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
for (p = s; *p != '\0'; ++p) {
|
2022-04-29 21:38:01 +00:00
|
|
|
if (!strchr(accept, *p))
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2022-04-29 21:38:01 +00:00
|
|
|
return p - s;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strspn);
|
|
|
|
#endif
|
|
|
|
|
2006-04-11 05:53:57 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRCSPN
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2007-02-10 09:45:59 +00:00
|
|
|
* strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
|
2005-04-16 22:20:36 +00:00
|
|
|
* @s: The string to be searched
|
|
|
|
* @reject: The string to avoid
|
|
|
|
*/
|
|
|
|
size_t strcspn(const char *s, const char *reject)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
for (p = s; *p != '\0'; ++p) {
|
2022-04-29 21:38:01 +00:00
|
|
|
if (strchr(reject, *p))
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2022-04-29 21:38:01 +00:00
|
|
|
return p - s;
|
2005-10-30 23:02:11 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
EXPORT_SYMBOL(strcspn);
|
2006-04-11 05:53:57 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRPBRK
|
|
|
|
/**
|
|
|
|
* strpbrk - Find the first occurrence of a set of characters
|
|
|
|
* @cs: The string to be searched
|
|
|
|
* @ct: The characters to search for
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strpbrk(const char *cs, const char *ct)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-01-27 15:51:35 +00:00
|
|
|
const char *sc;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2023-01-27 15:51:35 +00:00
|
|
|
for (sc = cs; *sc != '\0'; ++sc) {
|
|
|
|
if (strchr(ct, *sc))
|
|
|
|
return (char *)sc;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-11 05:53:56 +00:00
|
|
|
EXPORT_SYMBOL(strpbrk);
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRSEP
|
|
|
|
/**
|
|
|
|
* strsep - Split a string into tokens
|
|
|
|
* @s: The string to be searched
|
|
|
|
* @ct: The characters to search for
|
|
|
|
*
|
|
|
|
* strsep() updates @s to point after the token, ready for the next call.
|
|
|
|
*
|
|
|
|
* It returns empty tokens, too, behaving exactly like the libc function
|
|
|
|
* of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
|
|
|
|
* Same semantics, slimmer shape. ;)
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strsep(char **s, const char *ct)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-10-30 23:02:11 +00:00
|
|
|
char *sbegin = *s;
|
|
|
|
char *end;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (sbegin == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
end = strpbrk(sbegin, ct);
|
|
|
|
if (end)
|
|
|
|
*end++ = '\0';
|
|
|
|
*s = end;
|
|
|
|
return sbegin;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strsep);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_MEMSET
|
|
|
|
/**
|
|
|
|
* memset - Fill a region of memory with the given value
|
|
|
|
* @s: Pointer to the start of the area.
|
|
|
|
* @c: The byte to fill the area with
|
|
|
|
* @count: The size of the area.
|
|
|
|
*
|
|
|
|
* Do not use memset() to access IO space, use memset_io() instead.
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
void *memset(void *s, int c, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-10-30 23:02:13 +00:00
|
|
|
char *xs = s;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
while (count--)
|
|
|
|
*xs++ = c;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memset);
|
|
|
|
#endif
|
|
|
|
|
2017-09-08 23:13:48 +00:00
|
|
|
#ifndef __HAVE_ARCH_MEMSET16
|
|
|
|
/**
|
|
|
|
* memset16() - Fill a memory area with a uint16_t
|
|
|
|
* @s: Pointer to the start of the area.
|
|
|
|
* @v: The value to fill the area with
|
|
|
|
* @count: The number of values to store
|
|
|
|
*
|
|
|
|
* Differs from memset() in that it fills with a uint16_t instead
|
|
|
|
* of a byte. Remember that @count is the number of uint16_ts to
|
|
|
|
* store, not the number of bytes.
|
|
|
|
*/
|
|
|
|
void *memset16(uint16_t *s, uint16_t v, size_t count)
|
|
|
|
{
|
|
|
|
uint16_t *xs = s;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
*xs++ = v;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memset16);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_MEMSET32
|
|
|
|
/**
|
|
|
|
* memset32() - Fill a memory area with a uint32_t
|
|
|
|
* @s: Pointer to the start of the area.
|
|
|
|
* @v: The value to fill the area with
|
|
|
|
* @count: The number of values to store
|
|
|
|
*
|
|
|
|
* Differs from memset() in that it fills with a uint32_t instead
|
|
|
|
* of a byte. Remember that @count is the number of uint32_ts to
|
|
|
|
* store, not the number of bytes.
|
|
|
|
*/
|
|
|
|
void *memset32(uint32_t *s, uint32_t v, size_t count)
|
|
|
|
{
|
|
|
|
uint32_t *xs = s;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
*xs++ = v;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memset32);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_MEMSET64
|
|
|
|
/**
|
|
|
|
* memset64() - Fill a memory area with a uint64_t
|
|
|
|
* @s: Pointer to the start of the area.
|
|
|
|
* @v: The value to fill the area with
|
|
|
|
* @count: The number of values to store
|
|
|
|
*
|
|
|
|
* Differs from memset() in that it fills with a uint64_t instead
|
|
|
|
* of a byte. Remember that @count is the number of uint64_ts to
|
|
|
|
* store, not the number of bytes.
|
|
|
|
*/
|
|
|
|
void *memset64(uint64_t *s, uint64_t v, size_t count)
|
|
|
|
{
|
|
|
|
uint64_t *xs = s;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
*xs++ = v;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memset64);
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __HAVE_ARCH_MEMCPY
|
|
|
|
/**
|
|
|
|
* memcpy - Copy one area of memory to another
|
|
|
|
* @dest: Where to copy to
|
|
|
|
* @src: Where to copy from
|
|
|
|
* @count: The size of the area.
|
|
|
|
*
|
|
|
|
* You should not use this function to access IO space, use memcpy_toio()
|
|
|
|
* or memcpy_fromio() instead.
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
void *memcpy(void *dest, const void *src, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-10-30 23:02:13 +00:00
|
|
|
char *tmp = dest;
|
2006-04-11 05:54:09 +00:00
|
|
|
const char *s = src;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
while (count--)
|
|
|
|
*tmp++ = *s++;
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memcpy);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_MEMMOVE
|
|
|
|
/**
|
|
|
|
* memmove - Copy one area of memory to another
|
|
|
|
* @dest: Where to copy to
|
|
|
|
* @src: Where to copy from
|
|
|
|
* @count: The size of the area.
|
|
|
|
*
|
|
|
|
* Unlike memcpy(), memmove() copes with overlapping areas.
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
void *memmove(void *dest, const void *src, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-10-30 23:03:19 +00:00
|
|
|
char *tmp;
|
|
|
|
const char *s;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (dest <= src) {
|
2005-10-30 23:02:13 +00:00
|
|
|
tmp = dest;
|
|
|
|
s = src;
|
2005-04-16 22:20:36 +00:00
|
|
|
while (count--)
|
|
|
|
*tmp++ = *s++;
|
2005-10-30 23:02:11 +00:00
|
|
|
} else {
|
2005-10-30 23:02:13 +00:00
|
|
|
tmp = dest;
|
|
|
|
tmp += count;
|
|
|
|
s = src;
|
|
|
|
s += count;
|
2005-04-16 22:20:36 +00:00
|
|
|
while (count--)
|
|
|
|
*--tmp = *--s;
|
2005-10-30 23:02:11 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memmove);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_MEMCMP
|
|
|
|
/**
|
|
|
|
* memcmp - Compare two areas of memory
|
|
|
|
* @cs: One area of memory
|
|
|
|
* @ct: Another area of memory
|
|
|
|
* @count: The size of the area.
|
|
|
|
*/
|
2005-05-05 23:15:17 +00:00
|
|
|
#undef memcmp
|
2014-02-08 07:52:06 +00:00
|
|
|
__visible int memcmp(const void *cs, const void *ct, size_t count)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
const unsigned char *su1, *su2;
|
|
|
|
int res = 0;
|
|
|
|
|
2021-07-21 19:01:28 +00:00
|
|
|
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
|
|
|
|
if (count >= sizeof(unsigned long)) {
|
|
|
|
const unsigned long *u1 = cs;
|
|
|
|
const unsigned long *u2 = ct;
|
|
|
|
do {
|
|
|
|
if (get_unaligned(u1) != get_unaligned(u2))
|
|
|
|
break;
|
|
|
|
u1++;
|
|
|
|
u2++;
|
|
|
|
count -= sizeof(unsigned long);
|
|
|
|
} while (count >= sizeof(unsigned long));
|
|
|
|
cs = u1;
|
|
|
|
ct = u2;
|
|
|
|
}
|
|
|
|
#endif
|
2005-10-30 23:02:11 +00:00
|
|
|
for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((res = *su1 - *su2) != 0)
|
|
|
|
break;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memcmp);
|
|
|
|
#endif
|
|
|
|
|
2019-04-06 01:38:45 +00:00
|
|
|
#ifndef __HAVE_ARCH_BCMP
|
|
|
|
/**
|
|
|
|
* bcmp - returns 0 if and only if the buffers have identical contents.
|
|
|
|
* @a: pointer to first buffer.
|
|
|
|
* @b: pointer to second buffer.
|
|
|
|
* @len: size of buffers.
|
|
|
|
*
|
|
|
|
* The sign or magnitude of a non-zero return value has no particular
|
|
|
|
* meaning, and architectures may implement their own more efficient bcmp(). So
|
|
|
|
* while this particular implementation is a simple (tail) call to memcmp, do
|
|
|
|
* not rely on anything but whether the return value is zero or non-zero.
|
|
|
|
*/
|
|
|
|
int bcmp(const void *a, const void *b, size_t len)
|
|
|
|
{
|
|
|
|
return memcmp(a, b, len);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bcmp);
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __HAVE_ARCH_MEMSCAN
|
|
|
|
/**
|
|
|
|
* memscan - Find a character in an area of memory.
|
|
|
|
* @addr: The memory area
|
|
|
|
* @c: The byte to search for
|
|
|
|
* @size: The size of the area.
|
|
|
|
*
|
|
|
|
* returns the address of the first occurrence of @c, or 1 byte past
|
|
|
|
* the area if @c is not found
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
void *memscan(void *addr, int c, size_t size)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-10-30 23:02:13 +00:00
|
|
|
unsigned char *p = addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
while (size) {
|
2021-07-01 01:56:01 +00:00
|
|
|
if (*p == (unsigned char)c)
|
2005-10-30 23:02:11 +00:00
|
|
|
return (void *)p;
|
2005-04-16 22:20:36 +00:00
|
|
|
p++;
|
|
|
|
size--;
|
|
|
|
}
|
2005-10-30 23:02:11 +00:00
|
|
|
return (void *)p;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memscan);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __HAVE_ARCH_STRSTR
|
|
|
|
/**
|
|
|
|
* strstr - Find the first substring in a %NUL terminated string
|
|
|
|
* @s1: The string to be searched
|
|
|
|
* @s2: The string to search for
|
|
|
|
*/
|
2005-10-30 23:02:11 +00:00
|
|
|
char *strstr(const char *s1, const char *s2)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2010-01-14 02:53:55 +00:00
|
|
|
size_t l1, l2;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
l2 = strlen(s2);
|
|
|
|
if (!l2)
|
2005-10-30 23:02:11 +00:00
|
|
|
return (char *)s1;
|
2005-04-16 22:20:36 +00:00
|
|
|
l1 = strlen(s1);
|
|
|
|
while (l1 >= l2) {
|
|
|
|
l1--;
|
2005-10-30 23:02:11 +00:00
|
|
|
if (!memcmp(s1, s2, l2))
|
|
|
|
return (char *)s1;
|
2005-04-16 22:20:36 +00:00
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strstr);
|
|
|
|
#endif
|
|
|
|
|
2010-01-14 02:53:55 +00:00
|
|
|
#ifndef __HAVE_ARCH_STRNSTR
|
|
|
|
/**
|
|
|
|
* strnstr - Find the first substring in a length-limited string
|
|
|
|
* @s1: The string to be searched
|
|
|
|
* @s2: The string to search for
|
|
|
|
* @len: the maximum number of characters to search
|
|
|
|
*/
|
|
|
|
char *strnstr(const char *s1, const char *s2, size_t len)
|
|
|
|
{
|
2010-03-05 21:43:12 +00:00
|
|
|
size_t l2;
|
2010-01-14 02:53:55 +00:00
|
|
|
|
|
|
|
l2 = strlen(s2);
|
|
|
|
if (!l2)
|
|
|
|
return (char *)s1;
|
2010-03-05 21:43:12 +00:00
|
|
|
while (len >= l2) {
|
|
|
|
len--;
|
2010-01-14 02:53:55 +00:00
|
|
|
if (!memcmp(s1, s2, l2))
|
|
|
|
return (char *)s1;
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(strnstr);
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __HAVE_ARCH_MEMCHR
|
|
|
|
/**
|
|
|
|
* memchr - Find a character in an area of memory.
|
|
|
|
* @s: The memory area
|
|
|
|
* @c: The byte to search for
|
|
|
|
* @n: The size of the area.
|
|
|
|
*
|
|
|
|
* returns the address of the first occurrence of @c, or %NULL
|
|
|
|
* if @c is not found
|
|
|
|
*/
|
|
|
|
void *memchr(const void *s, int c, size_t n)
|
|
|
|
{
|
|
|
|
const unsigned char *p = s;
|
|
|
|
while (n-- != 0) {
|
|
|
|
if ((unsigned char)c == *p++) {
|
2005-10-30 23:02:11 +00:00
|
|
|
return (void *)(p - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memchr);
|
|
|
|
#endif
|
2011-11-01 00:08:07 +00:00
|
|
|
|
|
|
|
static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
|
|
|
|
{
|
|
|
|
while (bytes) {
|
|
|
|
if (*start != value)
|
|
|
|
return (void *)start;
|
|
|
|
start++;
|
|
|
|
bytes--;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* memchr_inv - Find an unmatching character in an area of memory.
|
|
|
|
* @start: The memory area
|
|
|
|
* @c: Find a character other than c
|
|
|
|
* @bytes: The size of the area.
|
|
|
|
*
|
|
|
|
* returns the address of the first character other than @c, or %NULL
|
|
|
|
* if the whole buffer contains just @c.
|
|
|
|
*/
|
|
|
|
void *memchr_inv(const void *start, int c, size_t bytes)
|
|
|
|
{
|
|
|
|
u8 value = c;
|
|
|
|
u64 value64;
|
|
|
|
unsigned int words, prefix;
|
|
|
|
|
|
|
|
if (bytes <= 16)
|
|
|
|
return check_bytes8(start, value, bytes);
|
|
|
|
|
2012-03-23 22:02:14 +00:00
|
|
|
value64 = value;
|
2014-09-13 18:14:53 +00:00
|
|
|
#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
|
2015-11-10 22:45:14 +00:00
|
|
|
value64 *= 0x0101010101010101ULL;
|
2014-09-13 18:14:53 +00:00
|
|
|
#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
|
2012-03-23 22:02:14 +00:00
|
|
|
value64 *= 0x01010101;
|
|
|
|
value64 |= value64 << 32;
|
|
|
|
#else
|
|
|
|
value64 |= value64 << 8;
|
|
|
|
value64 |= value64 << 16;
|
|
|
|
value64 |= value64 << 32;
|
|
|
|
#endif
|
2011-11-01 00:08:07 +00:00
|
|
|
|
2012-03-23 22:02:14 +00:00
|
|
|
prefix = (unsigned long)start % 8;
|
2011-11-01 00:08:07 +00:00
|
|
|
if (prefix) {
|
2012-03-23 22:02:14 +00:00
|
|
|
u8 *r;
|
|
|
|
|
|
|
|
prefix = 8 - prefix;
|
|
|
|
r = check_bytes8(start, value, prefix);
|
2011-11-01 00:08:07 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
start += prefix;
|
|
|
|
bytes -= prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
words = bytes / 8;
|
|
|
|
|
|
|
|
while (words) {
|
|
|
|
if (*(u64 *)start != value64)
|
|
|
|
return check_bytes8(start, value, 8);
|
|
|
|
start += 8;
|
|
|
|
words--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return check_bytes8(start, value, bytes % 8);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(memchr_inv);
|