| 492 | } |
| 493 | |
| 494 | AP_DECLARE(int) ap_regname(const ap_regex_t *preg, |
| 495 | apr_array_header_t *names, const char *prefix, |
| 496 | int upper) |
| 497 | { |
| 498 | char *nametable; |
| 499 | |
| 500 | #ifdef HAVE_PCRE2 |
| 501 | uint32_t namecount; |
| 502 | uint32_t nameentrysize; |
| 503 | uint32_t i; |
| 504 | pcre2_pattern_info((const pcre2_code *)preg->re_pcre, |
| 505 | PCRE2_INFO_NAMECOUNT, &namecount); |
| 506 | pcre2_pattern_info((const pcre2_code *)preg->re_pcre, |
| 507 | PCRE2_INFO_NAMEENTRYSIZE, &nameentrysize); |
| 508 | pcre2_pattern_info((const pcre2_code *)preg->re_pcre, |
| 509 | PCRE2_INFO_NAMETABLE, &nametable); |
| 510 | #else |
| 511 | int namecount; |
| 512 | int nameentrysize; |
| 513 | int i; |
| 514 | pcre_fullinfo((const pcre *)preg->re_pcre, NULL, |
| 515 | PCRE_INFO_NAMECOUNT, &namecount); |
| 516 | pcre_fullinfo((const pcre *)preg->re_pcre, NULL, |
| 517 | PCRE_INFO_NAMEENTRYSIZE, &nameentrysize); |
| 518 | pcre_fullinfo((const pcre *)preg->re_pcre, NULL, |
| 519 | PCRE_INFO_NAMETABLE, &nametable); |
| 520 | #endif |
| 521 | |
| 522 | for (i = 0; i < namecount; i++) { |
| 523 | const char *offset = nametable + i * nameentrysize; |
| 524 | int capture = (((unsigned char)offset[0] << 8) + (unsigned char)offset[1]); |
| 525 | |
| 526 | /* Sanity check: reject unreasonably large capture group numbers. |
| 527 | * PCRE allows up to 65535 groups, but such large numbers would |
| 528 | * cause excessive memory allocation. Limit to a reasonable maximum. |
| 529 | */ |
| 530 | if (capture > 1024) { |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | while (names->nelts <= capture) { |
| 535 | apr_array_push(names); |
| 536 | } |
| 537 | if (upper || prefix) { |
| 538 | char *name = ((char **) names->elts)[capture] = |
| 539 | prefix ? apr_pstrcat(names->pool, prefix, offset + 2, |
| 540 | NULL) : |
| 541 | apr_pstrdup(names->pool, offset + 2); |
| 542 | if (upper) { |
| 543 | ap_str_toupper(name); |
| 544 | } |
| 545 | } |
| 546 | else { |
| 547 | ((const char **)names->elts)[capture] = offset + 2; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return namecount; |
no test coverage detected