From: Mattijs Korpershoek <mkorpershoek@baylibre.com>
To: Nicolas Belin <nbelin@baylibre.com>,
Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
Safae Ouajih <souajih@baylibre.com>,
Ahmad Draidi <ar2000jp@gmail.com>
Cc: u-boot@lists.denx.de, Nicolas Belin <nbelin@baylibre.com>
Subject: Re: [PATCH 3/4] boot: android: rework bootargs concatenation
Date: Mon, 16 Dec 2024 09:41:05 +0100 [thread overview]
Message-ID: <87cyhsxboe.fsf@baylibre.com> (raw)
In-Reply-To: <20241211-fix-bootargs-concatenation-v1-3-c6752bcb9dde@baylibre.com>
Hi Nicolas,
Thank you for the patch.
On mer., déc. 11, 2024 at 14:53, Nicolas Belin <nbelin@baylibre.com> wrote:
> Rework the bootargs concatenation allocating more accurately
> the length that is needed.
> Do not forget an extra byte for the null termination byte as,
> in some cases, the allocation was 1 byte short.
>
> Fixes: 86f4695b ("image: Fix Android boot image support")
> Signed-off-by: Nicolas Belin <nbelin@baylibre.com>
> ---
> boot/image-android.c | 16 ++++++++--------
> 1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/boot/image-android.c b/boot/image-android.c
> index 362a5c7435a3a8bcf7b674b96e31069a91a892b5..ed72a5c30424a453c1800bc61edbe8f33b31b341 100644
> --- a/boot/image-android.c
> +++ b/boot/image-android.c
> @@ -289,7 +289,7 @@ int android_image_get_kernel(const void *hdr,
> int len = 0;
> if (*img_data.kcmdline) {
> printf("Kernel command line: %s\n", img_data.kcmdline);
> - len += strlen(img_data.kcmdline);
> + len += strlen(img_data.kcmdline) + 1; /* Extra space character needed */
> }
>
> if (*img_data.kcmdline_extra) {
> @@ -299,28 +299,28 @@ int android_image_get_kernel(const void *hdr,
>
> char *bootargs = env_get("bootargs");
> if (bootargs)
> - len += strlen(bootargs);
> + len += strlen(bootargs) + 1; /* Extra space character needed */
In some cases, we will allocate for an extra space character when this
is not needed.
For example, with:
* bootargs = "b"
* kcmdline = NULL
* kcmdline_extra = NULL
We will allocate strlen("b") + 1 + 1. But we only need to allocate
strlen("b") + 1.
Another example:
* bootargs = "b"
* kcmdline = "k"
* kcmdline_extra = NULL
Will allocate strlen("b") + 1 + strlen("k") + 1 + 1. But we only need:
strlen("b") + 1 + strlen("k") + 1.
How about we count the amount of elements that are not NULL to compute
the amount of spaces:
* 0 -> 0 space
* 1 -> 0 space
* 2 -> 1 space
* 3 -> 2 spaces
>
> - char *newbootargs = malloc(len + 2);
> + char *newbootargs = malloc(len + 1); /* +1 for the '\0' */
> if (!newbootargs) {
> puts("Error: malloc in android_image_get_kernel failed!\n");
> return -ENOMEM;
> }
> - *newbootargs = '\0';
> + *newbootargs = '\0'; /* set to NULL in case no components below are present */
The comment should state Null, not NULL. NULL is the pointer and Null is
the \0 character.
>
> if (bootargs) {
> strcpy(newbootargs, bootargs);
> strcat(newbootargs, " ");
A similar thing can be stated here. Sometimes, we add spaces which are
not needed.
Can we rework this a little to only add what is needed?
> }
>
> - if (*img_data.kcmdline)
> + if (*img_data.kcmdline) {
> strcat(newbootargs, img_data.kcmdline);
> -
> - if (*img_data.kcmdline_extra) {
> strcat(newbootargs, " ");
> - strcat(newbootargs, img_data.kcmdline_extra);
> }
>
> + if (*img_data.kcmdline_extra)
> + strcat(newbootargs, img_data.kcmdline_extra);
> +
> env_set("bootargs", newbootargs);
> free(newbootargs);
>
>
> --
> 2.34.1
next prev parent reply other threads:[~2024-12-16 8:41 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-11 13:53 [PATCH 0/4] boot: android: rework the bootargs concatenation Nicolas Belin
2024-12-11 13:53 ` [PATCH 1/4] boot: android: fix extra command line support Nicolas Belin
2024-12-16 7:58 ` Mattijs Korpershoek
2024-12-11 13:53 ` [PATCH 2/4] boot: android: free newbootargs when done Nicolas Belin
2024-12-16 8:00 ` Mattijs Korpershoek
2024-12-11 13:53 ` [PATCH 3/4] boot: android: rework bootargs concatenation Nicolas Belin
2024-12-16 8:41 ` Mattijs Korpershoek [this message]
2024-12-16 12:45 ` Nicolas Belin
2024-12-11 13:53 ` [PATCH 4/4] boot: android: reorder the length calculation order Nicolas Belin
2024-12-16 8:42 ` Mattijs Korpershoek
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=87cyhsxboe.fsf@baylibre.com \
--to=mkorpershoek@baylibre.com \
--cc=ar2000jp@gmail.com \
--cc=nbelin@baylibre.com \
--cc=sjg@chromium.org \
--cc=souajih@baylibre.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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.