All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Collin L. Walling" <walling@linux.vnet.ibm.com>
To: Thomas Huth <thuth@redhat.com>,
	qemu-s390x@nongnu.org, qemu-devel@nongnu.org
Cc: frankja@linux.vnet.ibm.com, cohuck@redhat.com, david@redhat.com,
	alifm@linux.vnet.ibm.com, mihajlov@linux.vnet.ibm.com,
	borntraeger@de.ibm.com
Subject: Re: [Qemu-devel] [qemu-s390x] [PATCH v7 04/12] s390-ccw: update libc
Date: Mon, 19 Feb 2018 12:17:20 -0500	[thread overview]
Message-ID: <26604847-20b0-3d62-8967-d520b599aaa8@linux.vnet.ibm.com> (raw)
In-Reply-To: <06328b8c-4aa2-e791-9bcc-f83c2ac49964@linux.vnet.ibm.com>

On 02/19/2018 11:19 AM, Collin L. Walling wrote:
> On 02/19/2018 11:00 AM, Thomas Huth wrote:
>> On 19.02.2018 16:40, Collin L. Walling wrote:
>>> On 02/17/2018 02:48 AM, Thomas Huth wrote:
>>>> On 16.02.2018 23:07, Collin L. Walling wrote:
>>>> [...]
>>>>> +/**
>>>>> + * uitoa:
>>>>> + * @num: an integer (base 10) to be converted.
>>>>> + * @str: a pointer to a string to store the conversion.
>>>>> + * @len: the length of the passed string.
>>>>> + *
>>>>> + * Given an integer @num, convert it to a string. The string @str
>>>>> must be
>>>>> + * allocated beforehand. The resulting string will be null
>>>>> terminated and
>>>>> + * returned. This function only handles numbers between 0 and
>>>>> UINT64_MAX
>>>>> + * inclusive.
>>>>> + *
>>>>> + * Returns: the string @str of the converted integer @num
>>>>> + */
>>>>> +char *uitoa(uint64_t num, char *str, size_t len)
>>>>> +{
>>>>> +    size_t num_idx = 0;
>>>>> +    uint64_t tmp = num;
>>>>> +
>>>>> +    IPL_assert(str != NULL, "uitoa: no space allocated to store
>>>>> string");
>>>>> +
>>>>> +    /* Get index to ones place */
>>>>> +    while ((tmp /= 10) != 0) {
>>>>> +        num_idx++;
>>>>> +    }
>>>>> +
>>>>> +    /* Check if we have enough space for num and null */
>>>>> +    IPL_assert(len > num_idx, "uitoa: array too small for 
>>>>> conversion");
>>>> Well, in v5 of this patch you've had "len >= num_idx + 1" where we
>>>> agreed that it was wrong. Now you have "len > num_idx" which is pretty
>>>> much the same. WTF?
>>>> I still think you need "len > num_idx + 1" here to properly take the
>>>> trailing NUL-byte into account properly. Please fix it!
>>>>
>>>>    Thomas
>>>>
>>> You're correct, and my apologies for not correcting the true problem 
>>> here:
>>> I messed up the value of num_idx.  It is off by one, but 
>>> initializing the
>>> value to 1 instead of 0 should fix this. I must've accounted for 
>>> this in
>>> my test file but forgot to update it in the actual source code.
>> Are you sure that initializing it to 1 is right? Unless you also change
>> the final while loop in this function, this will put the character into
>> the wrong location ("str[num_idx] = num % 10 + '0';"). Imagine a number
>> that only consists of one digit ... the digit will be placed in str[1]
>> which sounds wrong to me...?
>>
>>   Thomas
>>
> There's that, which we can solve by decrementing num_idx at the start 
> of the loop.
> We also have to change the line str[num_idx + 1] = '\0'; to no longer 
> add 1.
> It all boils down to "which way reads better", which I often struggle and
> bounce back-and-forth with (way) too much...
>
> Maybe I should also rename num_idx to just "idx" and let the comments 
> explain
> everything?
>
How is this for a compromise?

     - start num_idx at 1, provide comment as for why
     - change while loop comment to explain we are "counting the 
_indices_ _of_ _num_"
     - str[num_idx] is assigned \0, and we also decrement num_idx in one 
line
     - in conversion loop, post decrement num_idx as it is used

char *uitoa(int num, char *str, int len)
{
   int num_idx = 1; /* account for NULL */
   int tmp = num;

   assert(str != NULL, "uitoa: no space allocated to store string");

   /* Count indices of num */
   while ((tmp /= 10) != 0)
     num_idx++;

   /* Check if we have enough space for num and null */
   assert(len > num_idx, "uitoa: array too small for conversion");

   str[num_idx--] = '\0';

   /* Convert int to string */
   while (num_idx >= 0) {
     str[num_idx--] = num % 10 + '0';
     num /= 10;
   }

   return str;
}


-- 
- Collin L Walling

  reply	other threads:[~2018-02-19 17:17 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-16 22:07 [Qemu-devel] [PATCH v7 00/12] Interactive Boot Menu for DASD and SCSI Guests on s390x Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 01/12] s390-ccw: refactor boot map table code Collin L. Walling
2018-02-17  7:20   ` Thomas Huth
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 02/12] s390-ccw: refactor eckd_block_num to use CHS Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 03/12] s390-ccw: refactor IPL structs Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 04/12] s390-ccw: update libc Collin L. Walling
2018-02-17  7:48   ` [Qemu-devel] [qemu-s390x] " Thomas Huth
2018-02-19 15:40     ` Collin L. Walling
2018-02-19 16:00       ` Thomas Huth
2018-02-19 16:19         ` Collin L. Walling
2018-02-19 17:17           ` Collin L. Walling [this message]
2018-02-19 17:54             ` Eric Blake
2018-02-19 18:07             ` Thomas Huth
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 05/12] s390-ccw: move auxiliary IPL data to separate location Collin L. Walling
2018-02-17  8:11   ` [Qemu-devel] [qemu-s390x] " Thomas Huth
2018-02-19  8:50     ` Viktor Mihajlovski
2018-02-19 12:15       ` Viktor Mihajlovski
2018-02-19 14:14         ` Thomas Huth
2018-02-19 16:07   ` [Qemu-devel] " Viktor Mihajlovski
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 06/12] s390-ccw: parse and set boot menu options Collin L. Walling
2018-02-17  8:26   ` [Qemu-devel] [qemu-s390x] " Thomas Huth
2018-02-19 12:39     ` Viktor Mihajlovski
2018-02-19 15:52   ` [Qemu-devel] " Viktor Mihajlovski
2018-02-19 20:39     ` Collin L. Walling
2018-02-20  9:55       ` Viktor Mihajlovski
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 07/12] s390-ccw: set up interactive boot menu parameters Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 08/12] s390-ccw: read stage2 boot loader data to find menu Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 09/12] s390-ccw: print zipl boot menu Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 10/12] s390-ccw: read user input for boot index via the SCLP console Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 11/12] s390-ccw: set cp_receive mask only when needed and consume pending service irqs Collin L. Walling
2018-02-19 14:15   ` Christian Borntraeger
2018-02-19 14:17     ` Christian Borntraeger
2018-02-19 15:49       ` Collin L. Walling
2018-02-19 15:48     ` Collin L. Walling
2018-02-16 22:07 ` [Qemu-devel] [PATCH v7 12/12] s390-ccw: interactive boot menu for scsi Collin L. Walling

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=26604847-20b0-3d62-8967-d520b599aaa8@linux.vnet.ibm.com \
    --to=walling@linux.vnet.ibm.com \
    --cc=alifm@linux.vnet.ibm.com \
    --cc=borntraeger@de.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=david@redhat.com \
    --cc=frankja@linux.vnet.ibm.com \
    --cc=mihajlov@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=thuth@redhat.com \
    /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.