From: Hans de Goede <j.w.r.degoede@hhs.nl>
To: lm-sensors@vger.kernel.org
Subject: [lm-sensors] Proposal: howto handle sysfs attribute writes with
Date: Thu, 19 Jul 2007 15:02:20 +0000 [thread overview]
Message-ID: <469F7CFC.4040306@hhs.nl> (raw)
Hi All,
Since there was some discussion about how I'm handling sysfs attr writes with
invalid values in the f71882fg driver, and since I've seen other discussions
about it, here is a proposal to try and create a standard way to handle this.
This is intended to become a part of Documentation/hwmon/sysfs eventually.
---
Howto check the validity of user written values to sysfs attributes:
hwmon sysfs attributes always contain numbers, so the first thing todo is to
convert the input to a number, there are 2 ways todo this depending wether
the number can be negative or not:
unsigned long u = simple_strtoul(buf, NULL, 10);
long s = simple_strtol(buf, NULL, 10);
With buf being the buffer with the user input being passed by the kernel.
Notice that we do not use the second argument of strto[u]l, and thus cannot
tell when 0 is returned, if this was really 0 or is caused by invalid input.
This is done deliberately as checking this everywhere would add a lot of
code to the kernel. We do need to document clearly that writing a non-number
will be seen as writing 0.
Notice that it is important to always store the converted value in an unsigned
long or long, so that no wrap around can happen before any further checking.
After conversion and storing the converted value in the right type, and
preferably before any conversions on the value, the value should be checked if
it its acceptable. For example if its a temperature limit being stored in an
unsigned 8 bit register, we should have something like this:
unsigned long u = simple_strtoul(buf, NULL, 10);
if (u > 255000)
return -EINVAL;
One could argue to clamp instead of returning EINVAL, but what todo then when
something that is not a continues range like temp sensor type gets written? In
the not a continues range scenario returning -EINVAL is the only thing that
makes sense, so we do this in the continues range scenario too to be consistent.
---
So does this make sense? I know that many drivers are currently doing this
different, some clamp, some don't check at all, thus effectively wrapping
around in most cases, all these different ways are exactly the reason for me
writing this proposal.
Regards,
Hans
_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
next reply other threads:[~2007-07-19 15:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-19 15:02 Hans de Goede [this message]
2007-07-22 16:24 ` [lm-sensors] Proposal: howto handle sysfs attribute writes with Jean Delvare
2007-07-29 7:13 ` Hans de Goede
2007-08-12 11:01 ` Jean Delvare
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=469F7CFC.4040306@hhs.nl \
--to=j.w.r.degoede@hhs.nl \
--cc=lm-sensors@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.