Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755432AbdCGPFJ (ORCPT ); Tue, 7 Mar 2017 10:05:09 -0500 Received: from bh-25.webhostbox.net ([208.91.199.152]:51470 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754517AbdCGPFB (ORCPT ); Tue, 7 Mar 2017 10:05:01 -0500 Subject: Re: Question about hwmon_attr_show_string To: Jean Delvare References: <201703030133.01363.PeterHuewe@gmx.de> <201703062148.36101.PeterHuewe@gmx.de> <20170306234755.GA16512@roeck-us.net> <20170307100846.0487135b@endymion> Cc: =?UTF-8?Q?Peter_H=c3=bcwe?= , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org From: Guenter Roeck Message-ID: <333a9a55-e6d3-97ed-6223-6d09cb5d3c03@roeck-us.net> Date: Tue, 7 Mar 2017 06:16:15 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: <20170307100846.0487135b@endymion> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Authenticated_sender: linux@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: linux@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: linux@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2399 Lines: 65 On 03/07/2017 01:08 AM, Jean Delvare wrote: > Hi Guenter, > > On Mon, 6 Mar 2017 15:47:55 -0800, Guenter Roeck wrote: >> On Mon, Mar 06, 2017 at 09:48:35PM +0100, Peter Hüwe wrote: >>> Hi Guenter, >>> >>> I was wondering whether there was a particular reason why >>> hwmon_attr_show_string passes only an "empty" pointer(pointer) to the ops- >>>> read_string function rather than the buffer itself? >>> >>> Wouldn't this mean that in ops->read_string I'd have to reserve some space for >>> the value on the heap (and taking care to free it somewhere, since returning >>> an address on the stack is bad idea), instead of calling sprintf(buf, "%s\n", >>> s) directly? >>> >>> With the current implementation I have to sprintf it into my local buffer and >>> you sprintf it again into the final buffer. >> >> The idea was that the called code would return a pointer to a constant string, >> ie one that isn't changing from call to call. > > In that case, what about the following change? > > Subject: hwmon: Constify str parameter of hwmon_ops->read_string > > The read_string callback is supposed to retrieve a pointer to a > constant string. > > Signed-off-by: Jean Delvare Makes sense. I'll add it to -next. Thanks, Guenter > --- > drivers/hwmon/hwmon.c | 2 +- > include/linux/hwmon.h | 2 +- > 2 files changed, 2 insertions(+), 2 deletions(-) > > --- linux-4.10.orig/drivers/hwmon/hwmon.c 2017-02-19 23:34:00.000000000 +0100 > +++ linux-4.10/drivers/hwmon/hwmon.c 2017-03-07 08:22:27.784527968 +0100 > @@ -186,7 +186,7 @@ static ssize_t hwmon_attr_show_string(st > char *buf) > { > struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); > - char *s; > + const char *s; > int ret; > > ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, > --- linux-4.10.orig/include/linux/hwmon.h 2017-02-19 23:34:00.000000000 +0100 > +++ linux-4.10/include/linux/hwmon.h 2017-03-07 08:21:28.247998585 +0100 > @@ -336,7 +336,7 @@ struct hwmon_ops { > int (*read)(struct device *dev, enum hwmon_sensor_types type, > u32 attr, int channel, long *val); > int (*read_string)(struct device *dev, enum hwmon_sensor_types type, > - u32 attr, int channel, char **str); > + u32 attr, int channel, const char **str); > int (*write)(struct device *dev, enum hwmon_sensor_types type, > u32 attr, int channel, long val); > }; > >