xsel

Program for manipulating the X clipboard
git clone https://git.sinitax.com/kfish/xsel
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 3431440dbe7f02932efb51f98477fe9b00ab18ba
parent 48dcee4873d457760ec448a473c8a71aad741d7d
Author: conrad <conrad@9c49b5d1-7df3-0310-bce2-b7278e68f44c>
Date:   Mon, 21 Apr 2008 10:37:38 +0000

Apply patch from Yair K.:

1. strncpy is not guranteed to produce a null-terminated string (see man
page). The wrapper should make sure it's null-terminated.

2. It's odd there's a wrapper for malloc, but not for strdup. It can fail for
the same reasons since it also allocates memory (or because the argument is
NULL).

3. malloc(0) returns NULL on Linux. So if size==0, xs_malloc will exit
with "malloc error". Make sure instead that at least one byte is allocated.
At the moment, This can only happen if INCR returns *value == 0, which I'm not
sure is legal.

4. sigsetjmp expects a sigjmp_buf type arg, not a jmp_buf type arg.

5. In some cases an unsigned long value is assigned to an int. e.g.
in process_multiple (i = md->index. md->index is unsigned long).
Make sure everything related is unsigned long as well.
Same at get_append_property (*alloc = *offset + length). incr_alloc, incr_xfer
were turned to unsigned long in wait_incr_selection to fix this.

This has only been moderately tested.


git-svn-id: http://svn.kfish.org/xsel/trunk@223 9c49b5d1-7df3-0310-bce2-b7278e68f44c

Diffstat:
Mxsel.c | 44++++++++++++++++++++++++++++++++------------
1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/xsel.c b/xsel.c @@ -249,9 +249,9 @@ get_atom_name (Atom atom) */ static void debug_property (int level, Window requestor, Atom property, Atom target, - int length) + unsigned long length) { - print_debug (level, "Got window property: requestor 0x%x, property 0x%x, target 0x%x %s, length %d bytes", requestor, property, target, get_atom_name (target), length); + print_debug (level, "Got window property: requestor 0x%x, property 0x%x, target 0x%x %s, length %ld bytes", requestor, property, target, get_atom_name (target), length); } /* @@ -277,7 +277,18 @@ xs_malloc (size_t size) * * strdup wrapper for unsigned char * */ -#define xs_strdup(s) ((unsigned char *) strdup ((const char *)s)) +#define xs_strdup(s) ((unsigned char *) _xs_strdup ((const char *)s)) +static char * _xs_strdup (const char * s) +{ + char * ret; + + if (s == NULL) return NULL; + if ((ret = strdup(s)) == NULL) { + exit_err ("strdup error"); + } + + return ret; +} /* * xs_strlen (s) @@ -291,7 +302,16 @@ xs_malloc (size_t size) * * strncpy wrapper for unsigned char * */ -#define xs_strncpy(dest,src,n) (strncpy ((char *)dest, (const char *)src, n)) +#define xs_strncpy(dest,s,n) (_xs_strncpy ((char *)dest, (const char *)s, n)) +static char * +_xs_strncpy (char * dest, const char * src, size_t n) +{ + if (n > 0) { + strncpy (dest, src, n); + dest[n-1] = '\0'; + } + return dest; +} /* * get_homedir () @@ -328,7 +348,7 @@ gotpw: exit_err ("error retrieving passwd entry"); } - homedir = strdup (pw->pw_dir); + homedir = _xs_strdup (pw->pw_dir); return homedir; } @@ -463,7 +483,7 @@ get_timestamp (void) */ /* The jmp_buf to longjmp out of the signal handler */ -static jmp_buf env_alrm; +static sigjmp_buf env_alrm; /* * alarm_handler (sig) @@ -489,7 +509,7 @@ alarm_handler (int sig) */ static Bool get_append_property (XSelectionEvent * xsl, unsigned char ** buffer, - int * offset, int * alloc) + unsigned long * offset, unsigned long * alloc) { unsigned char * ptr; Atom target; @@ -514,7 +534,7 @@ get_append_property (XSelectionEvent * xsl, unsigned char ** buffer, print_debug (D_TRACE, "Got zero length property; end of INCR transfer"); return False; } else if (format == 8) { - if ((unsigned long)*offset + length > (unsigned long)*alloc) { + if (*offset + length > *alloc) { *alloc = *offset + length; if ((*buffer = realloc (*buffer, *alloc)) == NULL) { exit_err ("realloc error"); @@ -543,7 +563,7 @@ wait_incr_selection (Atom selection, XSelectionEvent * xsl, int init_alloc) { XEvent event; unsigned char * incr_base = NULL, * incr_ptr = NULL; - int incr_alloc = 0, incr_xfer = 0; + unsigned long incr_alloc = 0, incr_xfer = 0; Bool wait_prop = True; print_debug (D_TRACE, "Initialising incremental retrieval of at least %d bytes\n", init_alloc); @@ -1368,7 +1388,7 @@ static HandleResult process_multiple (MultTrack * mt, Bool do_parent) { HandleResult retval = HANDLE_OK; - int i; + unsigned long i; if (!mt) return retval; @@ -1864,7 +1884,7 @@ expand_argv(int * argc, char **argv[]) } } else { /* Simply copy the argument pointer to new_argv */ - new_argv[new_i++] = strdup ((*argv)[i]); + new_argv[new_i++] = _xs_strdup ((*argv)[i]); } } @@ -1981,7 +2001,7 @@ main(int argc, char *argv[]) dont_output = True; } else if (OPT("--logfile") || OPT("-l")) { i++; if (i >= argc) goto usage_err; - strncpy (logfile, argv[i], MAXFNAME); + _xs_strncpy (logfile, argv[i], MAXFNAME); } else { goto usage_err; }