From patchwork Tue Mar 7 09:08:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jean Delvare X-Patchwork-Id: 9608627 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0A19460414 for ; Tue, 7 Mar 2017 11:04:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0147A284F0 for ; Tue, 7 Mar 2017 11:04:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA408284F3; Tue, 7 Mar 2017 11:04:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 11A82284F1 for ; Tue, 7 Mar 2017 11:04:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755102AbdCGLEQ convert rfc822-to-8bit (ORCPT ); Tue, 7 Mar 2017 06:04:16 -0500 Received: from mx2.suse.de ([195.135.220.15]:58031 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754609AbdCGLEE (ORCPT ); Tue, 7 Mar 2017 06:04:04 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9477AAD51; Tue, 7 Mar 2017 09:08:48 +0000 (UTC) Date: Tue, 7 Mar 2017 10:08:46 +0100 From: Jean Delvare To: Guenter Roeck Cc: Peter =?UTF-8?B?SMO8d2U=?= , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: Question about hwmon_attr_show_string Message-ID: <20170307100846.0487135b@endymion> In-Reply-To: <20170306234755.GA16512@roeck-us.net> References: <201703030133.01363.PeterHuewe@gmx.de> <201703062148.36101.PeterHuewe@gmx.de> <20170306234755.GA16512@roeck-us.net> Organization: SUSE Linux X-Mailer: Claws Mail 3.12.0 (GTK+ 2.24.31; x86_64-suse-linux-gnu) MIME-Version: 1.0 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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); };