Commit b7a98993 authored by Paul Eggert's avatar Paul Eggert

Fix some integer issues in regex-emacs

Also, remove some duplicate comments related to thread.h.
* src/regex-emacs.h (struct re_registers):
* src/regex-emacs.c (SIGN_EXTEND_CHAR): Remove.
(TALLOC, RETALLOC): Remove.  All uses replaced by usual
allocators, which check for integer overflow.
(extract_number): Redo without using ‘unsigned’.
(CHARSET_RANGE_TABLE_EXISTS_P): Clearly return a boolean.
(print_fastmap, print_partial_compiled_pattern, CHECK_INFINITE_LOOP)
(regex_compile, analyze_first, bcmp_translate, mutually_exclusive_p)
(re_match_2_internal):
Use bool for booleans.
(print_fastmap, regex_compile, execute_charset):
Prefer int to unsigned where either will do.
(print_double_string): Prefer ptrdiff_t to ssize_t, since the
latter can in theory be narrower than the former.  Use fwrite
instead of repeated putchar.
(emacs_re_max_failures, fail_stack_type, compile_stack_type)
(re_wctype_parse, regex_compile, re_search, re_search_2)
(re_match_2, re_match_2_internal, re_compile_pattern):
Prefer ptrdiff_t to size_t where either will do.
(union fail_stack_elt, PUSH_FAILURE_REG, POP_FAILURE_REG_OR_COUNT):
Make the integer an intptr_t, not long.
(GET_BUFFER_SPACE, EXTEND_BUFFER, regex_compile):
Use xpalloc to simplify allocation.
(regex_compile): Check for integer overflow when calculating
register numbers.
* src/regex-emacs.c (re_set_registers, re_match_2_internal):
* src/regex-emacs.h (struct re_registers, struct re_pattern_buffer):
* src/search.c (Freplace_match):
Prefer ptrdiff_t to unsigned where either will do.
* src/regex-emacs.h (struct re_pattern_buffer):
Prefer bool_bf to unsigned where either will do.
parent 176ababa
Pipeline #1068 failed with stage
in 49 minutes and 6 seconds
......@@ -40,10 +40,6 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
# define CACHEABLE /* empty */
#endif
/* Chain of condition and catch handlers currently in effect. */
/* struct handler *handlerlist; */
/* Non-nil means record all fset's and provide's, to be undone
if the file being autoloaded is not fully loaded.
They are recorded by being consed onto the front of Vautoload_queue:
......@@ -248,8 +244,6 @@ init_eval_once_for_pdumper (void)
specpdl = specpdl_ptr = pdlvec + 1;
}
/* static struct handler handlerlist_sentinel; */
void
init_eval (void)
{
......
......@@ -3233,11 +3233,6 @@ union specbinding
} bt;
};
/* These 3 are defined as macros in thread.h. */
/* extern union specbinding *specpdl; */
/* extern union specbinding *specpdl_ptr; */
/* extern ptrdiff_t specpdl_size; */
INLINE ptrdiff_t
SPECPDL_INDEX (void)
{
......
This diff is collapsed.
......@@ -26,7 +26,7 @@
uses struct re_registers. */
struct re_registers
{
unsigned num_regs;
ptrdiff_t num_regs;
ptrdiff_t *start;
ptrdiff_t *end;
};
......@@ -50,7 +50,7 @@ struct re_registers
extern Lisp_Object re_match_object;
/* Roughly the maximum number of failure points on the stack. */
extern size_t emacs_re_max_failures;
extern ptrdiff_t emacs_re_max_failures;
/* Amount of memory that we can safely stack allocate. */
extern ptrdiff_t emacs_re_safe_alloca;
......@@ -69,10 +69,10 @@ struct re_pattern_buffer
unsigned char *buffer;
/* Number of bytes to which 'buffer' points. */
size_t allocated;
ptrdiff_t allocated;
/* Number of bytes actually used in 'buffer'. */
size_t used;
ptrdiff_t used;
/* Charset of unibyte characters at compiling time. */
int charset_unibyte;
......@@ -89,13 +89,13 @@ struct re_pattern_buffer
Lisp_Object translate;
/* Number of subexpressions found by the compiler. */
size_t re_nsub;
ptrdiff_t re_nsub;
/* True if and only if this pattern can match the empty string.
Well, in truth it's used only in 're_search_2', to see
whether or not we should use the fastmap, so we don't set
this absolutely perfectly; see 're_compile_fastmap'. */
unsigned can_be_null : 1;
bool_bf can_be_null : 1;
/* If REGS_UNALLOCATED, allocate space in the 'regs' structure
for 'max (RE_NREGS, re_nsub + 1)' groups.
......@@ -105,19 +105,19 @@ struct re_pattern_buffer
/* Set to false when 'regex_compile' compiles a pattern; set to true
by 're_compile_fastmap' if it updates the fastmap. */
unsigned fastmap_accurate : 1;
bool_bf fastmap_accurate : 1;
/* If true, the compilation of the pattern had to look up the syntax table,
so the compiled pattern is valid for the current syntax table only. */
unsigned used_syntax : 1;
bool_bf used_syntax : 1;
/* If true, multi-byte form in the regexp pattern should be
recognized as a multibyte character. */
unsigned multibyte : 1;
bool_bf multibyte : 1;
/* If true, multi-byte form in the target of match should be
recognized as a multibyte character. */
unsigned target_multibyte : 1;
bool_bf target_multibyte : 1;
};
/* Declarations for routines. */
......@@ -125,7 +125,7 @@ struct re_pattern_buffer
/* Compile the regular expression PATTERN, with length LENGTH
and syntax given by the global 're_syntax_options', into the buffer
BUFFER. Return NULL if successful, and an error string if not. */
extern const char *re_compile_pattern (const char *pattern, size_t length,
extern const char *re_compile_pattern (const char *pattern, ptrdiff_t length,
bool posix_backtracking,
const char *whitespace_regexp,
struct re_pattern_buffer *buffer);
......@@ -137,7 +137,7 @@ extern const char *re_compile_pattern (const char *pattern, size_t length,
match, or -2 for an internal error. Also return register
information in REGS (if REGS is non-null). */
extern ptrdiff_t re_search (struct re_pattern_buffer *buffer,
const char *string, size_t length,
const char *string, ptrdiff_t length,
ptrdiff_t start, ptrdiff_t range,
struct re_registers *regs);
......@@ -145,8 +145,8 @@ extern ptrdiff_t re_search (struct re_pattern_buffer *buffer,
/* Like 're_search', but search in the concatenation of STRING1 and
STRING2. Also, stop searching at index START + STOP. */
extern ptrdiff_t re_search_2 (struct re_pattern_buffer *buffer,
const char *string1, size_t length1,
const char *string2, size_t length2,
const char *string1, ptrdiff_t length1,
const char *string2, ptrdiff_t length2,
ptrdiff_t start, ptrdiff_t range,
struct re_registers *regs,
ptrdiff_t stop);
......@@ -155,8 +155,8 @@ extern ptrdiff_t re_search_2 (struct re_pattern_buffer *buffer,
/* Like 're_search_2', but return how many characters in STRING the regexp
in BUFFER matched, starting at position START. */
extern ptrdiff_t re_match_2 (struct re_pattern_buffer *buffer,
const char *string1, size_t length1,
const char *string2, size_t length2,
const char *string1, ptrdiff_t length1,
const char *string2, ptrdiff_t length2,
ptrdiff_t start, struct re_registers *regs,
ptrdiff_t stop);
......@@ -175,7 +175,7 @@ extern ptrdiff_t re_match_2 (struct re_pattern_buffer *buffer,
freeing the old data. */
extern void re_set_registers (struct re_pattern_buffer *buffer,
struct re_registers *regs,
unsigned num_regs,
ptrdiff_t num_regs,
ptrdiff_t *starts, ptrdiff_t *ends);
/* Character classes. */
......@@ -192,6 +192,6 @@ typedef enum { RECC_ERROR = 0,
extern bool re_iswctype (int ch, re_wctype_t cc);
extern re_wctype_t re_wctype_parse (const unsigned char **strp,
unsigned limit);
ptrdiff_t limit);
#endif /* EMACS_REGEX_H */
......@@ -59,31 +59,6 @@ static struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
/* The head of the linked list; points to the most recently used buffer. */
static struct regexp_cache *searchbuf_head;
/* Every call to re_search, etc., must pass &search_regs as the regs
argument unless you can show it is unnecessary (i.e., if re_search
is certainly going to be called again before region-around-match
can be called).
Since the registers are now dynamically allocated, we need to make
sure not to refer to the Nth register before checking that it has
been allocated by checking search_regs.num_regs.
The regex code keeps track of whether it has allocated the search
buffer using bits in the re_pattern_buffer. This means that whenever
you compile a new pattern, it completely forgets whether it has
allocated any registers, and will allocate new registers the next
time you call a searching or matching function. Therefore, we need
to call re_set_registers after compiling a new pattern or after
setting the match registers, so that the regex functions will be
able to free or re-allocate it properly. */
/* static struct re_registers search_regs; */
/* The buffer in which the last search was performed, or
Qt if the last search was done in a string;
Qnil if no searching has been done yet. */
/* static Lisp_Object last_thing_searched; */
static void set_search_regs (ptrdiff_t, ptrdiff_t);
static void save_search_regs (void);
static EMACS_INT simple_search (EMACS_INT, unsigned char *, ptrdiff_t,
......@@ -2763,7 +2738,7 @@ since only regular expressions have distinguished subexpressions. */)
error out since otherwise this will result in confusing bugs. */
ptrdiff_t sub_start = search_regs.start[sub];
ptrdiff_t sub_end = search_regs.end[sub];
unsigned num_regs = search_regs.num_regs;
ptrdiff_t num_regs = search_regs.num_regs;
newpoint = search_regs.start[sub] + SCHARS (newtext);
/* Replace the old text with the new in the cleanest possible way. */
......@@ -3079,12 +3054,6 @@ If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */)
return Qnil;
}
/* If true the match data have been saved in saved_search_regs
during the execution of a sentinel or filter. */
/* static bool search_regs_saved; */
/* static struct re_registers saved_search_regs; */
/* static Lisp_Object saved_last_thing_searched; */
/* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
if asynchronous code (filter or sentinel) is running. */
static void
......
......@@ -111,8 +111,8 @@ struct thread_state
struct buffer *m_current_buffer;
#define current_buffer (current_thread->m_current_buffer)
/* Every call to re_match_2, etc., must pass &search_regs as the regs
argument unless you can show it is unnecessary (i.e., if re_match_2
/* Every call to re_search, etc., must pass &search_regs as the regs
argument unless you can show it is unnecessary (i.e., if re_search
is certainly going to be called again before region-around-match
can be called).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment