astrand/xclip

"xclip -o -sel clip" segfaults

clbr opened this issue · 2 comments

clbr commented

Today's git master.

$ valgrind xclip -o -sel clip
==4588== Memcheck, a memory error detector
==4588== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==4588== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==4588== Command: xclip -o -sel clip
==4588==
==4588== Conditional jump or move depends on uninitialised value(s)
==4588==    at 0x4C221C1: free (in /opt/valgrind/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4588==    by 0x40430F: main (in /usr/bin/xclip)
==4588==
==4588== Invalid free() / delete / delete[] / realloc()
==4588==    at 0x4C2220F: free (in /opt/valgrind/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4588==    by 0x40430F: main (in /usr/bin/xclip)
==4588==  Address 0x4016db is not stack'd, malloc'd or (recently) free'd
==4588==
xclip: Error: There is no owner for the CLIPBOARD selection
==4588==
==4588== HEAP SUMMARY:
==4588==     in use at exit: 91,389 bytes in 473 blocks
==4588==   total heap usage: 707 allocs, 235 frees, 104,478 bytes allocated
==4588==
==4588== LEAK SUMMARY:
==4588==    definitely lost: 0 bytes in 0 blocks
==4588==    indirectly lost: 0 bytes in 0 blocks
==4588==      possibly lost: 0 bytes in 0 blocks
==4588==    still reachable: 91,389 bytes in 473 blocks
==4588==         suppressed: 0 bytes in 0 blocks
==4588== Rerun with --leak-check=full to see details of leaked memory
==4588==
==4588== For counts of detected and suppressed errors, rerun with: -v
==4588== Use --track-origins=yes to see where uninitialised values come from
==4588== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 5 from 5)
clbr commented

The following patch fixes this.

diff --git a/xclip.c b/xclip.c
index 9c2d157..1d4eb4b 100644
--- a/xclip.c
+++ b/xclip.c
@@ -714,7 +714,7 @@ static int
 doOut(Window win)
 {
     Atom sel_type = None;
-    unsigned char *sel_buf;    /* buffer for selection data */
+    unsigned char *sel_buf = NULL;     /* buffer for selection data */
     unsigned long sel_len = 0; /* length of sel_buf */
     XEvent evt;                        /* X Event Structures */
     unsigned int context = XCLIB_XCOUT_NONE;

Thank you.