[freetds] FW: dblib patch

ZIGLIO, Frediano, VF-IT Frediano.Ziglio at vodafone.com
Fri Jul 1 11:09:41 EDT 2005


> > 
> > static int rtrim(char *istr, int ilen) {
> >         char *t; int olen = ilen;
> > 
> >         for (t = istr + (ilen - 1); *t == ' '; t--) {
> >                 *t = '\0'; olen--;
> >         } return olen;
> > }
> > 
> > this contain a buffer underflow...
> > 
> > Perhaps it would better to rewrite like
> > 
> > static int rtrim(char *istr, int ilen) {
> >         char *t;
> > 
> >         for (t = istr + ilen; --t > istr && *t == ' '; ) {
> >                 *t = '\0'; --ilen;
> >         } return ilen;
> > }
> > 
> > (it solve also '' problem)
> 
> I like this better:
> 
> static int
> rtrim(char *str, int len)
> {
> 	char *t;
> 
> 	for (t = str + --len; t > str && *t == ' '; t--) {
> 		*t = '\0';
> 	}
> 	return t - str;
> }
> 
> Thanks for the patch Bill.  We distinguish, don't we, between char and
> varchar?  char columns shouldn't be trimmed.  At least, 
> Microsoft's bcp
> doesn't trim them.  
> 

last line should be 

  return t - str + 1;

a mix of the two

  for (t = istr + ilen; --t > istr && *t == ' '; )
    *t = '\0';
  return t - str + 1;

About char/varchar is the same, server fill needed spaces if necessary.

freddy77


More information about the FreeTDS mailing list