Commit Graph

216 Commits

Author SHA1 Message Date
Cyrill Gorcunov
4e1d5ab0cf preproc.: Fix NULL dereference on broken %strlen argument
Under particular circumstances %strlen may cause SIGSEG. A typical
example is %strlen with nonexistent macro argument.

[ Testcase test/strlen.asm ]

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-23 18:51:51 +04:00
H. Peter Anvin
077fb93d2b preproc: allow non-identifier character in environment variables
Allow non-identifier characters in the name of environment variables,
by surrounding them with string quotes (subject to ordinary
string-quoting rules.)

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2010-07-20 14:56:30 -07:00
H. Peter Anvin
6d9b2b59b5 preproc: add %ifenv
Add %ifenv to test for the presence of an environment variable.  The
environment variable can, but does not have to be, prefixed with %!.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2010-07-13 12:00:58 -07:00
H. Peter Anvin
5b00bf4d49 BR3028880: Revert to nonfatal, better error message, cleanup
Revert to issuing a nonfatal error (it makes no sense to make it a
fatal error, but it probably makes sense for it to be an error instead
of a warning, especially since a lot of prior versions would crash and
apparently noone noticed.)  We might have to revisit this based on
user requirements, and/or provide a method for the user to detect an
existing environment variable (%ifenv?).

Issue a better error message, indicating the nature of the failure.

Simplify the code by just updating the string in "p".

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2010-07-13 11:46:37 -07:00
Cyrill Gorcunov
41c5c6c36d BR3028880: Make nonexistent environment variable being fatal error
Frank suggested to just print out an error if environment
variable is not there. Agreed.

Suggested-by: Frank Kotler <fbkotler@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-13 21:17:49 +04:00
Cyrill Gorcunov
385d3e9c53 BR3028880: Fix NULL dereference on nonexistent environment variable
Frank reported we hit NULL dereference on nonexistent
environment variables. Fix it by leaving empty string
in text field of such token and yielding warning.

Reported-by: Frank Kotler <fbkotler@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-13 21:17:24 +04:00
Cyrill Gorcunov
c6360a757b tokenize: Fix wrong string index in indirect strings
At moment of calling the nasm_skip_string the string pointer
is already incremented which makes tokenize fail on correct
indirect strings.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-13 21:17:16 +04:00
Cyrill Gorcunov
15bdc51187 preproc: Extract reading line from predefined macros from read_line
It makes read_line less complex

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-13 21:17:10 +04:00
Cyrill Gorcunov
984279b1dd BR3026808: Assign to local preprocessor variable does not work in 2.09
The commits

20a94ad7fe
29c96651de
13dbfad76b
6f5f7ef417
ddd08c3ccc

seems to do the tricks we need. Eventually
get rid of commented "case".

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-13 21:13:24 +04:00
Cyrill Gorcunov
adabc1576b preproc.c: Fix NULL deref on token pasting
In case if there is a whitespace before
'paste' token we may reach NULL dereference
in strlen since paste_head will point to
TOK_WHITESPACE. Fix it.

[test: paste.asm]

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-07-10 02:14:24 +04:00
H. Peter Anvin
20a94ad7fe preproc: don't paste TOK_PREPROC_ID
Trying to deal with bug reports 3005117 and 3026808: don't paste after
TOK_PREPROC_ID.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2010-07-08 11:52:57 -07:00
H. Peter Anvin
29c96651de Revert "BR3005117: Expland local single macro before pasting tokens"
This reverts commit ec88c1beac.

Revert due to BR 3026808.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2010-07-08 08:35:32 -07:00
H. Peter Anvin
13dbfad76b Revert "expand_mmac_params: Don't forget to handle TOK_OTHER"
This reverts commit 51fd86e0fe.

Revert due to BR 3026808.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2010-07-08 08:34:41 -07:00
H. Peter Anvin
6f5f7ef417 Revert "expand_mmac_params: Expand local single macros unconditionally"
This reverts commit 1f6741fc78.

Revert due to BR 3026808.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2010-07-08 08:33:47 -07:00
H. Peter Anvin
ddd08c3ccc Revert "expand_mmac_params: Expand local single macros unconditionally"
This reverts commit 985d880c15.

Revert due to BR 3026808.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2010-07-08 08:33:14 -07:00
Cyrill Gorcunov
efb358339d preproc.c: Get rid of signed/unsigned comparison warning
We need mac->nparam being explicictly int'fied otherwise
compiler issue a warning. Note that we might have been
using unsigned int but it would break an ability to pass
negative indices.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-20 01:52:19 +04:00
Cyrill Gorcunov
985d880c15 expand_mmac_params: Expand local single macros unconditionally
Peter proposed to expand local single macros unconditionally.
This should not hurt but give us more cleaner code in result.

Reported-by: "H. Peter Anvin" <hpa@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-08 18:45:06 +04:00
Cyrill Gorcunov
1f6741fc78 expand_mmac_params: Expand local single macros unconditionally
Peter proposed to expand local single macros unconditionally.
This should not hurt but give us more cleaner code in result.

