SYMPHONY icon indicating copy to clipboard operation
SYMPHONY copied to clipboard

undefined behaviour in tm_func.c code

Open rmok-git opened this issue 4 months ago • 2 comments

From man sprintf:

CAVEATS
       Some programs imprudently rely on code such as the following

           sprintf(buf, "%s some further text", buf);

       to append text to buf.  However, the standards explicitly note that the
       results are undefined if source and destination  buffers  overlap  when
       calling  sprintf(), snprintf(), vsprintf(), and vsnprintf().  Depending
       on the version of gcc(1) used, and the compiler options employed, calls
       such as the above will not produce the expected results.

       The glibc implementation of the functions  snprintf()  and  vsnprintf()
       conforms  to  the  C99  standard,  that is, behaves as described above,
       since glibc 2.1.  Until glibc 2.0.6, they would return -1 when the out‐
       put was truncated.

And here is what I see with grep -n ', reason' tm_func.c | grep 'reason,'

1473:          sprintf(reason,"%s %c", reason, branch_dir);
1475:          sprintf(reason,"%s %c %f", reason, branch_dir,
1843:    sprintf(reason,"%s %i", reason, node->bc_index+1);
1845:       sprintf(reason, "%s %i", reason, node->parent->bc_index+1);
1860:    sprintf(reason,"%s %c %s", reason, branch_dir, "\n");
1864:    sprintf(reason, "%s %i", reason, node->bc_index+1);
1866:       sprintf(reason,"%s %i", reason, node->parent->bc_index+1);
1880:    sprintf(reason,"%s %c %s", reason, branch_dir, "\n");
1894:    sprintf(reason,"%s %c %f\n", reason, branch_dir, tm->ub);

I catch this with gcc -Wall

rmok-git avatar Aug 14 '25 08:08 rmok-git

Thanks for pointing this out. Those lines of code were probably all written 30+ years ago. To my knowledge, it's never been an issue, but a PR would be welcome!

tkralphs avatar Aug 14 '25 13:08 tkralphs

I don't really have a test case to cover this. I create patch.txt using 'git format-patch', not sure if you will accept it.

patch.txt

rmok-git avatar Aug 29 '25 07:08 rmok-git