Commit Graph

88 Commits

Author SHA1 Message Date
rillig d9ab151b25 make: in CLEANUP mode, free interned strings at the very end
Noticed by sjg.
2021-12-15 10:57:01 +00:00
rillig 5da10dfc62 make: fix memory leak for filenames in .for loops (since 2013-06-18)
Previously, each time a .for directive pushed its buffer on the input
file stack, the current filename was duplicated.  This was a waste of
memory.

The name of a file is typically only used while it is read in.  There is
one situation when the filename is needed for longer, which is when a
target is defined.

Since .for loops are implemented as a special form of included files,
each .for loop duplicated the current filename as well.

$ cat << EOF > for.mk
.for i in 1 2 3 4 5 6 7 8 9 0
.for i in 1 2 3 4 5 6 7 8 9 0
.for i in 1 2 3 4 5 6 7 8 9 0
.for i in 1 2 3 4 5 6 7 8 9 0
.for i in 1 2 3 4 5 6 7 8 9 0
.for i in 1 2 3 4 5 6 7 8 9 0
.for i in 1 2 3 4 5 6 7 8 9 0
.endfor
.endfor
.endfor
.endfor
.endfor
.endfor
.endfor

all:
	@ps -o rsz -p ${.MAKE.PID}
EOF

$ make-2021.12.13.03.55.16 -r -f for.mk
  RSZ
10720

$ ./make -r -f for.mk
 RSZ
1716

The difference is 8 MB, which amounts to 1 million .for loops.
2021-12-13 05:25:04 +00:00
rillig 8e716a5983 make: eliminate local variable in Substring_Words
No functional change.
2021-06-21 16:59:18 +00:00
rillig 0137ba05b0 make: inline str_concat4
This function is only ever used for forming strings of the form
"archive(member)".

No functional change.
2021-05-30 21:16:54 +00:00
rillig b740ab5e2d make: avoid unnecessary calls to strlen when evaluating modifiers
No functional change.
2021-04-11 19:05:06 +00:00
rillig a71bce0149 make: backport to C90
In the past few months I had accidentally used C99 features in the make
code.  According to tools/README, tools that are used in the build
system should restrict themselves to C90.

This allows make to build with GCC's options "-pedantic
-Wno-system-headers -Dinline= -Wno-error=cast-qual".

I didn't notice anyone actively complaining though, I just wanted to see
how much work this backporting would be.  The identifier __func__ is
still used, as in other tools.

No functional change.
2021-04-03 14:39:02 +00:00
rillig 244fd9f4c4 make: use C99 bool type instead of defining its own
No functional change.
2021-04-03 11:08:40 +00:00
rillig 21f0a9e6c9 make: document necessary tests for Str_Match 2021-02-01 22:36:28 +00:00
rillig 139d9dbc24 make: replace pre-increment with post-increment or simple addition
The rest of the code already prefers post-increment if there is no
actual difference.
2021-02-01 19:46:58 +00:00
rillig 85aee7a6f7 make(1): remove do-not-format markers from comments
These markers had been used inconsistently.  Furthermore the source code
had not been formatted automatically before 2020 at all, otherwise there
wouldn't have been any trailing whitespace left.
2021-01-19 20:51:46 +00:00
rillig 810790fcb7 make(1): make a few more bool expressions more precise
The previous version of lint(1) from a few hours ago didn't catch all
occurrences.  And even the current one doesn't catch everything.
Function arguments and return types still need some work.  The "return
quietly" from shouldDieQuietly still implicitly converts from int to
_Bool.

No functional change.
2021-01-10 23:59:53 +00:00
rillig 31940a95cd make(1): consistently use boolean expressions in conditions
Most of the make code already followed the style of explicitly writing
(ptr != NULL) instead of the shorter (ptr) in conditions.

The remaining 50 instances have been found by an experimental,
unpublished check in lint(1) that treats bool expressions as
incompatible to any other scalar type, just as in Java, C#, Pascal and
several other languages.

The only unsafe operation on Boolean that is left over is (flags &
FLAG), for an enum implementing a bit set.  If Boolean is an ordinary
integer type (the default), some high bits may get lost.  But if Boolean
is the same as _Bool (by compiling with -DUSE_C99_BOOLEAN), C99 6.3.1.2
defines that a conversion from any scalar to the type _Bool acts as a
comparison to 0, which cannot lose any bits.
2021-01-10 21:20:46 +00:00
rillig b754dcb0cc make(1): format multi-line comments 2020-12-30 10:03:16 +00:00
rillig ab1d2fd5b0 make(1): remove redundant call to strlen in Str_Words 2020-12-12 19:13:47 +00:00
rillig 71e1e47432 make(1): use postfix increment where possible 2020-11-16 18:28:27 +00:00
rillig 1c1f145671 make(1): clean up make.h, meta.c, parse.c, str.c
The main changes are in the comments, which have been shortened and
corrected.