Reported-by: "H. Peter Anvin" <hpa@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-08 18:39:56 +04:00
Cyrill Gorcunov
2f40375077 expand_mmac_params_range: Simplify condition
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-05 10:47:10 +04:00
Cyrill Gorcunov
c57eb51527 preproc.c: Fix argument indices checking in parameters range
Otherwise %{-1:-1} fails.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-05 01:58:10 +04:00
Cyrill Gorcunov
c29404d7ba preproc.c: Introduce macros parameters range expansion
Introduce an ability to expand multi-line macros parameters in
a range/sequence manner.

For this purpose a special form is introduced %{x:y} which means to
expand %{x:y} to %{x},%{x+1},%{x+2},...,%{y}.

Both arguments could be negative or positive but MUST NOT be zero.

The arguments take into account possible %rotate as well.

Note that unlike the approach implemented in yasm we refer :-1 as
_last_ argument passed to a macro call, this makes possible to refer
the last element from macro via record as %{-1:-1} which could be
a convenient trick.

Also you can refer the argument in reverse order, ie it's legitime
to write %{5:4}, or even to reverse the all arguments %{-1:1}.

An example

 |
 | %macro mpar 1-*
 |     db %{1:-2}
 | %endmacro
 |
 | mpar 1,2,3,4,5,6

in result we'll get the sequence of 1,2,3,4,5

Reported-by: nasm64developer <nasm64developer@users.sf.net>
Inspired-by: Mathieu Monnier <mathieu.monnier@polytechnique.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-05 01:50:59 +04:00
Cyrill Gorcunov
ca61119a01 expand_mmac_params: Format condition
It's much easier to read aligned

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-04 22:16:54 +04:00
Cyrill Gorcunov
a731924978 nasmlib: Rename elements() macro to ARRAY_SIZE
ARRAY_SIZE is a well known name pointing out that
we're dealing with array in macro argument.

Also to be on a safe side prefix_name helper should
check the index been in bounds more precisely.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-03 23:17:21 +04:00
Cyrill Gorcunov
3b4e86b1dd preproc.c: Use list_ helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-02 20:31:53 +04:00
Cyrill Gorcunov
51fd86e0fe expand_mmac_params: Don't forget to handle TOK_OTHER
TOK_OTHER is legitime to follow TOK_PREPROC_ID so don't forget to handle it as well.

[ An addition to commit ec88c1beac ]

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-02 20:31:46 +04:00
Cyrill Gorcunov
ec88c1beac BR3005117: Expland local single macro before pasting tokens
When we have switched to unified token pasting code we loose
backward compatibility. Restore it.

Note that new code MUST not expluatate this facility but rather
use paste macro %+ explicitly.

N.B. this patch is probably the candidate for revert, though
to give it a chance I commit it.

Reported-by: Alexey Dokuchaev
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-06-02 01:05:37 +04:00
Cyrill Gorcunov
367d59e272 expand_mmacro: Use list helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-04-10 00:13:57 +04:00
Cyrill Gorcunov
f32ed14ebd detoken: Use list_for_each helpers
And a few style nits.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-04-10 00:13:53 +04:00
Cyrill Gorcunov
ed4a805b0a expand_smacro: stylish nits
- no need to split functions even if it a bit longer
  then 80 characters, it becomes hard to read it

- initialize "thead" before "tail" is more natural

- use more simple while() instead of for() with a
  long initializer

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-04-10 00:13:49 +04:00
Victor van den Elzen
35eb2ea2ee preproc.c: Fix regression introduced in bebf0d215 2010-03-10 22:33:48 +01:00
Cyrill Gorcunov
f09116f445 preproc.c: Turn off rmacro,exitmacro support
We've a problem in supporting [i]rmacro, exitmacro
facilities at moment.

In a sake of not holding new NASM release any longer these
directives are just marked as being "forbidden".

This allow us to not squash much changes in current source
code base but remain on a safe side same time.

Reviewed-by: Keith Kanios <keith@kanios.net>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-28 12:24:20 +03:00
Cyrill Gorcunov
8e48edb68a preproc.c: Eliminate parasite comment on PP_STACKSIZE
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-17 00:11:07 +03:00
Cyrill Gorcunov
accda195a3 preproc.c: Fix tab\space mess
It's really hard to read the code which is
terribly messed in tabs\spaces. Fix it all
at once. It's dirty work but has to be done
once.

No change on binary level.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-17 00:11:01 +03:00
Cyrill Gorcunov
a327c65d28 preproc.c: Fix SIGSEV on missed %endm
In case if EOF is reached (or due to any
other case pp_cleanup is being called) we
do free "defining" macro but forgot to set
pointer to NULL itself which leads to attempt
to free memory again for this macro on further
pp_cleanup calls.
2010-02-14 17:27:10 +03:00
Cyrill Gorcunov
bebf0d2157 preproc.c: Fix NULL dereference on package absence
If package can't be retrieved we should not attempt
to dereference NULL'ed pointer which leads to segmentation
fault.

Reported-by: Serge
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-12 22:00:18 +03:00
Cyrill Gorcunov
c56d9ad350 expand_smacro: Don't search for ID in global context
The corner case is the code like

