From: Easwar Hariharan <eahariha@linux.microsoft.com>
To: Shradha Gupta <shradhagupta@linux.microsoft.com>
Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org,
"K. Y. Srinivasan" <kys@microsoft.com>,
Haiyang Zhang <haiyangz@microsoft.com>,
Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
Long Li <longli@microsoft.com>,
Michael Kelley <mikelley@microsoft.com>,
Olaf Hering <olaf@aepfle.de>, Ani Sinha <anisinha@redhat.com>,
Shradha Gupta <shradhagupta@microsoft.com>
Subject: Re: [PATCH v2] hv/hv_kvp_daemon: Handle IPv4 and Ipv6 combination for keyfile format
Date: Fri, 15 Mar 2024 09:56:22 -0700 [thread overview]
Message-ID: <dd1b378d-6750-419f-8c46-a8f42c0ebe11@linux.microsoft.com> (raw)
In-Reply-To: <20240315140914.GA14685@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net>
On 3/15/2024 7:09 AM, Shradha Gupta wrote:
<snip>
>>>> }
>>>>
>>>> +static int process_dns_gateway_nm(FILE *f, char *ip_string, int type,
>>>> + int ip_sec)
>>>> +{
>>>> + char addr[INET6_ADDRSTRLEN], *output_str;
>>>> + int ip_offset = 0, error = 0, ip_ver;
>>>> + char *param_name;
>>>> +
>>>> + output_str = (char *)calloc(INET6_ADDRSTRLEN * MAX_IP_ENTRIES,
>>>> + sizeof(char));
>>>> +
>>>> + if (!output_str)
>>>> + return -ENOMEM;
>>>> +
>>>> + memset(addr, 0, sizeof(addr));
>>>> +
>>>> + if (type == DNS) {
>>>> + param_name = "dns";
>>>> + } else if (type == GATEWAY) {
>>>> + param_name = "gateway";
>>>> + } else {
>>>> + error = -EINVAL;
>>>> + goto cleanup;
>>>> + }
>>>> +
>>>> + while (parse_ip_val_buffer(ip_string, &ip_offset, addr,
>>>> + (MAX_IP_ADDR_SIZE * 2))) {
>>>> + ip_ver = ip_version_check(addr);
>>>> + if (ip_ver < 0)
>>>> + continue;
>>>> +
>>>> + if ((ip_ver == IPV4 && ip_sec == IPV4) ||
>>>> + (ip_ver == IPV6 && ip_sec == IPV6)) {
>>>> + if (((INET6_ADDRSTRLEN * MAX_IP_ENTRIES) - strlen(output_str)) >
>>>> + (strlen(addr))) {
>>>> + strcat(output_str, addr);
>>>> + strcat(output_str, ",");
>>>
>>> Prefer strncat() here
> Is this needed with the bound check above. I am trying to keep parity with the rest of the
> file.
>>>
<snip>
I missed this earlier because my comment was more of a general best practice comment.
Note that in the worst case where your bounds check (INET6_ADDRSTRLEN*MAX_IP_ENTRIES) - strlen(output_str) equals (strlen(addr) + 1),
you will be adding strlen(addr)+1(","), and end up with no ASCII NUL '\0' delimiter.
If you're going to rely on the bounds check to ensure correctness, you'll need to correct that. Generally speaking, strncat would still
be helpful in case the bounds check changes in the future.
Thanks,
Easwar
next prev parent reply other threads:[~2024-03-15 16:56 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-12 12:38 [PATCH v2] hv/hv_kvp_daemon: Handle IPv4 and Ipv6 combination for keyfile format Shradha Gupta
2024-03-12 14:39 ` Ani Sinha
2024-03-13 5:16 ` Shradha Gupta
2024-03-12 16:58 ` Easwar Hariharan
2024-03-13 5:22 ` Shradha Gupta
2024-03-15 14:09 ` Shradha Gupta
2024-03-15 16:56 ` Easwar Hariharan [this message]
2024-03-18 2:02 ` Shradha Gupta
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=dd1b378d-6750-419f-8c46-a8f42c0ebe11@linux.microsoft.com \
--to=eahariha@linux.microsoft.com \
--cc=anisinha@redhat.com \
--cc=decui@microsoft.com \
--cc=haiyangz@microsoft.com \
--cc=kys@microsoft.com \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longli@microsoft.com \
--cc=mikelley@microsoft.com \
--cc=olaf@aepfle.de \
--cc=shradhagupta@linux.microsoft.com \
--cc=shradhagupta@microsoft.com \
--cc=wei.liu@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox