From: Andrew Morton <akpm@linux-foundation.org>
To: Harvey Harrison <harvey.harrison@gmail.com>
Cc: davem@davemloft.net, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 12/12] net: use the common ascii hex helpers
Date: Fri, 2 May 2008 17:30:30 -0700 [thread overview]
Message-ID: <20080502173030.33c40213.akpm@linux-foundation.org> (raw)
In-Reply-To: <1209773755.26173.114.camel@brick>
On Fri, 02 May 2008 17:15:55 -0700
Harvey Harrison <harvey.harrison@gmail.com> wrote:
> int hex_char_to_int(char ch)
> {
> return -1 if not [a-fA-F0-9]
> }
>
> int hex_to_u8(const char *buf, u8 *val)
> int hex_to_u16(const char *buf, u16 *val)
> int hex_to_u32(const char *buf, u32 *val)
> int hex_to_u64(const char *buf, u64 *val)
>
> Which return the number of chars read and reads up to a maximum of
> 2,4,8,16 chars respectively and returns early if a non-hex char
> is encountered.
>
> Thoughts?
I dunno. Return-by-reference is a bit nasty and forces the compiler to
generate extra code to put local storage into a stack slot rather than
keeping it in a register.
Does anyone actually test whether hex_to_foo returns -1? Probably not
many.
In which case it might be better to do
u64 hex_to_u64(const char *buf);
and, if we encounter a non-hex char, do a WARN_ON(1).
One could legitimately do
int c = hex_to_int(some_user_input);
if (c < 0) {
printk("user: you're an idiot\n");
}
to perform validation and conversion in a single operation but I expect
that such code is in the minority, and could be covered by running a separate
bool is_hex_string(const char *bug, size_t nchars);
beforehand?
prev parent reply other threads:[~2008-05-03 0:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-02 19:01 [PATCH 12/12] net: use the common ascii hex helpers Harvey Harrison
2008-05-02 23:25 ` David Miller
2008-05-02 23:31 ` Andrew Morton
2008-05-02 23:39 ` David Miller
2008-05-02 23:43 ` Andrew Morton
2008-05-02 23:52 ` Harvey Harrison
2008-05-03 0:08 ` Andrew Morton
2008-05-03 0:11 ` Harvey Harrison
2008-05-03 0:15 ` Harvey Harrison
2008-05-03 0:30 ` Andrew Morton [this message]
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=20080502173030.33c40213.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=davem@davemloft.net \
--cc=harvey.harrison@gmail.com \
--cc=linux-kernel@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.