%define foo 1
%push bar
%$foo:
%pop

for which v2.07 ends up with "foo = 1" while 0.98.39
issue an error.

hpa said that ideally we may need to create a context
structure for the global context but this seems to be
too agressive for 2.08.

Based on patch from nasm64developer

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-11 21:28:41 +03:00
Cyrill Gorcunov
9b66d8e4c3 Use ALIGN helper
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-11 21:28:30 +03:00
Per Jessen
53252e0cc3 preproc.c: Use 16 byte offset in "flat64" stack model
The first argument passed on stack with "flat64" stack model
(stack frame with base pointer) should be pointed by
[rbp + 16].

Signed-off-by: Per Jessen <per@computer.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2010-02-11 21:28:22 +03:00
Cyrill Gorcunov
bd38c8f231 preproc.c: expand_smacro -- break endless loop for interminable macro recursion
Frank reported:
|
| From the "expert questions" forum comes this:
|
| ---------------------
| By: jasper_neumann
|
| How can I delegate %undef?
|
| In the example below the assembler (called with "nasm.exe -t -f rdf q.asm")
| bemoans my code, displays
|
| "q.asm:19: error: interminable macro recursion"
|
| and hangs.
|
| q.asm
| -----
| bits 32
| CPU P4
|
| %macro my_def 2
|  %xdefine %1 esp+%2
| %endmacro
|
| %macro my_undef 1
|  %undef %1
| %endmacro
|
| global check_it
| check_it:
|  my_def x,4
|  mov eax,[x]
|  my_undef x
|
|  my_def x,8
|  add eax,[x]
|  my_undef x
|  ret
|

So in case of interminable macro recursion we should break
the expansion procedure that way to not return back and start
expand macro again.

This address a part of the original problem.
Nasm64developer pointed out:
|
| Btw, after you manage to fix this recursion problem, the code
| in question still faces the same fundamental issue -- the arg
| to the my_undef invocations (i.e. x) gets expanded first; thus
| the %undef inside the macro sees esp+4 and esp+8 instead
| of x, and fails. What you'd need is a means to prevent the ex-
| pansion -- look for e.g. %# in 4.1.4 of the manual.txt which is
| attached to SF #1842438; it implements exactly that -- I once
| filed SF #829879 for this feature.
|

In turn Keith Kanios said:
|
| Anon is also correct in that we would need a special directive to instruct
| the delay of macro expansion, although I don't see this as critical or even
| high priority at the moment. The intermediate solution for this is, don't
| use indirection if it is not needed... an inline %undef should be
| sufficient.
|

Reported-by: Frank Kotler <fbkotler@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Reviewed-by: Keith Kanios <keith@kanios.net>
2009-11-21 11:11:29 +03:00
Keith Kanios
3c0d91f3d5 preproc.c: fix exitmacro to account for if/endif blocks 2009-10-25 13:28:03 -05:00
Cyrill Gorcunov
f66ac7deb0 preproc.c: use string helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2009-10-13 19:42:10 +04:00
Cyrill Gorcunov
10b4a18d7f preproc.c - remove redundant assignment
There is no need for tline assignment right
before it'll be assigned again in for() cycle.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2009-07-20 17:59:00 +04:00
H. Peter Anvin
dbb640b846 Drop efunc and evalfunc as input to the preprocessor
We only ever invoked the preprocessor with fixed values for efunc and
evalfunc, so call nasm_error() and evaluate() directly.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-07-18 18:57:16 -07:00
H. Peter Anvin
89cee57ad0 preproc: style cleanups
Fix indentation, space between if and (, and prefer (foo) and (!foo)
to (foo != NULL) and (foo == NULL).

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-07-15 09:16:54 -04:00
H. Peter Anvin
db8f96e0e6 preproc: formatting cleanups
Fix inconsistent indentation, and legacy use of partial directives
instead of simply pp_directives[i].

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-07-15 09:07:29 -04:00
Keith Kanios
d37a38c359 pptok: change %rimacro to %irmacro
preproc: change PP_RIMACRO to PP_IRMACRO
nasmdoc: add entries for %[i]deftok and %[i]rmacro
2009-07-14 21:00:40 -05:00
H. Peter Anvin
cd0943e277 preproc: don't pass an enum to %s
Need to use pp_directives[directive], not the plain directive, which
is an enum.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-07-14 15:17:11 -04:00
H. Peter Anvin
f9c9a6717c preproc: generic solution for error on NUL characters in string
Strings returned by nasm_unquote() can contain NUL characters, which
will not be legal if then used as a C string.  Create a general
function which looks for NUL characters in the string and issues an
error if they are found.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-07-14 15:14:05 -04:00
H. Peter Anvin
2f55bdaaca preproc: reject NUL characters in %deftok directives
We could either error out or convert them to whitespace; this
implements the error option.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2009-07-14 15:04:04 -04:00
Keith Kanios
b83fd0b947 preproc: add %[i]deftok support
pptok: add %deftok and %ideftok preprocessor directives
2009-07-14 01:04:12 -05:00