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