[comp.lang.perl] bug in pack

phillips@cs.ubc.ca (George Phillips) (03/05/90)

This bug applies to perl 3.0 up to patchlevel 12.  When dealing with
ascii strings ("a" or "A"), pack will call str_ncat with a -ve length
if the source string is longer than the length specified by the
template.  To fix this, just don't call str_ncat with a -ve length.
Here's a patch to "doarg.c" which takes one way around the problem:

*** doarg.c.larry	Sun Mar  4 14:39:03 1990
--- doarg.c	Sun Mar  4 14:51:53 1990
***************
*** 395,416 ****
  	    aptr = str_get(fromstr);
  	    if (fromstr->str_cur > len)
  		str_ncat(str,aptr,len);
! 	    else
  		str_ncat(str,aptr,fromstr->str_cur);
! 	    len -= fromstr->str_cur;
! 	    if (datumtype == 'A') {
! 		while (len >= 10) {
! 		    str_ncat(str,space10,10);
! 		    len -= 10;
  		}
! 		str_ncat(str,space10,len);
! 	    }
! 	    else {
! 		while (len >= 10) {
! 		    str_ncat(str,null10,10);
! 		    len -= 10;
  		}
- 		str_ncat(str,null10,len);
  	    }
  	    break;
  	case 'C':
--- 395,417 ----
  	    aptr = str_get(fromstr);
  	    if (fromstr->str_cur > len)
  		str_ncat(str,aptr,len);
! 	    else {
  		str_ncat(str,aptr,fromstr->str_cur);
! 		len -= fromstr->str_cur;
! 		if (datumtype == 'A') {
! 		    while (len >= 10) {
! 			str_ncat(str,space10,10);
! 			len -= 10;
! 		    }
! 		    str_ncat(str,space10,len);
  		}
! 		else {
! 		    while (len >= 10) {
! 			str_ncat(str,null10,10);
! 			len -= 10;
! 		    }
! 		    str_ncat(str,null10,len);
  		}
  	    }
  	    break;
  	case 'C':

print pack("a24", "Just another Perl hacker and sometimes patcher") . "\n";

George Phillips phillips@cs.ubc.ca {alberta,uw-beaver,uunet}!ubc-cs!phillips