On Wed, Apr 16, 2014 at 6:47 PM, Tito <[email protected]> wrote:
> Hi,
> while reading some interesting stuff about memset being optimized
> away by compilers if the variable is not read after the memset call
> I recalled there was something similar in libbb/obscure.c file:
>
> static int string_checker(const char *p1, const char *p2)
> {
> int size, i;
> /* check string */
> int ret = string_checker_helper(p1, p2);
> /* make our own copy */
> char *p = xstrdup(p1);
>
> /* reverse string */
> i = size = strlen(p1);
> while (--i >= 0) {
> *p++ = p1[i];
> }
> p -= size; /* restore pointer */
>
> /* check reversed string */
> ret |= string_checker_helper(p, p2);
>
> /* clean up */
> memset(p, 0, size);
>
> free(p);
>
> return ret;
> }
$ make libbb/obscure.s
and I see the memset (inlined by compiler):
cld
xorl %eax, %eax # tmp76
movl %ebx, %edi # p.101,
movl %esi, %ecx # D.7349, D.7349
rep
stosb
Perhaps because compiler doesn't know that free(p) doesn't use
the contents of *p.
>
> - /* clean up */
> - memset(p, 0, size);
> + /* clean up, don't use memset as it is optimized away by compiler */
> + /*memset(p, 0, size);*/
> + nuke_str(p);
> free(p);
This may be unnecessary wrt correctness (memset isn't eliminated),
but it is also _fewer bytes of code_!
Applied to git, thanks!
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox