binutils-gdb/readline
Andrew Burgess 32a1adcccf gdb/readline: fix use of an undefined variable
This commit in binutils-gdb:

  commit 830b67068c
  Date:   Fri Jul 12 09:53:02 2019 +0200

      [readline] Fix heap-buffer-overflow in update_line

Which corresponds to this commit in upstream readline:

  commit 31547b4ea4a1a904e1b08e2bc4b4ebd5042aedaa
  Date:   Mon Aug 5 10:24:27 2019 -0400

      commit readline-20190805 snapshot

Introduced a use of an undefined variable, which can be seen using
valgrind:

  $ valgrind --tool=memcheck gdb
  GNU gdb (GDB) 8.3.50.20190918-git
  Copyright (C) 2019 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.
  Type "show copying" and "show warranty" for details.
  This GDB was configured as "x86_64-pc-linux-gnu".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  <http://www.gnu.org/software/gdb/bugs/>.
  Find the GDB manual and other documentation resources online at:
      <http://www.gnu.org/software/gdb/documentation/>.

  For help, type "help".
  Type "apropos word" to search for commands related to "word".
  ==24924== Conditional jump or move depends on uninitialised value(s)
  ==24924==    at 0x9986C3: rl_redisplay (display.c:710)
  ==24924==    by 0x9839CE: readline_internal_setup (readline.c:447)
  ==24924==    by 0x9A1C2B: _rl_callback_newline (callback.c:100)
  ==24924==    by 0x9A1C85: rl_callback_handler_install (callback.c:111)
  ==24924==    by 0x6195EB: gdb_rl_callback_handler_install(char const*) (event-top.c:319)
  ==24924==    by 0x61975E: display_gdb_prompt(char const*) (event-top.c:409)
  ==24924==    by 0x4FBFE3: cli_interp_base::pre_command_loop() (cli-interp.c:286)
  ==24924==    by 0x6E53DA: interp_pre_command_loop(interp*) (interps.c:321)
  ==24924==    by 0x731F30: captured_command_loop() (main.c:334)
  ==24924==    by 0x733568: captured_main(void*) (main.c:1182)
  ==24924==    by 0x7335CE: gdb_main(captured_main_args*) (main.c:1197)
  ==24924==    by 0x41325D: main (gdb.c:32)
  ==24924==
  (gdb)

The problem can be traced back to init_line_structures.  The very
first time this function is ever called its MINSIZE parameter is
always 0 and the global LINE_SIZE is 1024.  Prior to the above
mentioned commits we spot that the line_state variables have not yet
been initialised, and allocate them some new buffer, then we enter
this loop:

  for (n = minsize; n < line_size; n++)
    {
      visible_line[n] = 0;
      invisible_line[n] = 1;
    }

which would initialise everything from the incoming minimum up to the
potentially extended upper line size.

The problem is that the above patches added a new condition that would
bump up the minsize like this:

  if (minsize <= _rl_screenwidth)	/* XXX - for gdb */
    minsize = _rl_screenwidth + 1;

So, the first time this function is called the incoming MINSIZE is 0,
the LINE_SIZE global is 1024, and if the _rl_screenwidth is 80, we see
that MINSIZE will be pushed up to 80.  We still notice that the line
state is uninitialised and allocate some buffers, then we enter the
initialisation loop:

  for (n = minsize; n < line_size; n++)
    {
      visible_line[n] = 0;
      invisible_line[n] = 1;
    }

And initialise from 80 to 1023 i the newly allocated buffers, leaving
0 to 79 uninitialised.

To confirm this is an issue, if we then look at rl_redisplay we see
that a call to init_line_structures is followed first by a call to
rl_on_new_line, which does initialise visible_line[0], but not
invisible_line[0].  Later in rl_redisplay we have this logic:

  if (visible_line[0] != invisible_line[0])
    rl_display_fixed = 0;

The use of invisible_line[0] here will be undefined.

Considering how this variable was originally initialised before the
above patches, this patch modifies the initialisation loop in
init_line_structures, to use the original value of MINSIZE.  With this
change the valgrind warning goes away.

readline/ChangeLog:

	PR cli/24980
	* display.c (init_line_structures): Initialise line_state using
	original minsize value.
