summaryrefslogtreecommitdiff
path: root/sed/utils.h
Commit message (Collapse)AuthorAgeFilesLines
* maint: update copyright datesJim Meyering2023-01-011-1/+1
|
* sed: improve integer overflow checkingPaul Eggert2022-12-191-10/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fix some some longstanding but unlikely integer overflows. Internally, 'sed' now more often prefers signed integer arithmetic, which can be checked automatically via 'gcc -fsanitize=undefined'. * basicdefs.h (countT): Remove. All uses replaced with a more-specific signed type, e.g., idx_t. Similarly, change uses of types like size_t to signed types like idx_t when appropriate. (REALLOC): Remove; no longer used. We now use xpalloc because that detects integer overflow in size calculations. Also, we no longer use XCALLOC since the code never relies on the storage being zero, and leaving it uninitialized is more likely to catch errors when debugging implementations are used. We use XNMALLOC instead, or xpalloc. * bootstrap.conf (gnulib_modules): Add stdckdint, strtoimax. * lib/.gitignore, m4/.gitignore: Update for new Gnulib modules. * sed/compile.c: Include stdckdint.h. (VECTOR_ALLOC_INCREMENT): Remove; no longer used. (in_integer): Return maximal value if integer overflow. All callers changed to expect this. (next_cmd_entry): Use xpalloc instead of reallocating by hand, which might suffer integer overflow. (normalize_text): Don’t rely on system-defined conversion of out-of-range size_t to int. (next_cmd_entry): Arg is now pointer, not pointer-to-pointer. All uses changed. * sed/debug.c (debug_print_function): Don’t attempt to fwrite a null pointer with a zero size. * sed/execute.c: Include <stdckdint.h>, "minmax.h". (resize_line): LEN arg is now increment, not total length, to avoid overflow when calculating total length. All uses changed. Do not assume lb->alloc * 2 cannot overflow. (resize_line, line_copy): Use xpalloc instead of doing realloc by hand, which might suffer integer overflow. (str_append_modified): Do not add n to to->length until after it's known this cannot overflow. (read_file_line): Don’t assume ssize_t fits in long. (get_backup_file_name): Don’t assume string length fits in int. Do not assume PTR-1+1 works; behavior is undefined if PTR is at buffer start. Check for integer overflow in buffer size calculation. (read_pattern_space): Check for line number overflow. (match_address_p): Check for address overflow. (debug_print_line): Omit unnecessary test for in->active being null. (execute_program): Check for Q overflow. * sed/regexp.c: Include <stdckdint.h>. (match_regex): Don’t assume TYPE_MAXIMUM (regoff_t) == INT_MAX. * sed/sed.c: Include inttypes.h, for strtoimax. (main): Use strtoimax, not atoi. * sed/utils.c (init_buffer): Use xmalloc and xpalloc instead of guessing sizes ourselves, and unnecessarily initializing. (resize_buffer): Remove; all callers changed to use xpalloc. (free_buffer): Don’t call free (NULL), since we already test whether the pointer is null.
* sed: fix temp file cleanupPaul Eggert2022-07-021-1/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, the code would sometimes use FP after calling fclose (FP), which has undefined behavior in C. Problem found with --enable-gcc-warnings and GCC 12. * sed/execute.c (open_next_file): Do not register here, as it’s too late and this can cause the file to not be cleaned up. * sed/sed.c (G_file_to_unlink, register_cleanup_file, cancel_cleanup): Move from here to utils.c. (cleanup): Call remove_cleanup_file instead of doing it by hand. * sed/utils.c (struct open_file): Remove member temp (which was always false) and fclose_failed (which was not enough to prevent calling fclose with a bad pointer). All uses changed. (register_open_file): Do not access p->fp after it’s fclosed, as that has undefined behavior in C. Use xmalloc instead of xcalloc, since we initialize all members. (G_file_to_unlink, register_cleanup_file, cancel_cleanup): Move from utils.c to here. (remove_cleanup_file): New function. (ck_mkstemp): Fix a screwup when mkostemp succeeded but set_binary_mode or fdopen failed: we might misuse a null pointer, or forget to clean up the newly-created temp file. (ck_getdelim): Rename local to avoid confusion with global. (mark_as_fclose_failed): Remove. All uses removed. (ck_fclose): Remove entry from open_files before attempting to fclose it, so that panicking doesn’t try to fclose it again. (do_ck_fclose): New arg NAME so that there’s no need to call mark_as_fclose_failed, which inspected FP after fclosing it, which is undefined behavior. (ck_rename): Omit arg UNLINK_IF_FAIL. All callers changed. The cleanup handler removes this file now, as needed.
* maint: make update-copyrightJim Meyering2022-01-011-1/+1
|
* maint: update all copyright year number rangesAssaf Gordon2021-01-051-2/+3
| | | | | | | | | | | | | Run "make update-copyright" and then... * gnulib: Update to latest with copyright year adjusted. * bootstrap.conf (gnulib_modules): Remove getopt module, depracated in https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=0abc38bd2a1398f0 * po/POTFILES.in: Remove getopt.c * sed/utils.h (panic): Rename _GL_ATTRIBUTE_FORMAT_PRINTF to _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD following gnulib's change: https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=387d654cabd7bc15 * tests/init.sh: Sync with gnulib to pick up copyright year. * bootstrap: Likewise.
* maint: update all copyright year number rangesJim Meyering2020-01-011-1/+1
| | | | | | | Run "make update-copyright" and then... * gnulib: Update to latest with copyright year adjusted. * tests/init.sh: Sync with gnulib to pick up copyright year. * bootstrap: Likewise.
* maint: update copyright dates for 2019Assaf Gordon2019-01-011-1/+1
| | | | * all files: Run "make update-copyright".
* maint: replace street address with URL in license textAssaf Gordon2018-08-231-2/+1
| | | | | | | * basicdefs.h, sed/compile.c, sed/execute.c, sed/mbcs.c, sed/regexp.c, sed/sed.c, sed/sed.h, sed/utils.c, sed/utils.h, testsuite/get-mb-cur-max.c, testsuite/test-mbrtowc.c (GPL License notice): Replace FSF's office address with 'https://www.gnu.org/licenses/'.
* sed: replace myname with gnulib's prognameAssaf Gordon2018-08-031-2/+0
| | | | | | | | | * compile.c (bad_prog): Use program_name instead of myname. * execute.c (open_next_file): Same. * utils.c (panic): Same. (myname): Remove variable. * sed.c (usage): Use program_name instead of myname. (main): Call set_program_name once, don't set variables directly.
* sed: replace ck_strdup with gnulib's xstrdupAssaf Gordon2018-08-031-1/+0
| | | | | | * sed/utils.{c,h} (ck_strdup): Remove. * sed/compile.c, sed/execute.c, sed/sed.c: Replace ck_strdup with xstrdup.
* sed: replace ck_memdup with gnulib's xmemdupAssaf Gordon2018-08-031-2/+0
| | | | | | * bootstrap.conf: Add gnulib's xalloc module. * basicdefs.h (MEMDUP): Remove. * sed/utils.{c,h} (ck_memdup): Remove.
* maint: update gnulib and copyright dates for 2018Assaf Gordon2018-01-021-1/+1
| | | | | * gnulib: Update to latest. * all files: Run "make update-copyright".
* maint: update gnulib and copyright dates for 2017Jim Meyering2017-01-011-1/+1
| | | | | * gnulib: Update to latest. * all files: Run "make update-copyright".
* maint: fix -Wformat-exposed errors in uses of "panic"Jim Meyering2016-11-271-1/+1
| | | | | | | | | * sed/utils.h (panic): Add __printf__ attribute, so that compilers can diagnose format/arg-type errors. This exposed the following: * sed/utils.c (ck_fwrite): Don't try to print size_t via %u. Instead, use %llu and a cast to unsigned long long. * sed/mbcs.c (is_mb_char): Cast an "int" to unsigned int, to avoid mismatch with %x.
* sed: standardize exit code valuesAssaf Gordon2016-11-041-0/+8
| | | | | | | | | | | | | | Use exit-code constants, and document them: 1=syntax error, 2=bad input files, 4=I/O error or panic. Add "Exit Status" section in manual. * sed/utils.h: Define new constants. * sed/utils.c, sed/compile.c, sed/execute.c, sed/sed.c: Use constants instead of hard-coded values. * doc/sed-in.texi: Add "Exit Status" section. * doc/sed.texi: Automatically re-generated.
* maint: update copyright year, bootstrap, init.shJim Meyering2016-01-011-2/+1
| | | | | | | | Run "make update-copyright" and then... * gnulib: Update to latest. * tests/init.sh: Update from gnulib. * bootstrap: Likewise.
* maint: remove dead codeJim Meyering2015-07-241-1/+1
| | | | | | | | | | * sed/utils.c (ck_mkstemp): Remove $TMP/$TMPDIR-envvar-using code that would have been run only if this function's second parameter were to be NULL, but that could never happen, given the current and sole caller. Also, use mode_t as the type for the saved umask value, not "int". Also, move each declaration down to point of definition. * sed/utils.h (ck_mkstemp): Mark each of the four parameters as nonnull.
* maint: enable sc_prohibit_long_linesJim Meyering2015-01-011-1/+2
| | | | | | | | | | | | | | | | | * cfg.mk (local-checks-to-skip): Remove the overall exemption, but continue to exempt some files in testsuite/. * configure.ac: Split a long line. * doc/sed-in.texi: Split long lines. * doc/sed.texi: Likewise. * sed/compile.c: Likewise. * sed/execute.c: Split long lines. (execute_program): Reorder "#ifdef HAVE_POPEN" and "if (pipe_fp != NULL)" if/else blocks to make the code clearer and (incidentally) the lines shorter, due to a decrease in nesting level. * sed/sed.c: Split long lines. * sed/utils.c: Likewise. * sed/utils.h: Likewise.
* maint: update copyright year ranges to include 2015; update gnulibJim Meyering2015-01-011-1/+1
|
* maint: avoid more gcc warnings: apply _GL_ATTRIBUTE_PUREJim Meyering2014-11-301-2/+2
| | | | | | | * sed/utils.h (get_buffer, size_buffer): Make each functions sole parameter "const" and declare the function itself with the "pure" attribute. * sed/utils.c (get_buffer, size_buffer): Adjust definitions to match.
* maint: add _Noreturn attribute to two functionsJim Meyering2014-11-301-1/+1
| | | | | | This avoids two gcc may-be-noreturn warnings: * sed/sed.h (bad_prog): Add _Noreturn attribute. * sed/utils.h (panic): Likewise.
* maint: enable sc_prohibit_tab_based_indentation syntax checkJim Meyering2014-11-301-1/+1
| | | | | | | | | | | | | | | | | | * cfg.mk (local-checks-to-skip): Remove its exemption. Exempt all files under testsuite/, for now, and a few others. * .gitmodules: Change each leading TAB to 8 spaces spaces. * BUGS: Likewise. * NEWS: Likewise. * configure.ac: Likewise. * sed/compile.c: Likewise. * sed/execute.c: Likewise. * sed/fmt.c: Likewise. * sed/regexp.c: Likewise. * sed/sed.c: Likewise. * sed/sed.h: Likewise. * sed/utils.c: Likewise. * sed/utils.h: Likewise. * COPYING: Update from coreutils.
* maint: update copyright dates; use gnulib's update-copyright moduleJim Meyering2014-11-301-2/+2
| | | | | | * bootstrap.conf (gnulib_modules): Add update-copyright. Then, run "make update-copyright". * Makefile.am: Add copyright header.
* switch to the wonderful world of function prototypesPaolo Bonzini2012-03-161-26/+26
| | | | | | | | | | | | | 2012-03-16 Paolo Bonzini <bonzini@gnu.org> * basicdefs.h (P_): Remove. * sed/compile.c: Adjust. * sed/execute.c: Likewise. * sed/fmt.c: Likewise. * sed/sed.c: Likewise. * sed/sed.h: Likewise. * sed/utils.c: Likewise. * sed/utils.h: Likewise.
* remove ck_freePaolo Bonzini2012-03-161-1/+0
| | | | | | | | | | | 2012-03-16 Paolo Bonzini <bonzini@gnu.org> * basicdefs.h (FREE): Remove. * sed/compile.c: Adjust. * sed/execute.c: Adjust. * sed/regexp.c: Adjust. * sed/utils.c: Adjust, remove ck_free. * sed/utils.h: Adjust, remove ck_free.
* rip out old macrosPaolo Bonzini2012-03-161-7/+7
| | | | | | | | | | | 2012-03-16 Paolo Bonzini <bonzini@gnu.org> * basicdefs.h (CAST, VCAST, VOID, MEMCPY, MEMMOVE): Rip out. * sed/compile.c: Adjust. * sed/execute.c: Adjust. * sed/regexp.c: Adjust. * sed/utils.c: Adjust. * sed/utils.h: Adjust.
* add -z/--null-dataPaolo Bonzini2012-02-051-1/+1
| | | | | | | | | | | | | | | 2012-02-05 Paolo Bonzini <bonzini@gnu.org> Jim Hill <gjthill@gmail.com> * autoboot.conf: Change getline to getdelim. * doc/sed-in.texi: Document -z/--null-data. * doc/sed.texi: Regenerate. * sed/execute.c: Change '\n' to buffer_delimiter. * sed/sed.c: Add support for -z/--null-data. * sed/sed.h: Add buffer_delimiter. * sed/utils.c: Change ck_getline to ck_getdelim. * sed/utils.h: Change ck_getline to ck_getdelim. * NEWS: Document new option.
* use "w" or "wb" modes for sed -i, s///w, `w' and `W' depending on --binaryPaolo Bonzini2010-08-171-1/+2
| | | | | | | | | | | | | 2010-07-18 Paolo Bonzini <bonzini@gnu.org> * sed.c (write_mode): New. (main): Initialize it to "wb" if --binary is passed. * sed.h (write_mode): New. * compile.c (mark_subst_opts, compile_program): Use it instead of "w". * execute.c (open_next_file): Pass write_mode to ck_mkstemp. * utils.c (ck_mkstemp): Accept mode, pass it to fdopen. Constify. * utils.h (ck_mkstemp): Adjust prototype.
* declare bool arguments as int instead to please AIX XLCPaolo Bonzini2009-04-301-2/+2
| | | | | | | | | 2009-04-30 Paolo Bonzini <bonzini@gnu.org> * sed/compile.c: Declare bool arguments as int instead to please AIX XLC. * sed/execute.c: Declare bool arguments as int instead to please AIX XLC. * sed/utils.c: Declare bool arguments as int instead to please AIX XLC. * sed/utils.h: Declare bool arguments as int instead to please AIX XLC.
* switch to GPLv3v4.1dPaolo Bonzini2008-01-091-1/+1
| | | | git-archimport-id: bonzini@gnu.org--2004b/sed--stable--4.1--patch-94
* --posix disables all extensions to regular expressionsPaolo Bonzini2008-01-091-0/+1
| | | | | | | | 2006-12-15 Paolo Bonzini <bonzini@gnu.org> * sed/regexp.c: Disable all extensions on --posix. git-archimport-id: bonzini@gnu.org--2004b/sed--stable--4.1--patch-84
* gnulibify and add ACL supportPaolo Bonzini2008-01-091-0/+49
2006-09-24 Paolo Bonzini <bonzini@gnu.org> * sed/execute.c: Support ACLs. * lib/utils.c: Move... * sed/utils.c: ... here, and remove xmalloc. git-archimport-id: bonzini@gnu.org--2004b/sed--stable--4.1--patch-74