Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

nmap

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.


It's useless. It does absolutely nothing.

If I were to speculate it's probably the remains of some debugging code that was used during development.

I'm guessing that either one of i++ or i-- was introduced in one change and the other was introduced in another.

I have no way to find the point of introduction, though, because there was no revision history between the initial source release and the first SVN revision.


For a non-optimizing compiler, or one that recognized hardware side effects, the i++; i-- sequence would cause i to be read from memory, then re-written, regardless of the path taken through the for loop and nested if.

In parallel processing, sometimes compiler hacks are taken to ensure a code sequence uses its own local copies of variables rather than global copies.

Since the example is a code snippet, one cannot determine the compiler used, the expected operating system/hardware, nor whether this is in a code sequence/function that is possible to be executed as an independent thread.

In simpler systems, I've temporarily forced changes to variables to exercise the trap feature in a debugging environment. If that were the case, the author may have forgotten to remove the code when development was completed.


I will suggest you to check the updated code only. If you use (i = 2+1) right after that (i-1) that make no sense . The value of i remains unchanged. You can try it using any c or c++ compiler. or even in any other language it is same. Run the code in compiler.