aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias EngdegÄrd2020-09-25 17:00:17 +0200
committerMattias EngdegÄrd2020-09-25 17:08:00 +0200
commit497a1ed8bba528bf4078c80bb00b29870eb01e6f (patch)
tree55aec2280ff0b5cf281838ba3260cd2df41d955d /src
parent499848d8407855d8ca24f0c175c602a0f24da074 (diff)
downloademacs-497a1ed8bba528bf4078c80bb00b29870eb01e6f.tar.gz
emacs-497a1ed8bba528bf4078c80bb00b29870eb01e6f.zip
string-search robustness and documentation improvement (bug#43598)
* src/fns.c (Fstring_search): Check START-POS argument range. Simplify logic. Improve doc string. * test/src/fns-tests.el (string-search): Add test cases. * doc/lispref/strings.texi (Text Comparison): Elaborate. * lisp/emacs-lisp/byte-opt.el (pure-fns): Mark string-search as pure.
Diffstat (limited to 'src')
-rw-r--r--src/fns.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/fns.c b/src/fns.c
index 3927e4306e6..2f64d955760 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5456,15 +5456,18 @@ It should not be used for anything security-related. See
5456 5456
5457DEFUN ("string-search", Fstring_search, Sstring_search, 2, 3, 0, 5457DEFUN ("string-search", Fstring_search, Sstring_search, 2, 3, 0,
5458 doc: /* Search for the string NEEDLE in the string HAYSTACK. 5458 doc: /* Search for the string NEEDLE in the string HAYSTACK.
5459The return value is the position of the first instance of NEEDLE in 5459The return value is the position of the first occurrence of NEEDLE in
5460HAYSTACK. 5460HAYSTACK, or nil if no match was found.
5461 5461
5462The optional START-POS argument says where to start searching in 5462The optional START-POS argument says where to start searching in
5463HAYSTACK. If not given, start at the beginning. */) 5463HAYSTACK and defaults to zero (start at the beginning).
5464It must be between zero and the length of HAYSTACK, inclusive.
5465
5466Case is always significant and text properties are ignored. */)
5464 (register Lisp_Object needle, Lisp_Object haystack, Lisp_Object start_pos) 5467 (register Lisp_Object needle, Lisp_Object haystack, Lisp_Object start_pos)
5465{ 5468{
5466 ptrdiff_t start_byte = 0, haybytes; 5469 ptrdiff_t start_byte = 0, haybytes;
5467 char *res = NULL, *haystart; 5470 char *res, *haystart;
5468 5471
5469 CHECK_STRING (needle); 5472 CHECK_STRING (needle);
5470 CHECK_STRING (haystack); 5473 CHECK_STRING (haystack);
@@ -5472,7 +5475,10 @@ HAYSTACK. If not given, start at the beginning. */)
5472 if (!NILP (start_pos)) 5475 if (!NILP (start_pos))
5473 { 5476 {
5474 CHECK_FIXNUM (start_pos); 5477 CHECK_FIXNUM (start_pos);
5475 start_byte = string_char_to_byte (haystack, XFIXNUM (start_pos)); 5478 EMACS_INT start = XFIXNUM (start_pos);
5479 if (start < 0 || start > SCHARS (haystack))
5480 xsignal1 (Qargs_out_of_range, start_pos);
5481 start_byte = string_char_to_byte (haystack, start);
5476 } 5482 }
5477 5483
5478 haystart = SSDATA (haystack) + start_byte; 5484 haystart = SSDATA (haystack) + start_byte;
@@ -5481,13 +5487,13 @@ HAYSTACK. If not given, start at the beginning. */)
5481 if (STRING_MULTIBYTE (haystack) == STRING_MULTIBYTE (needle)) 5487 if (STRING_MULTIBYTE (haystack) == STRING_MULTIBYTE (needle))
5482 res = memmem (haystart, haybytes, 5488 res = memmem (haystart, haybytes,
5483 SSDATA (needle), SBYTES (needle)); 5489 SSDATA (needle), SBYTES (needle));
5484 else if (STRING_MULTIBYTE (haystack) && !STRING_MULTIBYTE (needle)) 5490 else if (STRING_MULTIBYTE (haystack)) /* unibyte needle */
5485 { 5491 {
5486 Lisp_Object multi_needle = string_to_multibyte (needle); 5492 Lisp_Object multi_needle = string_to_multibyte (needle);
5487 res = memmem (haystart, haybytes, 5493 res = memmem (haystart, haybytes,
5488 SSDATA (multi_needle), SBYTES (multi_needle)); 5494 SSDATA (multi_needle), SBYTES (multi_needle));
5489 } 5495 }
5490 else if (!STRING_MULTIBYTE (haystack) && STRING_MULTIBYTE (needle)) 5496 else /* unibyte haystack, multibyte needle */
5491 { 5497 {
5492 Lisp_Object uni_needle = Fstring_as_unibyte (needle); 5498 Lisp_Object uni_needle = Fstring_as_unibyte (needle);
5493 res = memmem (haystart, haybytes, 5499 res = memmem (haystart, haybytes,