0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-07-24 10:25:42 -04:00

232 Commits

Author SHA1 Message Date
H. Peter Anvin
8d62e99e14 Add %note directive to add a note in the list file
This differs from a plain old comment in the following ways:

1. It is optionally macro-expanded;
2. It has a dash prefix;
3. It can be used inside .nolist macros.

Suggested-by: <pushbx@ulukai.org>
Resolves: https://bugzilla.nasm.us/show_bug.cgi?id=3392915
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-09-19 13:21:30 +02:00
H. Peter Anvin
33ef63669c doc: improve some formatting, mostly of the warning list
Fix some formatting markups, especially with regards to warnings.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-04-04 12:58:32 -07:00
H. Peter Anvin
9f83c383e4 preproc, %map(): require second colon, update documentation
Require the second colon before the grouped parameter count; otherwise
the syntax is ambiguous since an expression can start with (.

Update/complete the documentation and the examples.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 13:42:16 -07:00
H. Peter Anvin
cb96db9b70 preproc: add the ability for %map() to have fixed arguments
Add the ability to have fixed arguments in %map. This is extremely
useful for parameterizing the invoked macro using arguments to a
surrounding macro.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 02:56:13 -07:00
H. Peter Anvin
78bde7562d preproc: factor expand_one_smacro() even more
Separate out counting and parsing smacro parameters into separate
functions. This not only makes the code *way* easier to read, but
these can be re-used e.g. for %map().

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 02:03:56 -07:00
H. Peter Anvin
8584bce804 preproc: handle empty expansion in %map
%map(foo) should expand to the empty string, but instead crashed NASM.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 01:24:20 -07:00
H. Peter Anvin
34eefd3803 preproc: add %map() function and radix specifiers
Add the %map() function which can apply arguments to a macro from a
list.

Allow the user to specify the desired radix for an evaluated
parameter. It doesn't make any direct difference, but can be nice for
debugging or turning into strings.

As part of this, split expand_one_smacro() into two parts: parameter
parsing and macro expansion. This is a very straightforward splitting
of two mostly unrelated pieces of functionality.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-15 22:43:28 -07:00
H. Peter Anvin
2e9120e56f BR 3392893: preproc: correctly handle empty %rep blocks
When expanding %rep blocks, if any of the %rep blocks are empty, there
may be need to unwind the %rep stack multiple times. The code would
not do so -- there was a break; in the loop, which incidentally turned
it into something that wasn't a loop at all.

Reported-by: E. C. Maslock <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-13 20:51:52 -07:00
H. Peter Anvin
dcac46d973 preproc: add options for a base prefix to %num(), add %hex()
Make it possible to add a base prefix to %num().

Add the %hex() function, producing hexadecimal values that are
nevertheless valid NASM numeric constants.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-13 17:50:55 -07:00
H. Peter Anvin
e2dc551796 preproc: fix the parameter number in error messages
The user would generally expect the parameter number to be counted
from 1 for human purposes, and that is also consistent with %1, %2,
... for multi-line macros.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-13 17:21:12 -07:00
C. Masloch
d48839780d BR 3392892: fix memory leak related to BR 3392414
The fix for BR 3392414 introduced a fairly serious memory
leak. C. Masloch was kind enough to track down the proper root cause
and fix it correctly.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-11 13:58:48 -07:00
H. Peter Anvin
488d7c7bee preproc: fix memory leak in %[...] processing
"Why dup_tlist() here? We should own it."

Yes, we own it, but we still need to advance the tail pointer. Create
steal_tlist() for this purpose.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392774
Reported-and-Debugged-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 12:08:07 -08:00
H. Peter Anvin
ae0d289123 preproc: avoid crashing on %exitmacro in broken nesting
Broken %if, %rep and %macro nesting can result in the %exitmacro
unwind overrunning the condition stack. Fix.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392796
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:51:15 -08:00
C. Masloch
65d1eca5c6 preproc: fix incorrect use of nasm_new() in alloc_Token()
The argument to nasm_new() is the pointer, not the indirection from
the pointer. This code is only relevant when compiled without token
recycling (TOKEN_BLOCKSIZE not set), but it is still wrong...

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:26:26 -08:00
C. Masloch
e0959432fe preproc: fix the expansion of %00 (token type change)
Fix the expansion of the %00 mmacro parameter; broken due to a missing
change of the token type.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392803
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:21:47 -08:00
H. Peter Anvin
516fbba03e preproc: add conditional-string smacro parameters; simplify functions
Add the option of having strings only conditionally quoted (&&) -- do
not quote an already quoted string again -- as opposed to always
quoting a string.

This makes a lot of the string functions way simpler to implement, and
removes the need to share ad hoc parsing code with directives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 10:54:19 -08:00
H. Peter Anvin
36cd305ade preproc: add %abs(), modify %num(), refactor
Add the %abs() function, to produce the absolute value as an
always-positive decimal constant.

Change the order of the arguments for %num().

Refactor the handling of optional arguments, to reduce the amount of
redundant code. This is currently only used for builtin functions, but
might be extended in the future.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-17 15:29:10 -08:00
H. Peter Anvin
1d1ba9c7d7 preproc: add %num() to format a number in an arbitrary base
Add the %num() preprocessor function, which returns a quoted string
with a number formatted in any base between 2 and 64 (using bash
encoding with '@' for 62 and '_' for 63.)

It can specify a fixed number of digits with or without truncation.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-15 16:58:20 -08:00
H. Peter Anvin
db6549c5aa preproc: classify warnings, move into common pp-* namespace
Classify all remaining WARN_OTHER warnings in the preprocessor. Move
all preprocessor warnings except "user" under a common pp-* prefix.

Warn for an out-of-range argument to the %sel() function.

Finally, use "dname" in additional places for consistency and future
ease of use.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-14 13:12:01 -08:00
H. Peter Anvin
664a79473d preproc: add %count() function
Add %count(), giving a count of the number of arguments.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-14 01:48:38 -08:00
H. Peter Anvin
d9b36e3c9c preproc: implement the %cond() and %sel() functions; fix memory leak
Implement the %cond() and %sel() functions that expand to a specific
one of the arguments. %cond(x,y,z) is basically a shorthand for
%sel(2-!(x),y,z) used when x is a boolean condition.

Fix a memory leak in %strcat and %strlen.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-13 22:59:16 -08:00
H. Peter Anvin
51ad8e1486 preproc: allow preprocessor function expansion to recurse
Allow preprocessor function expansion to recurse. Nearly all the
machinery for recursive smacros was already in place; this merely
activates it for the specific case of preprocessor functions. Making
it a general facility should be deferred to a later relese, though.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-13 20:34:04 -08:00
H. Peter Anvin
3fe5b3f5a1 preproc: distinguish between directives and functions
Some preprocessor functions have the same name as directives. In those
cases, they should be expanded as functions if and only if they are
followed by a left parenthesis. Although it is not inherently true that
either preprocessor functions require a paren nor that directives
cannot start with one, but it is true and will remain true for all
cases where there is a namespace collision.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:51:22 -08:00
H. Peter Anvin
359e21e773 preproc: implement %strlen as a preprocessor function
Implement a %strlen() preprocessor function, equivalent to the %strlen
directive.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:15:35 -08:00
H. Peter Anvin
4150848a7d preproc: implement %substr() and %tok() preprocessor functions
Implement preprocessor functions equivalent to the %substr and %deftok
directive.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:02:41 -08:00
H. Peter Anvin
913901e529 preproc: skip invalid advance in %substr
%substr contained a token skip to "skip expanded ID", which is
incorrect, as that has already been skipped at that point. It worked
anyway, accidentally, as this token would always be a whitespace token
-- but we then do skip_white() immediately thereafter.

Delete this to allow this code to be factored.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 18:28:11 -08:00
H. Peter Anvin
57fbd34d9f preproc: implement %str() and %strcat() functions
Add function equivalents of the %defstr and %strcat directives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 18:18:04 -08:00
Marco Vanotti
6224dd0b45 preproc: don't unmacro if macro cannot be found.
This commit adds a check to see if the macro that we want to unmacro exists.
A previous commit, introduced a check to see if the unmacro was undefining a macro being expanded, but that same check included a null pointer dereference if the macro to undefine did not exist.

The following code reproduced the issue:

```asm
%macro baz 0
  %unmacro F 0
%endmacro
baz
```

Compile with:
```shell
$ nasm -f elf64 -g -FDWARF -o tmp.o -werror file.asm
```

[hpa: adjusted code to match NASM style]

Fixes bug 3392761

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 16:30:49 -08:00
H. Peter Anvin
1a5fcb070a Revert "preproc: fix memory leak (and possibly CVEs?)"
This reverts commit 8fcc785f95b842694015e03d909a3131cbadbeb3.

This patch causes test a32offs.asm, and in general *any* use of the
"bits" macro, to totally fail.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 13:08:30 -08:00
H. Peter Anvin
b18e870d90 Merge remote-tracking branch 'ElyesH/typos' 2022-11-07 12:39:44 -08:00
H. Peter Anvin
3516258783 Merge remote-tracking branch 'ElyesH/comments' 2022-11-07 12:39:34 -08:00
H. Peter Anvin
c115cf4864 Merge remote-tracking branch 'igg0/strcat' 2022-11-07 12:36:24 -08:00
zhrf2020
8fcc785f95 preproc: fix memory leak (and possibly CVEs?)
case PP_ENDM:
    case PP_ENDMACRO:
        if (!(defining && defining->name)) {
            nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
            goto done;
        }
        mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
        defining->next = *mmhead;
        *mmhead = defining;
        defining = NULL;
        break;

The variable: mmacros has not been released, which will cause a memory
leak. Repair cve-2021-33450 cve-2021-33452 synchronously

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:34:04 -08:00
H. Peter Anvin
a131b1129c preproc: fix use of free() instead of nasm_free()
free() and nasm_free() are required to be compatible (as we may end up
having memory allocated on the heap by the C library), but that
doesn't mean we shouldn't use it whereever possible to allow for
better debugging.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392804
Reported-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:09:04 -08:00
H. Peter Anvin
7b8cd67d51 preproc: add warning for empty %{} construct
An empty %{} becomes % which is simply the arithmetic
operator. Although that is consistent, it might be surprising for
users, to issue a warning.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 12:01:00 -08:00
H. Peter Anvin
baae422e61 preproc: an empty %[] construct generates a null token, drop
%[] amounts to an empty token; this needs to be handled specially so
that it gets properly dropped.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392806
Reported-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 11:47:22 -08:00
Igor Glucksmann
349f19d402 Fixed optional comma separator in %strcat
Signed-off-by: "Glücksmann, Igor" <igor.glucksmann@avast.com>
2022-02-02 17:09:06 +01:00
Elyes HAOUAS
1dfbfa408d Fix some comments
Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr>
2022-01-09 18:27:53 +01:00
Elyes HAOUAS
cdf7ad02c2 Fix some typos
while on it, remove unneeded white spaces.

Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr>
2022-01-09 17:34:35 +01:00
Oleg Oshmyan
80a4e8e454 preproc: fix misparsing of << as right shift
Regression in commit 20e0d616dc954d567c8bf2c7e11cc5d6c10ac544.

Independently discovered and fixed by C. Masloch:
https://bugzilla.nasm.us/show_bug.cgi?id=3392747

Signed-off-by: Oleg Oshmyan <chortos@inbox.lv>
2021-09-21 23:30:53 +03:00
H. Peter Anvin (Intel)
5368e45794 preproc: fix pasting of TOKEN_HERE, TOKEN_BASE and TOKEN_QMARK
Make the pasting behavior of TOKEN_QMARK, TOKEN_HERE and TOKEN_BASE
match the NASM 2.15 behavior: ? is a keyword and pastes as an ID, $
and $$ are treated as operators (which doesn't seem to make much
sense, but it is the current legacy behavior.)

Reported-by: C. Masloch <pushbx@ulukai.org>
Bugzilla: https://bugzilla.nasm.us/show_bug.cgi?id=3392733
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2021-03-24 10:46:45 -07:00
Cyrill Gorcunov
79ac0d686d Merge branch 'nasm-2.15.xx'
* nasm-2.15.xx:
  preproc: prohibit unmacro while macro expansion
2020-12-31 16:50:25 +03:00
Igor Munkin
f95c7e983c preproc: prohibit unmacro while macro expansion
If macro is undefined while it's being expanded, use after free occurs,
since the MMacro instance is released, but it is still used to proceed
the expansion.

This change forbids macro undefinition: non-fatal error is raised and
the MMacro instance is not released if it is being processed by NASM
preprocessor.

Consider the following example:
| $ cat test.asm
| %macro m 0
| %unmacro m 0
| %endmacro
| m
| $ ./nasm test.asm
| test.asm:4: error: `%unmacro' can't undefine the macro being expanded
| test.asm:2: ... from macro `m' defined here

Fixes BR3392531 and BR3392716.

Signed-off-by: Igor Munkin <imun@cpan.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-12-31 16:42:31 +03:00
Cyrill Gorcunov
5e587fda09 preproc: use explicit addressing while clearing smacro
Better point out explicitly that SMacro::next member
is untouched, thus do not use SMacro::next and an array.

CID 1432925

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2020-10-05 00:09:18 +03:00
H. Peter Anvin (Intel)
6f01aca07c asm/preproc.c: in tokenize() advance pointer past ? for TOKEN_QMARK
When we process a TOKEN_QMARK we also need to advance p, in order to
get the proper start for the next token.

This fixes travis test br3392707.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-11 18:37:35 -07:00
H. Peter Anvin (Intel)
dab902cbdc preproc: when parsing an smacro template, don't mistake , for )
The operation of the ',' and ')' tokens are very similar, except for:

',' issues a error if the processed parameter is greedy;
')' sets the "done" variable.

The code would incorrectly set "done" for a ',' token. This fixes
travis test br3392711.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-04 14:35:49 -07:00
H. Peter Anvin (Intel)
ff97eb6f7e preproc: don't loop on a variable that doesn't advance
When pasting and stripping %+ and whitespace tokens, we either need to
set *nextp in the loop, or treat next as a separate variable and
update *nextp after the loop finishes. This implements the second
option.

This fixes travis test "amx".

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-04 14:09:37 -07:00
H. Peter Anvin (Intel)
a9ed8ced7c preproc: add %eval() function, add back %isn*()
Add the %eval() preprocessor function. It evaluates each of its
arguments like a number and expands to a comma-separated lists of the
evaluated arguments.

To support this, add the concept of "true varadic" macros, which are
only used internally. True varadic macros differ from greedy macros in
that the parameter list is still parsed as individual parameters and
provided to the expansion function. As this isn't meaningful for
user-defined macros, there is no way to specify it from a directive.

Add back the %isnfoo() functions. Although one could just as well write
!%isfoo(), it doesn't cost much to provide them, and might help avoid
programmer confusion.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-09-04 13:52:32 -07:00
H. Peter Anvin (Intel)
cc64861a61 Merge tag 'nasm-2.15.05'
NASM 2.15.05

Resolved Conflicts:
	asm/preproc.c
	version

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-08-31 12:23:36 -07:00
H. Peter Anvin (Intel)
2b4886afdd BR 3392715: fix %ifid with $ and $$
%ifid $ and %ifid $$ has traditionally been false, revert to that
behavior.

Reported-by: Mike Hommey <mh+anfz@glandium.org>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2020-08-27 11:43:08 -07:00