2019-09-23 22:35:05 +01:00
..
cross-build
doc Import readline 8.0 2019-08-12 10:57:56 -06:00
examples Import readline 8.0 2019-08-12 10:57:56 -06:00
shlib Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
support Import readline 8.0 2019-08-12 10:57:56 -06:00
.gitignore Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
aclocal.m4 Import readline 8.0 2019-08-12 10:57:56 -06:00
ansi_stdlib.h
bind.c Import readline 8.0 2019-08-12 10:57:56 -06:00
callback.c Import readline 8.0 2019-08-12 10:57:56 -06:00
CHANGELOG Import readline 8.0 2019-08-12 10:57:56 -06:00
ChangeLog.gdb gdb/readline: fix use of an undefined variable 2019-09-23 22:35:05 +01:00
CHANGES Import readline 8.0 2019-08-12 10:57:56 -06:00
chardefs.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
colors.c [readline] Fix compilation on MinGW 2019-08-13 14:57:03 -05:00
colors.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
compat.c Import readline 8.0 2019-08-12 10:57:56 -06:00
complete.c Import readline 8.0 2019-08-12 10:57:56 -06:00
config.h.in Import readline 8.0 2019-08-12 10:57:56 -06:00
configure Import readline 8.0 2019-08-12 10:57:56 -06:00
configure.ac Import readline 8.0 2019-08-12 10:57:56 -06:00
COPYING
display.c gdb/readline: fix use of an undefined variable 2019-09-23 22:35:05 +01:00
emacs_keymap.c Import readline 8.0 2019-08-12 10:57:56 -06:00
funmap.c Import readline 8.0 2019-08-12 10:57:56 -06:00
histexpand.c Import readline 8.0 2019-08-12 10:57:56 -06:00
histfile.c Import readline 8.0 2019-08-12 10:57:56 -06:00
histlib.h Import readline 8.0 2019-08-12 10:57:56 -06:00
history.c Import readline 8.0 2019-08-12 10:57:56 -06:00
history.h Import readline 8.0 2019-08-12 10:57:56 -06:00
histsearch.c Import readline 8.0 2019-08-12 10:57:56 -06:00
input.c Import readline 8.0 2019-08-12 10:57:56 -06:00
INSTALL Import readline 8.0 2019-08-12 10:57:56 -06:00
isearch.c Import readline 8.0 2019-08-12 10:57:56 -06:00
keymaps.c Import readline 8.0 2019-08-12 10:57:56 -06:00
keymaps.h Import readline 8.0 2019-08-12 10:57:56 -06:00
kill.c Import readline 8.0 2019-08-12 10:57:56 -06:00
macro.c Import readline 8.0 2019-08-12 10:57:56 -06:00
Makefile.in Import readline 8.0 2019-08-12 10:57:56 -06:00
MANIFEST Import readline 8.0 2019-08-12 10:57:56 -06:00
mbutil.c Import readline 8.0 2019-08-12 10:57:56 -06:00
misc.c Import readline 8.0 2019-08-12 10:57:56 -06:00
NEWS Import readline 8.0 2019-08-12 10:57:56 -06:00
nls.c Import readline 8.0 2019-08-12 10:57:56 -06:00
parens.c Import readline 8.0 2019-08-12 10:57:56 -06:00
parse-colors.c Import readline 8.0 2019-08-12 10:57:56 -06:00
parse-colors.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
patchlevel Import readline 8.0 2019-08-12 10:57:56 -06:00
posixdir.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
posixjmp.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
posixselect.h
posixstat.h
readline.c Import readline 8.0 2019-08-12 10:57:56 -06:00
readline.h Import readline 8.0 2019-08-12 10:57:56 -06:00
readline.pc.in Import readline 8.0 2019-08-12 10:57:56 -06:00
README Import readline 8.0 2019-08-12 10:57:56 -06:00
rlconf.h Import readline 8.0 2019-08-12 10:57:56 -06:00
rldefs.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
rlmbutil.h Import readline 8.0 2019-08-12 10:57:56 -06:00
rlprivate.h Import readline 8.0 2019-08-12 10:57:56 -06:00
rlshell.h
rlstdc.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
rltty.c Import readline 8.0 2019-08-12 10:57:56 -06:00
rltty.h
rltypedefs.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
rlwinsize.h
savestring.c Import readline 8.0 2019-08-12 10:57:56 -06:00
search.c Import readline 8.0 2019-08-12 10:57:56 -06:00
shell.c Import readline 8.0 2019-08-12 10:57:56 -06:00
signals.c Import readline 8.0 2019-08-12 10:57:56 -06:00
tcap.h Import readline 7.0 (patch 5) 2019-08-12 10:57:56 -06:00
terminal.c Import readline 8.0 2019-08-12 10:57:56 -06:00
text.c Import readline 8.0 2019-08-12 10:57:56 -06:00
tilde.c Import readline 8.0 2019-08-12 10:57:56 -06:00
tilde.h
undo.c Import readline 8.0 2019-08-12 10:57:56 -06:00
USAGE
util.c Import readline 8.0 2019-08-12 10:57:56 -06:00
vi_keymap.c Import readline 8.0 2019-08-12 10:57:56 -06:00
vi_mode.c Import readline 8.0 2019-08-12 10:57:56 -06:00
xfree.c Import readline 8.0 2019-08-12 10:57:56 -06:00
xmalloc.c Import readline 8.0 2019-08-12 10:57:56 -06:00
xmalloc.h Remove gdb workaround from readline/xfree.c 2019-08-12 10:57:56 -06:00

