Subject: Re: [Suggestion] net/atm : for sprintf, need check the total write length whether larger than a page.

From: Chen Gang <[email-masked]>
Date: Wed, 05 Dec 2012 09:26:35 +0800

firstly, sorry for reply late (yesterday I have an annual leave for
some personal things).

  and thank you for your reply, too.

于 2012年12月04日 11:46, Chas Williams (CONTRACTOR) 写道:
>  static ssize_t show_address(struct device *cdev,
>  			    struct device_attribute *attr, char *buf)
>  {
> -	char *pos = buf;
>  	struct atm_dev *adev = to_atm_dev(cdev);
> -	int i;
> -
> -	for (i = 0; i < (ESI_LEN - 1); i++)
> -		pos += sprintf(pos, "%02x:", adev->esi[i]);
> -	pos += sprintf(pos, "%02x\n", adev->esi[i]);
>  
> -	return pos - buf;
> +	return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi);
>  }
>  

  "%p" seems print a pointer, not contents of pointer (is it correct ?)

  will it change the original display format to outside ?

>  static ssize_t show_atmaddress(struct device *cdev,
>  			       struct device_attribute *attr, char *buf)
>  {
>  	unsigned long flags;
> -	char *pos = buf;
>  	struct atm_dev *adev = to_atm_dev(cdev);
>  	struct atm_dev_addr *aaddr;
>  	int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
> -	int i, j;
> +	int i, j, count = 0;
>  
>  	spin_lock_irqsave(&adev->lock, flags);
>  	list_for_each_entry(aaddr, &adev->local, entry) {
>  		for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
>  			if (j == *fmt) {
> -				pos += sprintf(pos, ".");
> +				count += scnprintf(buf + count,
> +						   PAGE_SIZE - count, ".");
>  				++fmt;
>  				j = 0;
>  			}
> -			pos += sprintf(pos, "%02x",
> -				       aaddr->addr.sas_addr.prv[i]);
> +			count += scnprintf(buf + count,
> +					   PAGE_SIZE - count, "%02x",
> +					   aaddr->addr.sas_addr.prv[i]);
>  		}
> -		pos += sprintf(pos, "\n");
> +		count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
>  	}
>  	spin_unlock_irqrestore(&adev->lock, flags);
>  
> -	return pos - buf;
> +	return count;
>  }
>  

  need we judge whether count >= PAGE_SIZE ?

  these are my suggestions, maybe not correct.

  :-)

  thanks.

-- 
Chen Gang

Asianux Corporation