Some local variables changed their names.

In ParseErrorInternal, the scope of va_start is now narrower.

In ParseDoDependency, the type of tOp has been fixed.

ParseGetLine doesn't take flags anymore but instead a parsing mode.
Previously, the flags had not been combined anyway.

At the beginning of Parse_File, fatals is already guaranteed to be 0, and
even if not, it would be wrong to just discard the fatal errors.
2020-11-15 12:02:44 +00:00
rillig 3a6dda4ef7 make(1): add pp_skip_hspace to skip horizontal whitespace during parsing 2020-11-07 10:44:53 +00:00
rillig a0c9252554 make(1): clean up code stylistically
* Replace character literal 0 with '\0'.
* Replace pointer literal 0 with NULL.
* Remove redundant parentheses.
* Parentheses in multi-line conditions are not redundant at the
  beginning of a line.
* Replace a few !ptr with ptr == NULL.
* Replace a few ptr with ptr != NULL.
* Replace (expr & mask) == 0 with !(expr & mask).
* Remove redundant braces for blocks in cases where the generated code
  stays the same.  (Assertions further down in the code would get
  different line numbers.)
* Rename parameters in CondParser_String to reflect the data flow.
* Replace #ifdef notdef with #if 0.

The generated code stays exactly the same, at least with GCC 5.5.0 on
NetBSD 8.0 amd64 using the default configuration.
2020-11-07 10:16:18 +00:00
rillig 12d3b8fc47 make(1): remove macros MIN and MAX
These macros typically evaluate one of their arguments twice.  Until
2020-08-31, they had not parenthesized their arguments properly.  They
are only used in a few places, therefore it doesn't hurt much to have
them expanded.
2020-10-24 20:51:49 +00:00
rillig 60b23912e3 make(1): remove redundant type casts
This mainly affects the void pointers in callback functions for lists.
These had been necessary once when the parameter type was still
ClientData instead of void pointer.
2020-10-22 05:50:02 +00:00
rillig f336e55bc4 make(1): revert previous commit
It had accidentally reverted all the work from the past few days.
2020-10-05 19:27:47 +00:00
rillig b6c0384ff9 make(1): fix double-free bug in -DCLEANUP mode (since 2020-10-02)
The bug had been introduced with dir.c 1.155 on 2020-10-02 22:20:25.  In
that commit, openDirectories was replaced with a combination of a list
with a hash table, for more efficient lookup by name.

Upon cleanup, OpenDirs_Done is called, which in turn called
Dir_ClearPath.  Dir_ClearPath takes full ownership of the given list and
empties it.  This was no problem before since afterwards the list was
empty and calling Lst_Free just frees the remaining list pointer.

With OpenDirs, this list was combined with a hash table, and the hash
table contains the list nodes, assuming that the OpenDirs functions have
full ownership of both the list and the hash table.  This assumption was
generally correct, except for the one moment during cleanup where full
ownership of the list was passed to Dir_ClearPath, while the hash table
still contained pointers to the (now freed) list nodes.  This by itself
was not a problem since the hash table would be freed afterwards.  But
as part of Dir_ClearPath, OpenDirs_Remove was called, which looked up
the freed directory by name and now found the freed list node, trying to
free it again.  Boom.

Fixed by replacing the call to Dir_ClearPath with code that only frees
the directories, without giving up control over the list.
2020-10-05 19:24:29 +00:00
rillig 7455b57006 make(1): inline Str_FindSubstring in JobOutput 2020-10-03 15:00:57 +00:00
rillig 6de75a95f8 make(1): clean up RCSID blocks
These blocks mostly consisted of redundant structure, following the same
#ifndef pattern over and over, with only minimal variation.

It's easier to maintain if the common structure is only written once and
encapsulated in a macro.