Introduction
============

This is the Gnu Readline library, version 8.0.

The Readline library provides a set of functions for use by applications
that allow users to edit command lines as they are typed in.  Both
Emacs and vi editing modes are available.  The Readline library includes
additional functions to maintain a list of previously-entered command
lines, to recall and perhaps reedit those lines, and perform csh-like
history expansion on previous commands.

The history facilites are also placed into a separate library, the
History library, as part of the build process.  The History library
may be used without Readline in applications which desire its
capabilities.

The Readline library is free software, distributed under the terms of
the [GNU] General Public License as published by the Free Software
Foundation, version 3 of the License.  For more information, see the
file COPYING.

To build the library, try typing `./configure', then `make'.  The
configuration process is automated, so no further intervention should
be necessary.  Readline builds with `gcc' by default if it is
available.  If you want to use `cc' instead, type

        CC=cc ./configure

if you are using a Bourne-style shell.  If you are not, the following
may work:

        env CC=cc ./configure

Read the file INSTALL in this directory for more information about how
to customize and control the build process.

The file rlconf.h contains C preprocessor defines that enable and disable
certain Readline features.

The special make target `everything' will build the static and shared
libraries (if the target platform supports them) and the examples.

Examples
========

There are several example programs that use Readline features in the
examples directory.  The `rl' program is of particular interest.  It
is a command-line interface to Readline, suitable for use in shell
scripts in place of `read'.

Shared Libraries
================

There is skeletal support for building shared versions of the
Readline and History libraries.  The configure script creates
a Makefile in the `shlib' subdirectory, and typing `make shared'
will cause shared versions of the Readline and History libraries
to be built on supported platforms.

If `configure' is given the `--enable-shared' option, it will attempt
to build the shared libraries by default on supported platforms.

Configure calls the script support/shobj-conf to test whether or
not shared library creation is supported and to generate the values
of variables that are substituted into shlib/Makefile.  If you
try to build shared libraries on an unsupported platform, `make'
will display a message asking you to update support/shobj-conf for
your platform.

If you need to update support/shobj-conf, you will need to create
a `stanza' for your operating system and compiler.  The script uses
the value of host_os and ${CC} as determined by configure.  For
instance, FreeBSD 4.2 with any version of gcc is identified as
`freebsd4.2-gcc*'.

In the stanza for your operating system-compiler pair, you will need to
define several variables.  They are:

SHOBJ_CC	The C compiler used to compile source files into shareable
		object files.  This is normally set to the value of ${CC}
		by configure, and should not need to be changed.

SHOBJ_CFLAGS	Flags to pass to the C compiler ($SHOBJ_CC) to create
		position-independent code.  If you are using gcc, this
		should probably be set to `-fpic'.

