Changeset 223

Show
Ignore:
Timestamp:
04/21/08 19:37:38 (9 months ago)
Author:
conrad
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • xsel/trunk/xsel.c

    r221 r223  
    250250static void 
    251251debug_property (int level, Window requestor, Atom property, Atom target, 
    252                 int length) 
    253 { 
    254   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); 
     252                unsigned long length) 
     253{ 
     254  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); 
    255255} 
    256256 
     
    278278 * strdup wrapper for unsigned char * 
    279279 */ 
    280 #define xs_strdup(s) ((unsigned char *) strdup ((const char *)s)) 
     280#define xs_strdup(s) ((unsigned char *) _xs_strdup ((const char *)s)) 
     281static char * _xs_strdup (const char * s) 
     282
     283  char * ret; 
     284 
     285  if (s == NULL) return NULL; 
     286  if ((ret = strdup(s)) == NULL) { 
     287    exit_err ("strdup error"); 
     288  } 
     289 
     290  return ret;  
     291
    281292 
    282293/* 
     
    292303 * strncpy wrapper for unsigned char * 
    293304 */ 
    294 #define xs_strncpy(dest,src,n) (strncpy ((char *)dest, (const char *)src, n)) 
     305#define xs_strncpy(dest,s,n) (_xs_strncpy ((char *)dest, (const char *)s, n)) 
     306static char * 
     307_xs_strncpy (char * dest, const char * src, size_t n) 
     308
     309  if (n > 0) { 
     310    strncpy (dest, src, n); 
     311    dest[n-1] = '\0'; 
     312  } 
     313  return dest; 
     314
    295315 
    296316/* 
     
    329349  } 
    330350 
    331   homedir = strdup (pw->pw_dir); 
     351  homedir = _xs_strdup (pw->pw_dir); 
    332352 
    333353  return homedir; 
     
    464484 
    465485/* The jmp_buf to longjmp out of the signal handler */ 
    466 static jmp_buf env_alrm; 
     486static sigjmp_buf env_alrm; 
    467487 
    468488/* 
     
    490510static Bool 
    491511get_append_property (XSelectionEvent * xsl, unsigned char ** buffer, 
    492                      int * offset, int * alloc) 
     512                     unsigned long * offset, unsigned long * alloc) 
    493513{ 
    494514  unsigned char * ptr; 
     
    515535    return False; 
    516536  } else if (format == 8) { 
    517     if ((unsigned long)*offset + length > (unsigned long)*alloc) { 
     537    if (*offset + length > *alloc) { 
    518538      *alloc = *offset + length; 
    519539      if ((*buffer = realloc (*buffer, *alloc)) == NULL) { 
     
    544564  XEvent event; 
    545565  unsigned char * incr_base = NULL, * incr_ptr = NULL; 
    546   int incr_alloc = 0, incr_xfer = 0; 
     566  unsigned long incr_alloc = 0, incr_xfer = 0; 
    547567  Bool wait_prop = True; 
    548568 
     
    13691389{ 
    13701390  HandleResult retval = HANDLE_OK; 
    1371   int i; 
     1391  unsigned long i; 
    13721392 
    13731393  if (!mt) return retval; 
     
    18651885    } else { 
    18661886      /* Simply copy the argument pointer to new_argv */ 
    1867       new_argv[new_i++] = strdup ((*argv)[i]); 
     1887      new_argv[new_i++] = _xs_strdup ((*argv)[i]); 
    18681888    } 
    18691889  } 
     
    19822002    } else if (OPT("--logfile") || OPT("-l")) { 
    19832003      i++; if (i >= argc) goto usage_err; 
    1984       strncpy (logfile, argv[i], MAXFNAME); 
     2004      _xs_strncpy (logfile, argv[i], MAXFNAME); 
    19852005    } else { 
    19862006      goto usage_err;