|
15 | 15 | #include "frameobject.h"
|
16 | 16 | #include "pycore_atomic_funcs.h" // _Py_atomic_int_get()
|
17 | 17 | #include "pycore_bitutils.h" // _Py_bswap32()
|
| 18 | +#include "pycore_bytesobject.h" // _PyBytes_Find() |
18 | 19 | #include "pycore_compile.h" // _PyCompile_CodeGen, _PyCompile_OptimizeCfg, _PyCompile_Assemble
|
19 | 20 | #include "pycore_ceval.h" // _PyEval_AddPendingCall
|
20 | 21 | #include "pycore_fileutils.h" // _Py_normpath
|
@@ -441,6 +442,118 @@ test_edit_cost(PyObject *self, PyObject *Py_UNUSED(args))
|
441 | 442 | }
|
442 | 443 |
|
443 | 444 |
|
| 445 | +static int |
| 446 | +check_bytes_find(const char *haystack0, const char *needle0, |
| 447 | + int offset, Py_ssize_t expected) |
| 448 | +{ |
| 449 | + Py_ssize_t len_haystack = strlen(haystack0); |
| 450 | + Py_ssize_t len_needle = strlen(needle0); |
| 451 | + Py_ssize_t result_1 = _PyBytes_Find(haystack0, len_haystack, |
| 452 | + needle0, len_needle, offset); |
| 453 | + if (result_1 != expected) { |
| 454 | + PyErr_Format(PyExc_AssertionError, |
| 455 | + "Incorrect result_1: '%s' in '%s' (offset=%zd)", |
| 456 | + needle0, haystack0, offset); |
| 457 | + return -1; |
| 458 | + } |
| 459 | + // Allocate new buffer with no NULL terminator. |
| 460 | + char *haystack = PyMem_Malloc(len_haystack); |
| 461 | + if (haystack == NULL) { |
| 462 | + PyErr_NoMemory(); |
| 463 | + return -1; |
| 464 | + } |
| 465 | + char *needle = PyMem_Malloc(len_needle); |
| 466 | + if (needle == NULL) { |
| 467 | + PyMem_Free(haystack); |
| 468 | + PyErr_NoMemory(); |
| 469 | + return -1; |
| 470 | + } |
| 471 | + memcpy(haystack, haystack0, len_haystack); |
| 472 | + memcpy(needle, needle0, len_needle); |
| 473 | + Py_ssize_t result_2 = _PyBytes_Find(haystack, len_haystack, |
| 474 | + needle, len_needle, offset); |
| 475 | + PyMem_Free(haystack); |
| 476 | + PyMem_Free(needle); |
| 477 | + if (result_2 != expected) { |
| 478 | + PyErr_Format(PyExc_AssertionError, |
| 479 | + "Incorrect result_2: '%s' in '%s' (offset=%zd)", |
| 480 | + needle0, haystack0, offset); |
| 481 | + return -1; |
| 482 | + } |
| 483 | + return 0; |
| 484 | +} |
| 485 | + |
| 486 | +static int |
| 487 | +check_bytes_find_large(Py_ssize_t len_haystack, Py_ssize_t len_needle, |
| 488 | + const char *needle) |
| 489 | +{ |
| 490 | + char *zeros = PyMem_RawCalloc(len_haystack, 1); |
| 491 | + if (zeros == NULL) { |
| 492 | + PyErr_NoMemory(); |
| 493 | + return -1; |
| 494 | + } |
| 495 | + Py_ssize_t res = _PyBytes_Find(zeros, len_haystack, needle, len_needle, 0); |
| 496 | + PyMem_RawFree(zeros); |
| 497 | + if (res != -1) { |
| 498 | + PyErr_Format(PyExc_AssertionError, |
| 499 | + "check_bytes_find_large(%zd, %zd) found %zd", |
| 500 | + len_haystack, len_needle, res); |
| 501 | + return -1; |
| 502 | + } |
| 503 | + return 0; |
| 504 | +} |
| 505 | + |
| 506 | +static PyObject * |
| 507 | +test_bytes_find(PyObject *self, PyObject *Py_UNUSED(args)) |
| 508 | +{ |
| 509 | + #define CHECK(H, N, O, E) do { \ |
| 510 | + if (check_bytes_find(H, N, O, E) < 0) { \ |
| 511 | + return NULL; \ |
| 512 | + } \ |
| 513 | + } while (0) |
| 514 | + |
| 515 | + CHECK("", "", 0, 0); |
| 516 | + CHECK("Python", "", 0, 0); |
| 517 | + CHECK("Python", "", 3, 3); |
| 518 | + CHECK("Python", "", 6, 6); |
| 519 | + CHECK("Python", "yth", 0, 1); |
| 520 | + CHECK("ython", "yth", 1, 1); |
| 521 | + CHECK("thon", "yth", 2, -1); |
| 522 | + CHECK("Python", "thon", 0, 2); |
| 523 | + CHECK("ython", "thon", 1, 2); |
| 524 | + CHECK("thon", "thon", 2, 2); |
| 525 | + CHECK("hon", "thon", 3, -1); |
| 526 | + CHECK("Pytho", "zz", 0, -1); |
| 527 | + CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "ab", 0, -1); |
| 528 | + CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "ba", 0, -1); |
| 529 | + CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bb", 0, -1); |
| 530 | + CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", "ab", 0, 30); |
| 531 | + CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba", "ba", 0, 30); |
| 532 | + CHECK("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb", "bb", 0, 30); |
| 533 | + #undef CHECK |
| 534 | + |
| 535 | + // Hunt for segfaults |
| 536 | + // n, m chosen here so that (n - m) % (m + 1) == 0 |
| 537 | + // This would make default_find in fastsearch.h access haystack[n]. |
| 538 | + if (check_bytes_find_large(2048, 2, "ab") < 0) { |
| 539 | + return NULL; |
| 540 | + } |
| 541 | + if (check_bytes_find_large(4096, 16, "0123456789abcdef") < 0) { |
| 542 | + return NULL; |
| 543 | + } |
| 544 | + if (check_bytes_find_large(8192, 2, "ab") < 0) { |
| 545 | + return NULL; |
| 546 | + } |
| 547 | + if (check_bytes_find_large(16384, 4, "abcd") < 0) { |
| 548 | + return NULL; |
| 549 | + } |
| 550 | + if (check_bytes_find_large(32768, 2, "ab") < 0) { |
| 551 | + return NULL; |
| 552 | + } |
| 553 | + Py_RETURN_NONE; |
| 554 | +} |
| 555 | + |
| 556 | + |
444 | 557 | static PyObject *
|
445 | 558 | normalize_path(PyObject *self, PyObject *filename)
|
446 | 559 | {
|
@@ -950,6 +1063,7 @@ static PyMethodDef module_functions[] = {
|
950 | 1063 | {"reset_path_config", test_reset_path_config, METH_NOARGS},
|
951 | 1064 | {"test_atomic_funcs", test_atomic_funcs, METH_NOARGS},
|
952 | 1065 | {"test_edit_cost", test_edit_cost, METH_NOARGS},
|
| 1066 | + {"test_bytes_find", test_bytes_find, METH_NOARGS}, |
953 | 1067 | {"normalize_path", normalize_path, METH_O, NULL},
|
954 | 1068 | {"get_getpath_codeobject", get_getpath_codeobject, METH_NOARGS, NULL},
|
955 | 1069 | {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
|
|
0 commit comments