aboutsummaryrefslogtreecommitdiffstats
path: root/lib/regex_internal.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/regex_internal.c')
-rw-r--r--lib/regex_internal.c152
1 files changed, 79 insertions, 73 deletions
diff --git a/lib/regex_internal.c b/lib/regex_internal.c
index e3ce4abfa6b..f13def37bf6 100644
--- a/lib/regex_internal.c
+++ b/lib/regex_internal.c
@@ -59,7 +59,7 @@ re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
59 re_string_construct_common (str, len, pstr, trans, icase, dfa); 59 re_string_construct_common (str, len, pstr, trans, icase, dfa);
60 60
61 ret = re_string_realloc_buffers (pstr, init_buf_len); 61 ret = re_string_realloc_buffers (pstr, init_buf_len);
62 if (BE (ret != REG_NOERROR, 0)) 62 if (__glibc_unlikely (ret != REG_NOERROR))
63 return ret; 63 return ret;
64 64
65 pstr->word_char = dfa->word_char; 65 pstr->word_char = dfa->word_char;
@@ -84,7 +84,7 @@ re_string_construct (re_string_t *pstr, const char *str, Idx len,
84 if (len > 0) 84 if (len > 0)
85 { 85 {
86 ret = re_string_realloc_buffers (pstr, len + 1); 86 ret = re_string_realloc_buffers (pstr, len + 1);
87 if (BE (ret != REG_NOERROR, 0)) 87 if (__glibc_unlikely (ret != REG_NOERROR))
88 return ret; 88 return ret;
89 } 89 }
90 pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str; 90 pstr->mbs = pstr->mbs_allocated ? pstr->mbs : (unsigned char *) str;
@@ -97,14 +97,14 @@ re_string_construct (re_string_t *pstr, const char *str, Idx len,
97 while (1) 97 while (1)
98 { 98 {
99 ret = build_wcs_upper_buffer (pstr); 99 ret = build_wcs_upper_buffer (pstr);
100 if (BE (ret != REG_NOERROR, 0)) 100 if (__glibc_unlikely (ret != REG_NOERROR))
101 return ret; 101 return ret;
102 if (pstr->valid_raw_len >= len) 102 if (pstr->valid_raw_len >= len)
103 break; 103 break;
104 if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max) 104 if (pstr->bufs_len > pstr->valid_len + dfa->mb_cur_max)
105 break; 105 break;
106 ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2); 106 ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
107 if (BE (ret != REG_NOERROR, 0)) 107 if (__glibc_unlikely (ret != REG_NOERROR))
108 return ret; 108 return ret;
109 } 109 }
110 } 110 }
@@ -146,17 +146,18 @@ re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
146 146
147 /* Avoid overflow in realloc. */ 147 /* Avoid overflow in realloc. */
148 const size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx)); 148 const size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx));
149 if (BE (MIN (IDX_MAX, SIZE_MAX / max_object_size) < new_buf_len, 0)) 149 if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size)
150 < new_buf_len))
150 return REG_ESPACE; 151 return REG_ESPACE;
151 152
152 new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len); 153 new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
153 if (BE (new_wcs == NULL, 0)) 154 if (__glibc_unlikely (new_wcs == NULL))
154 return REG_ESPACE; 155 return REG_ESPACE;
155 pstr->wcs = new_wcs; 156 pstr->wcs = new_wcs;
156 if (pstr->offsets != NULL) 157 if (pstr->offsets != NULL)
157 { 158 {
158 Idx *new_offsets = re_realloc (pstr->offsets, Idx, new_buf_len); 159 Idx *new_offsets = re_realloc (pstr->offsets, Idx, new_buf_len);
159 if (BE (new_offsets == NULL, 0)) 160 if (__glibc_unlikely (new_offsets == NULL))
160 return REG_ESPACE; 161 return REG_ESPACE;
161 pstr->offsets = new_offsets; 162 pstr->offsets = new_offsets;
162 } 163 }
@@ -166,7 +167,7 @@ re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
166 { 167 {
167 unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char, 168 unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
168 new_buf_len); 169 new_buf_len);
169 if (BE (new_mbs == NULL, 0)) 170 if (__glibc_unlikely (new_mbs == NULL))
170 return REG_ESPACE; 171 return REG_ESPACE;
171 pstr->mbs = new_mbs; 172 pstr->mbs = new_mbs;
172 } 173 }
@@ -230,7 +231,7 @@ build_wcs_buffer (re_string_t *pstr)
230 remain_len = end_idx - byte_idx; 231 remain_len = end_idx - byte_idx;
231 prev_st = pstr->cur_state; 232 prev_st = pstr->cur_state;
232 /* Apply the translation if we need. */ 233 /* Apply the translation if we need. */
233 if (BE (pstr->trans != NULL, 0)) 234 if (__glibc_unlikely (pstr->trans != NULL))
234 { 235 {
235 int i, ch; 236 int i, ch;
236 237
@@ -244,17 +245,18 @@ build_wcs_buffer (re_string_t *pstr)
244 else 245 else
245 p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx; 246 p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + byte_idx;
246 mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state); 247 mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
247 if (BE (mbclen == (size_t) -1 || mbclen == 0 248 if (__glibc_unlikely (mbclen == (size_t) -1 || mbclen == 0
248 || (mbclen == (size_t) -2 && pstr->bufs_len >= pstr->len), 0)) 249 || (mbclen == (size_t) -2
250 && pstr->bufs_len >= pstr->len)))
249 { 251 {
250 /* We treat these cases as a singlebyte character. */ 252 /* We treat these cases as a singlebyte character. */
251 mbclen = 1; 253 mbclen = 1;
252 wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx]; 254 wc = (wchar_t) pstr->raw_mbs[pstr->raw_mbs_idx + byte_idx];
253 if (BE (pstr->trans != NULL, 0)) 255 if (__glibc_unlikely (pstr->trans != NULL))
254 wc = pstr->trans[wc]; 256 wc = pstr->trans[wc];
255 pstr->cur_state = prev_st; 257 pstr->cur_state = prev_st;
256 } 258 }
257 else if (BE (mbclen == (size_t) -2, 0)) 259 else if (__glibc_unlikely (mbclen == (size_t) -2))
258 { 260 {
259 /* The buffer doesn't have enough space, finish to build. */ 261 /* The buffer doesn't have enough space, finish to build. */
260 pstr->cur_state = prev_st; 262 pstr->cur_state = prev_st;
@@ -317,7 +319,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
317 mbclen = __mbrtowc (&wc, 319 mbclen = __mbrtowc (&wc,
318 ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx 320 ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
319 + byte_idx), remain_len, &pstr->cur_state); 321 + byte_idx), remain_len, &pstr->cur_state);
320 if (BE (0 < mbclen && mbclen < (size_t) -2, 1)) 322 if (__glibc_likely (0 < mbclen && mbclen < (size_t) -2))
321 { 323 {
322 wchar_t wcu = __towupper (wc); 324 wchar_t wcu = __towupper (wc);
323 if (wcu != wc) 325 if (wcu != wc)
@@ -325,7 +327,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
325 size_t mbcdlen; 327 size_t mbcdlen;
326 328
327 mbcdlen = __wcrtomb (buf, wcu, &prev_st); 329 mbcdlen = __wcrtomb (buf, wcu, &prev_st);
328 if (BE (mbclen == mbcdlen, 1)) 330 if (__glibc_likely (mbclen == mbcdlen))
329 memcpy (pstr->mbs + byte_idx, buf, mbclen); 331 memcpy (pstr->mbs + byte_idx, buf, mbclen);
330 else 332 else
331 { 333 {
@@ -350,7 +352,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
350 pstr->mbs[byte_idx] = ch; 352 pstr->mbs[byte_idx] = ch;
351 /* And also cast it to wide char. */ 353 /* And also cast it to wide char. */
352 pstr->wcs[byte_idx++] = (wchar_t) ch; 354 pstr->wcs[byte_idx++] = (wchar_t) ch;
353 if (BE (mbclen == (size_t) -1, 0)) 355 if (__glibc_unlikely (mbclen == (size_t) -1))
354 pstr->cur_state = prev_st; 356 pstr->cur_state = prev_st;
355 } 357 }
356 else 358 else
@@ -372,7 +374,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
372 offsets_needed: 374 offsets_needed:
373 remain_len = end_idx - byte_idx; 375 remain_len = end_idx - byte_idx;
374 prev_st = pstr->cur_state; 376 prev_st = pstr->cur_state;
375 if (BE (pstr->trans != NULL, 0)) 377 if (__glibc_unlikely (pstr->trans != NULL))
376 { 378 {
377 int i, ch; 379 int i, ch;
378 380
@@ -386,7 +388,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
386 else 388 else
387 p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx; 389 p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
388 mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state); 390 mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
389 if (BE (0 < mbclen && mbclen < (size_t) -2, 1)) 391 if (__glibc_likely (0 < mbclen && mbclen < (size_t) -2))
390 { 392 {
391 wchar_t wcu = __towupper (wc); 393 wchar_t wcu = __towupper (wc);
392 if (wcu != wc) 394 if (wcu != wc)
@@ -394,7 +396,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
394 size_t mbcdlen; 396 size_t mbcdlen;
395 397
396 mbcdlen = __wcrtomb ((char *) buf, wcu, &prev_st); 398 mbcdlen = __wcrtomb ((char *) buf, wcu, &prev_st);
397 if (BE (mbclen == mbcdlen, 1)) 399 if (__glibc_likely (mbclen == mbcdlen))
398 memcpy (pstr->mbs + byte_idx, buf, mbclen); 400 memcpy (pstr->mbs + byte_idx, buf, mbclen);
399 else if (mbcdlen != (size_t) -1) 401 else if (mbcdlen != (size_t) -1)
400 { 402 {
@@ -444,7 +446,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
444 else 446 else
445 memcpy (pstr->mbs + byte_idx, p, mbclen); 447 memcpy (pstr->mbs + byte_idx, p, mbclen);
446 448
447 if (BE (pstr->offsets_needed != 0, 0)) 449 if (__glibc_unlikely (pstr->offsets_needed != 0))
448 { 450 {
449 size_t i; 451 size_t i;
450 for (i = 0; i < mbclen; ++i) 452 for (i = 0; i < mbclen; ++i)
@@ -463,17 +465,17 @@ build_wcs_upper_buffer (re_string_t *pstr)
463 /* It is an invalid character or '\0'. Just use the byte. */ 465 /* It is an invalid character or '\0'. Just use the byte. */
464 int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx]; 466 int ch = pstr->raw_mbs[pstr->raw_mbs_idx + src_idx];
465 467
466 if (BE (pstr->trans != NULL, 0)) 468 if (__glibc_unlikely (pstr->trans != NULL))
467 ch = pstr->trans [ch]; 469 ch = pstr->trans [ch];
468 pstr->mbs[byte_idx] = ch; 470 pstr->mbs[byte_idx] = ch;
469 471
470 if (BE (pstr->offsets_needed != 0, 0)) 472 if (__glibc_unlikely (pstr->offsets_needed != 0))
471 pstr->offsets[byte_idx] = src_idx; 473 pstr->offsets[byte_idx] = src_idx;
472 ++src_idx; 474 ++src_idx;
473 475
474 /* And also cast it to wide char. */ 476 /* And also cast it to wide char. */
475 pstr->wcs[byte_idx++] = (wchar_t) ch; 477 pstr->wcs[byte_idx++] = (wchar_t) ch;
476 if (BE (mbclen == (size_t) -1, 0)) 478 if (__glibc_unlikely (mbclen == (size_t) -1))
477 pstr->cur_state = prev_st; 479 pstr->cur_state = prev_st;
478 } 480 }
479 else 481 else
@@ -508,7 +510,8 @@ re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
508 prev_st = pstr->cur_state; 510 prev_st = pstr->cur_state;
509 mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx, 511 mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx,
510 remain_len, &pstr->cur_state); 512 remain_len, &pstr->cur_state);
511 if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0)) 513 if (__glibc_unlikely (mbclen == (size_t) -2 || mbclen == (size_t) -1
514 || mbclen == 0))
512 { 515 {
513 /* We treat these cases as a single byte character. */ 516 /* We treat these cases as a single byte character. */
514 if (mbclen == 0 || remain_len == 0) 517 if (mbclen == 0 || remain_len == 0)
@@ -540,7 +543,7 @@ build_upper_buffer (re_string_t *pstr)
540 for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx) 543 for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
541 { 544 {
542 int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx]; 545 int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
543 if (BE (pstr->trans != NULL, 0)) 546 if (__glibc_unlikely (pstr->trans != NULL))
544 ch = pstr->trans[ch]; 547 ch = pstr->trans[ch];
545 pstr->mbs[char_idx] = toupper (ch); 548 pstr->mbs[char_idx] = toupper (ch);
546 } 549 }
@@ -576,7 +579,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
576{ 579{
577 Idx offset; 580 Idx offset;
578 581
579 if (BE (pstr->raw_mbs_idx <= idx, 0)) 582 if (__glibc_unlikely (pstr->raw_mbs_idx <= idx))
580 offset = idx - pstr->raw_mbs_idx; 583 offset = idx - pstr->raw_mbs_idx;
581 else 584 else
582 { 585 {
@@ -598,14 +601,14 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
598 offset = idx; 601 offset = idx;
599 } 602 }
600 603
601 if (BE (offset != 0, 1)) 604 if (__glibc_likely (offset != 0))
602 { 605 {
603 /* Should the already checked characters be kept? */ 606 /* Should the already checked characters be kept? */
604 if (BE (offset < pstr->valid_raw_len, 1)) 607 if (__glibc_likely (offset < pstr->valid_raw_len))
605 { 608 {
606 /* Yes, move them to the front of the buffer. */ 609 /* Yes, move them to the front of the buffer. */
607#ifdef RE_ENABLE_I18N 610#ifdef RE_ENABLE_I18N
608 if (BE (pstr->offsets_needed, 0)) 611 if (__glibc_unlikely (pstr->offsets_needed))
609 { 612 {
610 Idx low = 0, high = pstr->valid_len, mid; 613 Idx low = 0, high = pstr->valid_len, mid;
611 do 614 do
@@ -677,7 +680,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
677 memmove (pstr->wcs, pstr->wcs + offset, 680 memmove (pstr->wcs, pstr->wcs + offset,
678 (pstr->valid_len - offset) * sizeof (wint_t)); 681 (pstr->valid_len - offset) * sizeof (wint_t));
679#endif /* RE_ENABLE_I18N */ 682#endif /* RE_ENABLE_I18N */
680 if (BE (pstr->mbs_allocated, 0)) 683 if (__glibc_unlikely (pstr->mbs_allocated))
681 memmove (pstr->mbs, pstr->mbs + offset, 684 memmove (pstr->mbs, pstr->mbs + offset,
682 pstr->valid_len - offset); 685 pstr->valid_len - offset);
683 pstr->valid_len -= offset; 686 pstr->valid_len -= offset;
@@ -693,7 +696,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
693 /* No, skip all characters until IDX. */ 696 /* No, skip all characters until IDX. */
694 Idx prev_valid_len = pstr->valid_len; 697 Idx prev_valid_len = pstr->valid_len;
695 698
696 if (BE (pstr->offsets_needed, 0)) 699 if (__glibc_unlikely (pstr->offsets_needed))
697 { 700 {
698 pstr->len = pstr->raw_len - idx + offset; 701 pstr->len = pstr->raw_len - idx + offset;
699 pstr->stop = pstr->raw_stop - idx + offset; 702 pstr->stop = pstr->raw_stop - idx + offset;
@@ -721,7 +724,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
721#ifdef _LIBC 724#ifdef _LIBC
722 /* We know the wchar_t encoding is UCS4, so for the simple 725 /* We know the wchar_t encoding is UCS4, so for the simple
723 case, ASCII characters, skip the conversion step. */ 726 case, ASCII characters, skip the conversion step. */
724 if (isascii (*p) && BE (pstr->trans == NULL, 1)) 727 if (isascii (*p) && __glibc_likely (pstr->trans == NULL))
725 { 728 {
726 memset (&pstr->cur_state, '\0', sizeof (mbstate_t)); 729 memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
727 /* pstr->valid_len = 0; */ 730 /* pstr->valid_len = 0; */
@@ -739,7 +742,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
739 size_t mbclen; 742 size_t mbclen;
740 743
741 const unsigned char *pp = p; 744 const unsigned char *pp = p;
742 if (BE (pstr->trans != NULL, 0)) 745 if (__glibc_unlikely (pstr->trans != NULL))
743 { 746 {
744 int i = mlen < 6 ? mlen : 6; 747 int i = mlen < 6 ? mlen : 6;
745 while (--i >= 0) 748 while (--i >= 0)
@@ -769,13 +772,13 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
769 pstr->tip_context 772 pstr->tip_context
770 = re_string_context_at (pstr, prev_valid_len - 1, eflags); 773 = re_string_context_at (pstr, prev_valid_len - 1, eflags);
771 else 774 else
772 pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0) 775 pstr->tip_context = ((__glibc_unlikely (pstr->word_ops_used != 0)
773 && IS_WIDE_WORD_CHAR (wc)) 776 && IS_WIDE_WORD_CHAR (wc))
774 ? CONTEXT_WORD 777 ? CONTEXT_WORD
775 : ((IS_WIDE_NEWLINE (wc) 778 : ((IS_WIDE_NEWLINE (wc)
776 && pstr->newline_anchor) 779 && pstr->newline_anchor)
777 ? CONTEXT_NEWLINE : 0)); 780 ? CONTEXT_NEWLINE : 0));
778 if (BE (pstr->valid_len, 0)) 781 if (__glibc_unlikely (pstr->valid_len))
779 { 782 {
780 for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx) 783 for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
781 pstr->wcs[wcs_idx] = WEOF; 784 pstr->wcs[wcs_idx] = WEOF;
@@ -797,7 +800,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
797 ? CONTEXT_NEWLINE : 0)); 800 ? CONTEXT_NEWLINE : 0));
798 } 801 }
799 } 802 }
800 if (!BE (pstr->mbs_allocated, 0)) 803 if (!__glibc_unlikely (pstr->mbs_allocated))
801 pstr->mbs += offset; 804 pstr->mbs += offset;
802 } 805 }
803 pstr->raw_mbs_idx = idx; 806 pstr->raw_mbs_idx = idx;
@@ -811,7 +814,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
811 if (pstr->icase) 814 if (pstr->icase)
812 { 815 {
813 reg_errcode_t ret = build_wcs_upper_buffer (pstr); 816 reg_errcode_t ret = build_wcs_upper_buffer (pstr);
814 if (BE (ret != REG_NOERROR, 0)) 817 if (__glibc_unlikely (ret != REG_NOERROR))
815 return ret; 818 return ret;
816 } 819 }
817 else 820 else
@@ -819,7 +822,7 @@ re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
819 } 822 }
820 else 823 else
821#endif /* RE_ENABLE_I18N */ 824#endif /* RE_ENABLE_I18N */
822 if (BE (pstr->mbs_allocated, 0)) 825 if (__glibc_unlikely (pstr->mbs_allocated))
823 { 826 {
824 if (pstr->icase) 827 if (pstr->icase)
825 build_upper_buffer (pstr); 828 build_upper_buffer (pstr);
@@ -841,7 +844,7 @@ re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
841 Idx off; 844 Idx off;
842 845
843 /* Handle the common (easiest) cases first. */ 846 /* Handle the common (easiest) cases first. */
844 if (BE (!pstr->mbs_allocated, 1)) 847 if (__glibc_likely (!pstr->mbs_allocated))
845 return re_string_peek_byte (pstr, idx); 848 return re_string_peek_byte (pstr, idx);
846 849
847#ifdef RE_ENABLE_I18N 850#ifdef RE_ENABLE_I18N
@@ -873,7 +876,7 @@ re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
873static unsigned char 876static unsigned char
874re_string_fetch_byte_case (re_string_t *pstr) 877re_string_fetch_byte_case (re_string_t *pstr)
875{ 878{
876 if (BE (!pstr->mbs_allocated, 1)) 879 if (__glibc_likely (!pstr->mbs_allocated))
877 return re_string_fetch_byte (pstr); 880 return re_string_fetch_byte (pstr);
878 881
879#ifdef RE_ENABLE_I18N 882#ifdef RE_ENABLE_I18N
@@ -924,11 +927,11 @@ static unsigned int
924re_string_context_at (const re_string_t *input, Idx idx, int eflags) 927re_string_context_at (const re_string_t *input, Idx idx, int eflags)
925{ 928{
926 int c; 929 int c;
927 if (BE (idx < 0, 0)) 930 if (__glibc_unlikely (idx < 0))
928 /* In this case, we use the value stored in input->tip_context, 931 /* In this case, we use the value stored in input->tip_context,
929 since we can't know the character in input->mbs[-1] here. */ 932 since we can't know the character in input->mbs[-1] here. */
930 return input->tip_context; 933 return input->tip_context;
931 if (BE (idx == input->len, 0)) 934 if (__glibc_unlikely (idx == input->len))
932 return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF 935 return ((eflags & REG_NOTEOL) ? CONTEXT_ENDBUF
933 : CONTEXT_NEWLINE | CONTEXT_ENDBUF); 936 : CONTEXT_NEWLINE | CONTEXT_ENDBUF);
934#ifdef RE_ENABLE_I18N 937#ifdef RE_ENABLE_I18N
@@ -947,7 +950,8 @@ re_string_context_at (const re_string_t *input, Idx idx, int eflags)
947 return input->tip_context; 950 return input->tip_context;
948 } 951 }
949 wc = input->wcs[wc_idx]; 952 wc = input->wcs[wc_idx];
950 if (BE (input->word_ops_used != 0, 0) && IS_WIDE_WORD_CHAR (wc)) 953 if (__glibc_unlikely (input->word_ops_used != 0)
954 && IS_WIDE_WORD_CHAR (wc))
951 return CONTEXT_WORD; 955 return CONTEXT_WORD;
952 return (IS_WIDE_NEWLINE (wc) && input->newline_anchor 956 return (IS_WIDE_NEWLINE (wc) && input->newline_anchor
953 ? CONTEXT_NEWLINE : 0); 957 ? CONTEXT_NEWLINE : 0);
@@ -971,7 +975,8 @@ re_node_set_alloc (re_node_set *set, Idx size)
971 set->alloc = size; 975 set->alloc = size;
972 set->nelem = 0; 976 set->nelem = 0;
973 set->elems = re_malloc (Idx, size); 977 set->elems = re_malloc (Idx, size);
974 if (BE (set->elems == NULL, 0) && (MALLOC_0_IS_NONNULL || size != 0)) 978 if (__glibc_unlikely (set->elems == NULL)
979 && (MALLOC_0_IS_NONNULL || size != 0))
975 return REG_ESPACE; 980 return REG_ESPACE;
976 return REG_NOERROR; 981 return REG_NOERROR;
977} 982}
@@ -983,7 +988,7 @@ re_node_set_init_1 (re_node_set *set, Idx elem)
983 set->alloc = 1; 988 set->alloc = 1;
984 set->nelem = 1; 989 set->nelem = 1;
985 set->elems = re_malloc (Idx, 1); 990 set->elems = re_malloc (Idx, 1);
986 if (BE (set->elems == NULL, 0)) 991 if (__glibc_unlikely (set->elems == NULL))
987 { 992 {
988 set->alloc = set->nelem = 0; 993 set->alloc = set->nelem = 0;
989 return REG_ESPACE; 994 return REG_ESPACE;
@@ -998,7 +1003,7 @@ re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
998{ 1003{
999 set->alloc = 2; 1004 set->alloc = 2;
1000 set->elems = re_malloc (Idx, 2); 1005 set->elems = re_malloc (Idx, 2);
1001 if (BE (set->elems == NULL, 0)) 1006 if (__glibc_unlikely (set->elems == NULL))
1002 return REG_ESPACE; 1007 return REG_ESPACE;
1003 if (elem1 == elem2) 1008 if (elem1 == elem2)
1004 { 1009 {
@@ -1031,7 +1036,7 @@ re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
1031 { 1036 {
1032 dest->alloc = dest->nelem; 1037 dest->alloc = dest->nelem;
1033 dest->elems = re_malloc (Idx, dest->alloc); 1038 dest->elems = re_malloc (Idx, dest->alloc);
1034 if (BE (dest->elems == NULL, 0)) 1039 if (__glibc_unlikely (dest->elems == NULL))
1035 { 1040 {
1036 dest->alloc = dest->nelem = 0; 1041 dest->alloc = dest->nelem = 0;
1037 return REG_ESPACE; 1042 return REG_ESPACE;
@@ -1062,7 +1067,7 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
1062 { 1067 {
1063 Idx new_alloc = src1->nelem + src2->nelem + dest->alloc; 1068 Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
1064 Idx *new_elems = re_realloc (dest->elems, Idx, new_alloc); 1069 Idx *new_elems = re_realloc (dest->elems, Idx, new_alloc);
1065 if (BE (new_elems == NULL, 0)) 1070 if (__glibc_unlikely (new_elems == NULL))
1066 return REG_ESPACE; 1071 return REG_ESPACE;
1067 dest->elems = new_elems; 1072 dest->elems = new_elems;
1068 dest->alloc = new_alloc; 1073 dest->alloc = new_alloc;
@@ -1148,7 +1153,7 @@ re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
1148 { 1153 {
1149 dest->alloc = src1->nelem + src2->nelem; 1154 dest->alloc = src1->nelem + src2->nelem;
1150 dest->elems = re_malloc (Idx, dest->alloc); 1155 dest->elems = re_malloc (Idx, dest->alloc);
1151 if (BE (dest->elems == NULL, 0)) 1156 if (__glibc_unlikely (dest->elems == NULL))
1152 return REG_ESPACE; 1157 return REG_ESPACE;
1153 } 1158 }
1154 else 1159 else
@@ -1202,13 +1207,13 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
1202 { 1207 {
1203 Idx new_alloc = 2 * (src->nelem + dest->alloc); 1208 Idx new_alloc = 2 * (src->nelem + dest->alloc);
1204 Idx *new_buffer = re_realloc (dest->elems, Idx, new_alloc); 1209 Idx *new_buffer = re_realloc (dest->elems, Idx, new_alloc);
1205 if (BE (new_buffer == NULL, 0)) 1210 if (__glibc_unlikely (new_buffer == NULL))
1206 return REG_ESPACE; 1211 return REG_ESPACE;
1207 dest->elems = new_buffer; 1212 dest->elems = new_buffer;
1208 dest->alloc = new_alloc; 1213 dest->alloc = new_alloc;
1209 } 1214 }
1210 1215
1211 if (BE (dest->nelem == 0, 0)) 1216 if (__glibc_unlikely (dest->nelem == 0))
1212 { 1217 {
1213 dest->nelem = src->nelem; 1218 dest->nelem = src->nelem;
1214 memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx)); 1219 memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
@@ -1281,9 +1286,9 @@ re_node_set_insert (re_node_set *set, Idx elem)
1281 Idx idx; 1286 Idx idx;
1282 /* In case the set is empty. */ 1287 /* In case the set is empty. */
1283 if (set->alloc == 0) 1288 if (set->alloc == 0)
1284 return BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1); 1289 return __glibc_likely (re_node_set_init_1 (set, elem) == REG_NOERROR);
1285 1290
1286 if (BE (set->nelem, 0) == 0) 1291 if (__glibc_unlikely (set->nelem) == 0)
1287 { 1292 {
1288 /* We already guaranteed above that set->alloc != 0. */ 1293 /* We already guaranteed above that set->alloc != 0. */
1289 set->elems[0] = elem; 1294 set->elems[0] = elem;
@@ -1297,7 +1302,7 @@ re_node_set_insert (re_node_set *set, Idx elem)
1297 Idx *new_elems; 1302 Idx *new_elems;
1298 set->alloc = set->alloc * 2; 1303 set->alloc = set->alloc * 2;
1299 new_elems = re_realloc (set->elems, Idx, set->alloc); 1304 new_elems = re_realloc (set->elems, Idx, set->alloc);
1300 if (BE (new_elems == NULL, 0)) 1305 if (__glibc_unlikely (new_elems == NULL))
1301 return false; 1306 return false;
1302 set->elems = new_elems; 1307 set->elems = new_elems;
1303 } 1308 }
@@ -1336,7 +1341,7 @@ re_node_set_insert_last (re_node_set *set, Idx elem)
1336 Idx *new_elems; 1341 Idx *new_elems;
1337 set->alloc = (set->alloc + 1) * 2; 1342 set->alloc = (set->alloc + 1) * 2;
1338 new_elems = re_realloc (set->elems, Idx, set->alloc); 1343 new_elems = re_realloc (set->elems, Idx, set->alloc);
1339 if (BE (new_elems == NULL, 0)) 1344 if (__glibc_unlikely (new_elems == NULL))
1340 return false; 1345 return false;
1341 set->elems = new_elems; 1346 set->elems = new_elems;
1342 } 1347 }
@@ -1403,7 +1408,7 @@ re_node_set_remove_at (re_node_set *set, Idx idx)
1403static Idx 1408static Idx
1404re_dfa_add_node (re_dfa_t *dfa, re_token_t token) 1409re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
1405{ 1410{
1406 if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0)) 1411 if (__glibc_unlikely (dfa->nodes_len >= dfa->nodes_alloc))
1407 { 1412 {
1408 size_t new_nodes_alloc = dfa->nodes_alloc * 2; 1413 size_t new_nodes_alloc = dfa->nodes_alloc * 2;
1409 Idx *new_nexts, *new_indices; 1414 Idx *new_nexts, *new_indices;
@@ -1414,19 +1419,20 @@ re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
1414 const size_t max_object_size = MAX (sizeof (re_token_t), 1419 const size_t max_object_size = MAX (sizeof (re_token_t),
1415 MAX (sizeof (re_node_set), 1420 MAX (sizeof (re_node_set),
1416 sizeof (Idx))); 1421 sizeof (Idx)));
1417 if (BE (MIN (IDX_MAX, SIZE_MAX / max_object_size) < new_nodes_alloc, 0)) 1422 if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size)
1423 < new_nodes_alloc))
1418 return -1; 1424 return -1;
1419 1425
1420 new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc); 1426 new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
1421 if (BE (new_nodes == NULL, 0)) 1427 if (__glibc_unlikely (new_nodes == NULL))
1422 return -1; 1428 return -1;
1423 dfa->nodes = new_nodes; 1429 dfa->nodes = new_nodes;
1424 new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc); 1430 new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
1425 new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc); 1431 new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
1426 new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc); 1432 new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
1427 new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc); 1433 new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
1428 if (BE (new_nexts == NULL || new_indices == NULL 1434 if (__glibc_unlikely (new_nexts == NULL || new_indices == NULL
1429 || new_edests == NULL || new_eclosures == NULL, 0)) 1435 || new_edests == NULL || new_eclosures == NULL))
1430 { 1436 {
1431 re_free (new_nexts); 1437 re_free (new_nexts);
1432 re_free (new_indices); 1438 re_free (new_indices);
@@ -1485,7 +1491,7 @@ re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
1485 /* Suppress bogus uninitialized-variable warnings. */ 1491 /* Suppress bogus uninitialized-variable warnings. */
1486 *err = REG_NOERROR; 1492 *err = REG_NOERROR;
1487#endif 1493#endif
1488 if (BE (nodes->nelem == 0, 0)) 1494 if (__glibc_unlikely (nodes->nelem == 0))
1489 { 1495 {
1490 *err = REG_NOERROR; 1496 *err = REG_NOERROR;
1491 return NULL; 1497 return NULL;
@@ -1504,7 +1510,7 @@ re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
1504 1510
1505 /* There are no appropriate state in the dfa, create the new one. */ 1511 /* There are no appropriate state in the dfa, create the new one. */
1506 new_state = create_ci_newstate (dfa, nodes, hash); 1512 new_state = create_ci_newstate (dfa, nodes, hash);
1507 if (BE (new_state == NULL, 0)) 1513 if (__glibc_unlikely (new_state == NULL))
1508 *err = REG_ESPACE; 1514 *err = REG_ESPACE;
1509 1515
1510 return new_state; 1516 return new_state;
@@ -1551,7 +1557,7 @@ re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
1551 } 1557 }
1552 /* There are no appropriate state in 'dfa', create the new one. */ 1558 /* There are no appropriate state in 'dfa', create the new one. */
1553 new_state = create_cd_newstate (dfa, nodes, context, hash); 1559 new_state = create_cd_newstate (dfa, nodes, context, hash);
1554 if (BE (new_state == NULL, 0)) 1560 if (__glibc_unlikely (new_state == NULL))
1555 *err = REG_ESPACE; 1561 *err = REG_ESPACE;
1556 1562
1557 return new_state; 1563 return new_state;
@@ -1572,7 +1578,7 @@ register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
1572 1578
1573 newstate->hash = hash; 1579 newstate->hash = hash;
1574 err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem); 1580 err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
1575 if (BE (err != REG_NOERROR, 0)) 1581 if (__glibc_unlikely (err != REG_NOERROR))
1576 return REG_ESPACE; 1582 return REG_ESPACE;
1577 for (i = 0; i < newstate->nodes.nelem; i++) 1583 for (i = 0; i < newstate->nodes.nelem; i++)
1578 { 1584 {
@@ -1583,12 +1589,12 @@ register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
1583 } 1589 }
1584 1590
1585 spot = dfa->state_table + (hash & dfa->state_hash_mask); 1591 spot = dfa->state_table + (hash & dfa->state_hash_mask);
1586 if (BE (spot->alloc <= spot->num, 0)) 1592 if (__glibc_unlikely (spot->alloc <= spot->num))
1587 { 1593 {
1588 Idx new_alloc = 2 * spot->num + 2; 1594 Idx new_alloc = 2 * spot->num + 2;
1589 re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *, 1595 re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
1590 new_alloc); 1596 new_alloc);
1591 if (BE (new_array == NULL, 0)) 1597 if (__glibc_unlikely (new_array == NULL))
1592 return REG_ESPACE; 1598 return REG_ESPACE;
1593 spot->array = new_array; 1599 spot->array = new_array;
1594 spot->alloc = new_alloc; 1600 spot->alloc = new_alloc;
@@ -1626,10 +1632,10 @@ create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1626 re_dfastate_t *newstate; 1632 re_dfastate_t *newstate;
1627 1633
1628 newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); 1634 newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1629 if (BE (newstate == NULL, 0)) 1635 if (__glibc_unlikely (newstate == NULL))
1630 return NULL; 1636 return NULL;
1631 err = re_node_set_init_copy (&newstate->nodes, nodes); 1637 err = re_node_set_init_copy (&newstate->nodes, nodes);
1632 if (BE (err != REG_NOERROR, 0)) 1638 if (__glibc_unlikely (err != REG_NOERROR))
1633 { 1639 {
1634 re_free (newstate); 1640 re_free (newstate);
1635 return NULL; 1641 return NULL;
@@ -1655,7 +1661,7 @@ create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1655 newstate->has_constraint = 1; 1661 newstate->has_constraint = 1;
1656 } 1662 }
1657 err = register_state (dfa, newstate, hash); 1663 err = register_state (dfa, newstate, hash);
1658 if (BE (err != REG_NOERROR, 0)) 1664 if (__glibc_unlikely (err != REG_NOERROR))
1659 { 1665 {
1660 free_state (newstate); 1666 free_state (newstate);
1661 newstate = NULL; 1667 newstate = NULL;
@@ -1676,10 +1682,10 @@ create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1676 re_dfastate_t *newstate; 1682 re_dfastate_t *newstate;
1677 1683
1678 newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); 1684 newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
1679 if (BE (newstate == NULL, 0)) 1685 if (__glibc_unlikely (newstate == NULL))
1680 return NULL; 1686 return NULL;
1681 err = re_node_set_init_copy (&newstate->nodes, nodes); 1687 err = re_node_set_init_copy (&newstate->nodes, nodes);
1682 if (BE (err != REG_NOERROR, 0)) 1688 if (__glibc_unlikely (err != REG_NOERROR))
1683 { 1689 {
1684 re_free (newstate); 1690 re_free (newstate);
1685 return NULL; 1691 return NULL;
@@ -1711,7 +1717,7 @@ create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1711 if (newstate->entrance_nodes == &newstate->nodes) 1717 if (newstate->entrance_nodes == &newstate->nodes)
1712 { 1718 {
1713 newstate->entrance_nodes = re_malloc (re_node_set, 1); 1719 newstate->entrance_nodes = re_malloc (re_node_set, 1);
1714 if (BE (newstate->entrance_nodes == NULL, 0)) 1720 if (__glibc_unlikely (newstate->entrance_nodes == NULL))
1715 { 1721 {
1716 free_state (newstate); 1722 free_state (newstate);
1717 return NULL; 1723 return NULL;
@@ -1731,7 +1737,7 @@ create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1731 } 1737 }
1732 } 1738 }
1733 err = register_state (dfa, newstate, hash); 1739 err = register_state (dfa, newstate, hash);
1734 if (BE (err != REG_NOERROR, 0)) 1740 if (__glibc_unlikely (err != REG_NOERROR))
1735 { 1741 {
1736 free_state (newstate); 1742 free_state (newstate);
1737 newstate = NULL; 1743 newstate = NULL;