SHOBJ_LD	The link editor to be used to create the shared library from
		the object files created by $SHOBJ_CC.  If you are using
		gcc, a value of `gcc' will probably work.

SHOBJ_LDFLAGS	Flags to pass to SHOBJ_LD to enable shared object creation.
		If you are using gcc, `-shared' may be all that is necessary.
		These should be the flags needed for generic shared object
		creation.

SHLIB_XLDFLAGS	Additional flags to pass to SHOBJ_LD for shared library
		creation.  Many systems use the -R option to the link
		editor to embed a path within the library for run-time
		library searches.  A reasonable value for such systems would
		be `-R$(libdir)'.

SHLIB_LIBS	Any additional libraries that shared libraries should be
		linked against when they are created.

SHLIB_LIBPREF	The prefix to use when generating the filename of the shared
		library.  The default is `lib'; Cygwin uses `cyg'.

SHLIB_LIBSUFF	The suffix to add to `libreadline' and `libhistory' when
		generating the filename of the shared library.  Many systems
		use `so'; HP-UX uses `sl'.

SHLIB_LIBVERSION The string to append to the filename to indicate the version
		of the shared library.  It should begin with $(SHLIB_LIBSUFF),
		and possibly include version information that allows the
		run-time loader to load the version of the shared library
		appropriate for a particular program.  Systems using shared
		libraries similar to SunOS 4.x use major and minor library
		version numbers; for those systems a value of
		`$(SHLIB_LIBSUFF).$(SHLIB_MAJOR)$(SHLIB_MINOR)' is appropriate.
		Systems based on System V Release 4 don't use minor version
		numbers; use `$(SHLIB_LIBSUFF).$(SHLIB_MAJOR)' on those systems.
		Other Unix versions use different schemes.

SHLIB_DLLVERSION The version number for shared libraries that determines API
		compatibility between readline versions and the underlying
		system.  Used only on Cygwin.  Defaults to $SHLIB_MAJOR, but
		can be overridden at configuration time by defining DLLVERSION
		in the environment.

SHLIB_DOT	The character used to separate the name of the shared library
		from the suffix and version information.  The default is `.';
		systems like Cygwin which don't separate version information
		from the library name should set this to the empty string.

SHLIB_STATUS	Set this to `supported' when you have defined the other
		necessary variables.  Make uses this to determine whether
		or not shared library creation should be attempted.

You should look at the existing stanzas in support/shobj-conf for ideas.

Once you have updated support/shobj-conf, re-run configure and type
`make shared'.  The shared libraries will be created in the shlib
subdirectory.

If shared libraries are created, `make install' will install them. 
You may install only the shared libraries by running `make
install-shared' from the top-level build directory.  Running `make
install' in the shlib subdirectory will also work.  If you don't want
to install any created shared libraries, run `make install-static'. 

Documentation
=============

The documentation for the Readline and History libraries appears in
the `doc' subdirectory.  There are three texinfo files and a
Unix-style manual page describing the facilities available in the
Readline library.  The texinfo files include both user and
programmer's manuals.  HTML versions of the manuals appear in the
`doc' subdirectory as well. 

Usage
=====

Our position on the use of Readline through a shared-library linking
mechanism is that there is no legal difference between shared-library
linking and static linking--either kind of linking combines various
modules into a single larger work.  The conditions for using Readline
in a larger work are stated in section 3 of the GNU GPL.

Reporting Bugs
==============

Bug reports for Readline should be sent to:

        bug-readline@gnu.org

When reporting a bug, please include the following information:

        * the version number and release status of Readline (e.g., 4.2-release)
        * the machine and OS that it is running on
        * a list of the compilation flags or the contents of `config.h', if
          appropriate
        * a description of the bug
        * a recipe for recreating the bug reliably
        * a fix for the bug if you have one!

If you would like to contact the Readline maintainer directly, send mail
to bash-maintainers@gnu.org.

Since Readline is developed along with bash, the bug-bash@gnu.org mailing
list (mirrored to the Usenet newsgroup gnu.bash.bug) often contains
Readline bug reports and fixes. 

Chet Ramey
chet.ramey@case.edu