Received: from [140.186.70.92] (port=34707 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P5wN1-0006Yy-Gq for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:07:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P5wMu-0006S2-Gv for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:07:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8715) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P5wMu-0006Rx-Ai for qemu-devel@nongnu.org; Wed, 13 Oct 2010 04:07:24 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9D87N02015580 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Oct 2010 04:07:23 -0400 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count. References: <1286881809-11917-1-git-send-email-Jes.Sorensen@redhat.com> <1286881809-11917-2-git-send-email-Jes.Sorensen@redhat.com> <4CB55614.6080302@redhat.com> Date: Wed, 13 Oct 2010 10:07:20 +0200 In-Reply-To: <4CB55614.6080302@redhat.com> (Jes Sorensen's message of "Wed, 13 Oct 2010 08:47:48 +0200") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jes Sorensen Cc: pbonzini@redhat.com, qemu-devel@nongnu.org Jes Sorensen writes: > On 10/12/10 17:52, Markus Armbruster wrote: [...] >>> + c = *endptr++; >>> + if (isspace(c) || c == '\0') { >>> + c = 0; >>> + } else if (!isspace(*endptr) && *endptr != 0) { >>> + goto fail; >>> + } >> >> I'm not happy with this check. >> >> If the caller needs a complete string consumed, then this check is >> insufficient, because it doesn't catch trailing garbage as long as it >> starts with whitespace. The caller still needs to check !*endptr. >> >> If the caller needs to continue parsing after the value, and expects >> anything but whitespace there, it has to copy the value first. Only >> easy if the value is followed by some delimiter that can't occur in the >> value. Example: parse a size value from something of them form >> name=value,name=value... Need to copy up to the next comma or end of >> string. >> >> The check complicates the second case without really helping the first >> case. >> >> Nevertheless, it's good enough for the uses in this patch series, so I'm >> not insisting on getting this changed now. > > I hadn't thought of case #2, but I think that is pretty easy to handle > by accepting ',' as a separator as well. Then callers that don't want ',' have to check the suffix. And when a caller comes around that wants '"', it has to copy the value string, or hack strtosz() and figure out which of the existing callers now have to check the suffix. strtosz() shouldn't second guess what characters can legitimately follow (the follow set in parsing parlance). That's only known up the call chain. I still think there are two clean interfaces: * Consume the longest prefix that makes sense, then stop. Return where we stopped. Leave parsing / rejecting the suffix to the caller. That's how strtol() & friends work. * Consume a complete string, fail if there's trailing garbage. This is easier to use when you want to parse complete strings. It's awkward when you don't. > It's worth keeping in kind that > the old code didn't do anything with trailing garbage either, it was > silently ignored. > > For case #1 then I think it's ok to just accept trailing garbage, the > old code would simply use strtoull and leave it at that. As long as your patch doesn't make things worse, it can be committed. When I come across another use of strtosz() where it doesn't work nicely, I can fix it up. [...]