Search code examples
cnmap

Why have i++; i--; right after each other?


I was looking at the source code for nmap that was released in 1997 and I noticed this section of code that looks a little odd to me:

int i=0, j=0,start,end;
char *expr = strdup(origexpr);
ports = safe_malloc(65536 * sizeof(short));
i++;                                         /* <<<<<< */
i--;                                         /* <<<<<< */
for(;j < exlen; j++) 
  if (expr[j] != ' ') expr[i++] = expr[j]; 
expr[i] = '\0';

Why would you have i++; and then i--; right after each other? i is 0, then i++ turns i to 1. After that, i-- turns i to 0.

Link to original source code. Search for:

i++;
i--;

Can anyone explain what this is for?


Solution

  • This was a bug. These lines together result in i being unchanged, so they shouldn't have been there.

    The linked article that introduced nmap was published on September 1 1997. If you look at the SVN repository for nmap at https://svn.nmap.org/nmap, the initial revision checked in on February 10 1998 does not have those lines:

    int i=0, j=0,start,end;
    char *expr = strdup(origexpr);
    char *mem = expr;
    
    ports = safe_malloc(65536 * sizeof(short));
    for(;j < exlen; j++) 
      if (expr[j] != ' ') expr[i++] = expr[j]; 
    expr[i] = '\0';
    

    So this is something the author found and fixed between publishing the initial nmap source code and the initial checkin to SVN.