To avoid "defined but unused" warnings from GCC in the case where
MAKE_NATIVE is not defined, I had to add volatile.  Adding
MAKE_ATTR_UNUSED alone would not preserve the rcsid variable in the
resulting binary.
2020-09-13 15:15:51 +00:00
rillig 5dd250059b make(1): replace brk_string with Str_Words
The API is much simpler, and there is less detail that is exposed by
default and fewer punctuation to type on the caller's side.  To see that
there is some memory to be freed, one would have to look into the
struct.  Having part of the return value as the actual return value and
the rest in output parameters was unnecessarily asymmetrical.
2020-08-30 19:56:02 +00:00
rillig 5783a5d404 make(1): allow for strict type checking for Boolean
Having Boolean aliased to int creates ambiguities since int is widely
used.  Allow to occasionally compile make with -DUSE_DOUBLE_BOOLEAN to
check that the type definitions still agree.
2020-08-29 07:52:55 +00:00
rillig 38885a7509 make(1): make brk_string return size_t for the number of words 2020-08-23 18:26:35 +00:00
rillig fe6807ec25 make(1): prepare str.c for WARNS=6
The next step is to make brk_string return size_t instead of int.
2020-08-23 18:03:35 +00:00
rillig 499d5689cb make(1): add str_concat4 to make the other code simpler
There's no need for arch.c to call strlen when there is a high-level API
for the same purpose.
2020-08-11 18:41:46 +00:00
rillig 644e5ad76b make(1): replace str_concat with str_concat2 and str_concat3
The new functions have a simpler interface, and str_concat3 is even more
general-purpose, since the middle string is no longer required to be
exactly of length 1.
2020-08-10 19:53:19 +00:00
rillig 21107e4e40 make(1): fix parameter name of str_concat
The previous documentation mentioned Str_Concat, but str_concat has been
written in lowercase for years.  The "flags" are not flags since they
cannot be combined, not even when they are written in hex.
2020-08-10 19:30:30 +00:00
rillig 6ae45ff07e make(1): fix integer type in str_concat 2020-08-10 19:20:33 +00:00
rillig 6bf7698c15 make(1): format code in str.c consistently 2020-08-09 10:24:02 +00:00
rillig 86abf69ed1 make(1): no declaration-after-statement anymore
NetBSD make is intended to be maximally portable, therefore it uses only
C89.  This was not declared in the Makefile before.

There are still a few places in parse.c and metachar.c that use
end-of-line comments.  These will be fixed in a follow-up commit.
2020-08-03 20:26:09 +00:00
rillig e624c50576 make(1): reduce scope of local variables in brk_string
This also removes the unused assignment to words_cap.
2020-08-01 10:44:23 +00:00
rillig efe25fc5ac make(1): document the circumstances in which brk_string returns NULL 2020-07-26 16:51:53 +00:00
rillig 5412fdea01 make(1): move SysV string matching to var.c
This kind of string matching is only used in variable modifiers, and only
if this feature is enabled by SYSVVARSUB.
2020-07-19 09:26:18 +00:00
rillig 12b3007c76 make(1): refactor Str_Match
- use shorter variable names to avoid line breaks
- eliminate c2 variable
- change return type to Boolean
2020-07-03 07:40:13 +00:00
rillig 76ed5386c9 make(1): clean up Str_Match
- removed unnecessary parentheses
- uniform test for end of string
2020-07-03 06:30:02 +00:00
rillig 001c9f20c0 make(1): clean up documentation of Str_Match 2020-07-03 06:25:23 +00:00
rillig a230e16d2d make(1): fix performance problem in specially crafted :M modifier
This fix was previously suspected to make the vax build fail.  The next
build succeeded though, and it started 2 hours before this fix was
reverted.
2020-06-15 14:46:28 +00:00
rillig 014f40b954 usr.bin/make: revert performance improvement
That change might be the cause of a build failure for vax.
http://releng.netbsd.org/builds/HEAD/202006131940Z/ builds fine.
http://releng.netbsd.org/builds/HEAD/202006141020Z/ doesn't.

The build fails with:
/home/source/ab/HEAD/src/external/gpl3/gcc/dist/gcc/machmode.h:524:28:
error: 'mode_size_inline' was not declared in this scope
2020-06-14 23:13:21 +00:00
rillig 09fc09f3fb usr.bin/make: make Str_Match faster for repeated asterisks
Conceptually related to https://en.wikipedia.org/wiki/ReDoS.
2020-06-13 21:16:27 +00:00
rillig e8c43cef44 usr.bin/make: remove redundant parentheses around return 2020-06-13 07:48:59 +00:00
rillig 6bcabb95b0 usr.bin/make: consistently use ++ for incrementing pointers 2020-06-13 07:36:07 +00:00
rillig 2548dc6c57 usr.bin/make: fix typo in comment 2020-06-13 07:30:02 +00:00
christos 3e820cb068 Behave like gmake: empty string does not match % pattern 2020-05-06 02:30:10 +00:00
rillig 8176f52bc9 usr.bin/make: refactor brk_string
The variables are renamed to reflect to which memory region each pointer
belongs.

The variable "curlen" was always zero.

The type of "ch" has changed to char, and its scope is now limited to
its actual use.

Comparisons of pointers are now consistently written as p != NULL
instead of !p, like character comparisons are written as ch != '\0'.

The "store_words_buf" is updated when the function returns, not before.
2020-05-03 12:10:28 +00:00
christos 3b58d8437a - Percent on the rhs is special only if the lhs has one too.
- If the rhs does not have percent, but the lhs has, the result is the rhs
This behavior matches gmake
2020-04-25 18:20:57 +00:00
rillig f5741db816 Fix out-of-bounds read in Str_Match. 2019-12-01 23:53:49 +00:00