* [PATCH iproute2] ifstat: convert sprintf to snprintf
@ 2024-01-31 12:41 Denis Kirjanov
2024-01-31 16:14 ` Stephen Hemminger
0 siblings, 1 reply; 6+ messages in thread
From: Denis Kirjanov @ 2024-01-31 12:41 UTC (permalink / raw)
To: stephen; +Cc: netdev, Denis Kirjanov
Use snprintf to print only valid memory
Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
---
misc/ifstat.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/misc/ifstat.c b/misc/ifstat.c
index 721f4914..08a13d7a 100644
--- a/misc/ifstat.c
+++ b/misc/ifstat.c
@@ -379,10 +379,10 @@ static void format_rate(FILE *fp, const unsigned long long *vals,
fprintf(fp, "%8llu ", vals[i]);
if (rates[i] > mega) {
- sprintf(temp, "%uM", (unsigned int)(rates[i]/mega));
+ snprintf(temp, sizeof(temp), "%uM", (unsigned int)(rates[i]/mega));
fprintf(fp, "%-6s ", temp);
} else if (rates[i] > kilo) {
- sprintf(temp, "%uK", (unsigned int)(rates[i]/kilo));
+ snprintf(temp, sizeof(temp), "%uK", (unsigned int)(rates[i]/kilo));
fprintf(fp, "%-6s ", temp);
} else
fprintf(fp, "%-6u ", (unsigned int)rates[i]);
@@ -400,10 +400,10 @@ static void format_pair(FILE *fp, const unsigned long long *vals, int i, int k)
fprintf(fp, "%8llu ", vals[i]);
if (vals[k] > giga) {
- sprintf(temp, "%uM", (unsigned int)(vals[k]/mega));
+ snprintf(temp, sizeof(temp), "%uM", (unsigned int)(vals[k]/mega));
fprintf(fp, "%-6s ", temp);
} else if (vals[k] > mega) {
- sprintf(temp, "%uK", (unsigned int)(vals[k]/kilo));
+ snprintf(temp, sizeof(temp), "%uK", (unsigned int)(vals[k]/kilo));
fprintf(fp, "%-6s ", temp);
} else
fprintf(fp, "%-6u ", (unsigned int)vals[k]);
@@ -675,7 +675,7 @@ static void server_loop(int fd)
p.fd = fd;
p.events = p.revents = POLLIN;
- sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
+ snprintf(info_source, sizeof(info_source), "%d.%lu sampling_interval=%d time_const=%d",
getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
load_info();
@@ -893,7 +893,7 @@ int main(int argc, char *argv[])
sun.sun_family = AF_UNIX;
sun.sun_path[0] = 0;
- sprintf(sun.sun_path+1, "ifstat%d", getuid());
+ snprintf(sun.sun_path+1, sizeof(sun.sun_path), "ifstat%d", getuid());
if (scan_interval > 0) {
if (time_constant == 0)
--
2.30.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH iproute2] ifstat: convert sprintf to snprintf
2024-01-31 12:41 [PATCH iproute2] ifstat: convert sprintf to snprintf Denis Kirjanov
@ 2024-01-31 16:14 ` Stephen Hemminger
2024-02-02 11:32 ` David Laight
0 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2024-01-31 16:14 UTC (permalink / raw)
To: Denis Kirjanov; +Cc: netdev, Denis Kirjanov
On Wed, 31 Jan 2024 07:41:07 -0500
Denis Kirjanov <kirjanov@gmail.com> wrote:
> @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
>
> sun.sun_family = AF_UNIX;
> sun.sun_path[0] = 0;
> - sprintf(sun.sun_path+1, "ifstat%d", getuid());
> + snprintf(sun.sun_path+1, sizeof(sun.sun_path), "ifstat%d", getuid());
If you are changing the line, please add spaces around plus sign
p
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [PATCH iproute2] ifstat: convert sprintf to snprintf
2024-01-31 16:14 ` Stephen Hemminger
@ 2024-02-02 11:32 ` David Laight
2024-02-02 12:23 ` Denis Kirjanov
0 siblings, 1 reply; 6+ messages in thread
From: David Laight @ 2024-02-02 11:32 UTC (permalink / raw)
To: 'Stephen Hemminger', Denis Kirjanov
Cc: netdev@vger.kernel.org, Denis Kirjanov
From: Stephen Hemminger
> Sent: 31 January 2024 16:14
>
> On Wed, 31 Jan 2024 07:41:07 -0500
> Denis Kirjanov <kirjanov@gmail.com> wrote:
>
> > @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
> >
> > sun.sun_family = AF_UNIX;
> > sun.sun_path[0] = 0;
> > - sprintf(sun.sun_path+1, "ifstat%d", getuid());
> > + snprintf(sun.sun_path+1, sizeof(sun.sun_path), "ifstat%d", getuid());
>
> If you are changing the line, please add spaces around plus sign
Isn't the size also wrong - needs a matching '- 1'.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH iproute2] ifstat: convert sprintf to snprintf
2024-02-02 11:32 ` David Laight
@ 2024-02-02 12:23 ` Denis Kirjanov
2024-02-02 13:02 ` David Laight
0 siblings, 1 reply; 6+ messages in thread
From: Denis Kirjanov @ 2024-02-02 12:23 UTC (permalink / raw)
To: David Laight, 'Stephen Hemminger', Denis Kirjanov
Cc: netdev@vger.kernel.org
On 2/2/24 14:32, David Laight wrote:
> From: Stephen Hemminger
>> Sent: 31 January 2024 16:14
>
>>
>> On Wed, 31 Jan 2024 07:41:07 -0500
>> Denis Kirjanov <kirjanov@gmail.com> wrote:
>>
>>> @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
>>>
>>> sun.sun_family = AF_UNIX;
>>> sun.sun_path[0] = 0;
>>> - sprintf(sun.sun_path+1, "ifstat%d", getuid());
>>> + snprintf(sun.sun_path+1, sizeof(sun.sun_path), "ifstat%d", getuid());
>>
>> If you are changing the line, please add spaces around plus sign
>
> Isn't the size also wrong - needs a matching '- 1'.
I don't think it's wrong, it's just the size of the target buffer which is
UNIX_PATH_MAX bytes.
>
> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [PATCH iproute2] ifstat: convert sprintf to snprintf
2024-02-02 12:23 ` Denis Kirjanov
@ 2024-02-02 13:02 ` David Laight
2024-02-02 13:28 ` Denis Kirjanov
0 siblings, 1 reply; 6+ messages in thread
From: David Laight @ 2024-02-02 13:02 UTC (permalink / raw)
To: 'Denis Kirjanov', 'Stephen Hemminger',
Denis Kirjanov
Cc: netdev@vger.kernel.org
From: Denis Kirjanov
> Sent: 02 February 2024 12:24
>
> On 2/2/24 14:32, David Laight wrote:
> > From: Stephen Hemminger
> >> Sent: 31 January 2024 16:14
> >
> >>
> >> On Wed, 31 Jan 2024 07:41:07 -0500
> >> Denis Kirjanov <kirjanov@gmail.com> wrote:
> >>
> >>> @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
> >>>
> >>> sun.sun_family = AF_UNIX;
> >>> sun.sun_path[0] = 0;
> >>> - sprintf(sun.sun_path+1, "ifstat%d", getuid());
> >>> + snprintf(sun.sun_path+1, sizeof(sun.sun_path), "ifstat%d", getuid());
> >>
> >> If you are changing the line, please add spaces around plus sign
> >
> > Isn't the size also wrong - needs a matching '- 1'.
>
> I don't think it's wrong, it's just the size of the target buffer which is
> UNIX_PATH_MAX bytes.
But you are starting one byte in.
So, if the size were 8 the '\0' would be written after the end.
Also, to avoid the next patch in a few weeks it should be
calling scnprintf().
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH iproute2] ifstat: convert sprintf to snprintf
2024-02-02 13:02 ` David Laight
@ 2024-02-02 13:28 ` Denis Kirjanov
0 siblings, 0 replies; 6+ messages in thread
From: Denis Kirjanov @ 2024-02-02 13:28 UTC (permalink / raw)
To: David Laight, 'Stephen Hemminger', Denis Kirjanov
Cc: netdev@vger.kernel.org
On 2/2/24 16:02, David Laight wrote:
> From: Denis Kirjanov
>> Sent: 02 February 2024 12:24
>>
>> On 2/2/24 14:32, David Laight wrote:
>>> From: Stephen Hemminger
>>>> Sent: 31 January 2024 16:14
>>>
>>>>
>>>> On Wed, 31 Jan 2024 07:41:07 -0500
>>>> Denis Kirjanov <kirjanov@gmail.com> wrote:
>>>>
>>>>> @@ -893,7 +893,7 @@ int main(int argc, char *argv[])
>>>>>
>>>>> sun.sun_family = AF_UNIX;
>>>>> sun.sun_path[0] = 0;
>>>>> - sprintf(sun.sun_path+1, "ifstat%d", getuid());
>>>>> + snprintf(sun.sun_path+1, sizeof(sun.sun_path), "ifstat%d", getuid());
>>>>
>>>> If you are changing the line, please add spaces around plus sign
>>>
>>> Isn't the size also wrong - needs a matching '- 1'.
>>
>> I don't think it's wrong, it's just the size of the target buffer which is
>> UNIX_PATH_MAX bytes.
>
> But you are starting one byte in.
> So, if the size were 8 the '\0' would be written after the end.
yep, you're right
>
> Also, to avoid the next patch in a few weeks it should be
> calling scnprintf().
I'll post the next version
>
> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-02-02 13:28 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-31 12:41 [PATCH iproute2] ifstat: convert sprintf to snprintf Denis Kirjanov
2024-01-31 16:14 ` Stephen Hemminger
2024-02-02 11:32 ` David Laight
2024-02-02 12:23 ` Denis Kirjanov
2024-02-02 13:02 ` David Laight
2024-02-02 13:28 ` Denis Kirjanov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox