[hackers] [scc] Fix cut/paste in macro expansion || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Sat, 18 Jul 2015 07:32:10 +0200 (CEST)

commit 36f259ae86ef2ab10ef3beeb6bcda9cdec68acfd
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sat Jul 18 00:04:03 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Sat Jul 18 07:22:29 2015 +0200

    Fix cut/paste in macro expansion
    
    The replacement of the macro invocation by the macro expansion
    was wrong, mainly because we must know left length, rigth length,
    expansion length and invocation length, but we were using only
    one length.

diff --git a/cc1/cpp.c b/cc1/cpp.c
index b904e5a..30a3b20 100644
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
_AT_@ -182,22 +182,22 @@ expansion_too_long:
 bool
 expand(char *begin, Symbol *sym)
 {
- size_t len;
+ size_t total, elen, rlen, llen, ilen;
         int n, r;
         char *s = sym->u.s;
         char *arglist[NR_MACROARG], arguments[INPUTSIZ], buffer[BUFSIZE];
 
- fprintf(stderr, "macro '%s':%s\n", sym->name, sym->u.s);
         if (sym == symfile) {
                 sprintf(buffer, "\"%s\"", input->fname);
                 goto print_subs;
         }
         if (sym == symline) {
- r = snprintf(buffer, sizeof(buffer), "%s", input->line);
+ r = snprintf(buffer, sizeof(buffer), "%d", input->nline);
                 if(r == -1 || (size_t)r >= sizeof(buffer)) {
                         error("expansion of macro \"%s\" is too long", sym->name);
                         return 0;
                 }
+ fprintf(stderr, "Expansion of line '%s'\n", buffer);
                 goto print_subs;
         }
 
_AT_@ -211,17 +211,22 @@ expand(char *begin, Symbol *sym)
 
 print_subs:
         fprintf(stderr, "macro '%s' expanded to :'%s'\n", macroname, buffer);
- len = strlen(buffer);
+ elen = strlen(buffer); /* expansion lentgh */
+ rlen = strlen(input->p); /* rigth length */
+ llen = begin - input->line; /* left length */
+ ilen = input->p - begin; /* invocation length */
+ total = llen + elen + rlen;
 
- if (begin - input->line + len >= LINESIZ-1)
+ if (total >= LINESIZ)
                 error("macro expansion too long");
 
         /* cut macro invocation */
- memmove(begin, input->p, input->p - begin);
+ memmove(begin, begin + ilen, rlen);
 
         /* paste macro expansion */
- memmove(begin + len, begin, len);
- memcpy(begin, buffer, len);
+ memmove(begin + elen, begin, rlen);
+ memcpy(begin, buffer, elen);
+ input->line[total] = '\0';
 
         input->p = input->begin = begin;
 
Received on Sat Jul 18 2015 - 07:32:10 CEST

This archive was generated by hypermail 2.3.0 : Sat Jul 18 2015 - 07:36:27 CEST