1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-10-20 02:38:43 +00:00
freebsd/usr.bin/indent
Pedro F. Giffuni 458051a57b indent(1): Avoid out of bound access of array in_buffer
Work-around a somewhat complex interaction within the code. From
Piotr's commit [1]:

When pr_comment() calls dump_line() for the first line of a multiline
comment, it doesn't include any indentation - it starts with the "/*".
This is consistent for both boxed and not boxed comments. Where the logic
diverges is in how it treats the rest of the lines of the comment. For box
comments indent assumes that it must not change anything, so lines are
dumped as they were, including the indentation where it exists. For the
rest of comments, it will first remove the indentation to store plain text
of the comment and then add it again where indent thinks it's appropriate
-- this is part of comment re-indenting process.

For continuations of multi-line comments, the code that handles comments
in dump_line() will use pad_output() to create indentation from the
beginning of the line (what indent calls the first column) and then write
string pointed by s_com afterwards. But if it's a box comment, the string
will include original indentation, unless it's the first line of the
comment. This is why tab characters from s_com have to be considered when
calculating how much padding is needed and the "while (*com_st == '\t')
com_st++, target += 8;" does that.

In dump_line(), /target/ is initially set to ps.com_col, so it always
assumes that indentation needs to be produced in this function, regardless
of which line of a box comment it is. But for the first line of a box
comment it is not true, so pr_comment() signals it by setting
ps.n_comment_delta, the negative comment delta, to a negative number which
is then added to /target/ in dump_line() on all lines except the first
one, so that the function produces adequate indentation in this special
case.

The bug was in how that negative offset was calculated: pr_comment() used
count_spaces() on in_buffer, which pr_comment() expected to contain
non-null terminated sequence of characters, originating from whatever
originally was on the left side of the comment. Understanding that
count_spaces() requires a string, pr_comment() temporarily set buf_ptr[-2]
to 0 in hope that it would nul-terminate the right thing in in_buffer and
calling count_spaces() would be safe and do the expected thing. This was
false whenever buf_ptr would point into save_com, an entirely different
char array than in_buffer.

The short-term fix is to recognize whether buf_ptr points into in_buffer
or save_com.

Reference:
[1]
ea486a2aa3

Taken from:	 Piotr Stefaniak
2016-12-01 01:32:13 +00:00
..
args.c indent(1): Fix memory leaks pointed out by clang-analyzer. 2016-08-23 01:40:45 +00:00
indent_codes.h indent(1): Properly handle the wide string literal and wide char constant L. 2016-11-27 20:38:14 +00:00
indent_globs.h indent(1): add new -sac and -U options. 2016-08-03 22:08:07 +00:00
indent.1 indent(1): add new -sac and -U options. 2016-08-03 22:08:07 +00:00
indent.c indent(1): Properly handle the wide string literal and wide char constant L. 2016-11-27 20:38:14 +00:00
indent.h indent(1): Use bsearch() for looking up type keywords. 2016-08-04 15:27:09 +00:00
io.c indent(1): Avoid out of bound access of array in_buffer 2016-12-01 01:32:13 +00:00
lexi.c indent(1): Properly handle the wide string literal and wide char constant L. 2016-11-27 20:38:14 +00:00
Makefile
Makefile.depend
parse.c indent(1): minor off-by-one error. 2016-11-27 20:11:55 +00:00
pr_comment.c indent(1): Avoid out of bound access of array in_buffer 2016-12-01 01:32:13 +00:00
README

  $FreeBSD$

This is the C indenter, it originally came from the University of Illinois
via some distribution tape for PDP-11 Unix.  It has subsequently been
hacked upon by James Gosling @ CMU.  It isn't very pretty, and really needs
to be completely redone, but it is probably the nicest C pretty printer
around.

Further additions to provide "Kernel Normal Form" were contributed
by the folks at Sun Microsystems.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> From mnetor!yunexus!oz@uunet.UU.NET Wed Mar  9 15:30:55 1988
> Date: Tue, 8 Mar 88 18:36:25 EST
> From: yunexus!oz@uunet.UU.NET (Ozan Yigit)
> To: bostic@okeeffe.berkeley.edu
> Cc: ccvaxa!willcox@uunet.UU.NET, jag@sun.com, rsalz@uunet.UU.NET
> In-Reply-To: Keith Bostic's message of Tue, 16 Feb 88 16:09:06 PST 
> Subject: Re: Indent...

Thank you for your response about indent. I was wrong in my original
observation (or mis-observation :-). UCB did keep the Illinois
copyright intact.

The issue still is whether we can distribute indent, and if we can, which
version. David Willcox (the author) states that:

| Several people have asked me on what basis I claim that indent is in
| the public domain.  I knew I would be sorry I made that posting.
| 
| Some history.  Way back in 1976, the project I worked on at the
| University of Illinois Center for Advanced Computation had a huge
| battle about how to format C code.  After about a week of fighting, I
| got disgusted and wrote a program, which I called indent, to reformat C
| code.  It had a bunch of different options that would let you format
| the output the way you liked.  In particular, all of the different
| formats being championed were supported.
| 
| It was my first big C program.  It was ugly.  It wasn't designed, it
| just sort of grew.  But it pretty much worked, and it stopped most of
| the fighting.
| 
| As a matter of form, I included a University of Illinois Copyright
| notice.  However, my understanding was that, since the work was done
| on an ARPA contract, it was in the public domain.
| 
| Time passed.  Some years later, indent showed up on one of the early
| emacs distributions.
| 
| Later still, someone from UC Berkeley called the UofI and asked if
| indent was in the public domain.  They wanted to include it in their
| UNIX distributions, along with the emacs stuff.  I was no longer at the
| UofI, but Rob Kolstad, who was, asked me about it.  I told him I didn't
| care if they used it, and since then it has been on the BSD distributions.
| 
| Somewhere along the way, several other unnamed people have had their
| hands in it.  It was converted to understand version 7 C.  (The
| original was version 6.)  It was converted from its original filter
| interface to its current "blow away the user's file" interface.
| The $HOME/.indent.pro file parsing was added.  Some more formatting
| options were added.
| 
| The source I have right now has two copyright notices.  One is the
| original from the UofI.  One is from Berkeley.
| 
| I am not a lawyer, and I certainly do not understand copyright law.  As
| far as I am concerned, the bulk of this program, everything covered by
| the UofI copyright, is in the public domain, and worth every penny.
| Berkeley's copyright probably should only cover their changes, and I
| don't know their feelings about sending it out.  

In any case, there appears to be none at UofI to clarify/and change
that copyright, but I am confident (based on the statements of its
author) that the code, as it stands with its copyright, is
distributable, and will not cause any legal problems.

Hence, the issue reduces to *which* one to distribute through
comp.sources.unix. I would suggest that with the permission of you
folks (given that you have parts copyrighted), we distribute the 4.3
version of indent, which appears to be the most up-to-date version. I
happen to have just about every known version of indent, including the
very original submission from the author to a unix tape, later the
G-Emacs version, any 4.n version, sun version and the Unipress
version.  I still think we should not have to "go-back-in-time" and
re-do all the work you people have done.

I hope to hear from you as to what you think about this. You may of
course send 4.3 version to the moderator directly, or you can let me
know of your permission, and I will send the sources, or you can let
me know that 4.3 version is off-limits, in which case we would probably
have to revert to an older version. One way or another, I hope to get
a version of indent to comp.sources.unix.

regards..	oz

cc: ccvaxa!willcox
    sun.com!jar
    uunet!rsalz