[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen master] libxenstat/Linux: pass nul-terminated string to strpbrk()



commit 82f7f7be462ddb435c60aef86960252240bf03c7
Author:     Jan Beulich <jbeulich@xxxxxxxx>
AuthorDate: Wed Aug 9 09:03:51 2023 +0200
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Wed Aug 9 09:03:51 2023 +0200

    libxenstat/Linux: pass nul-terminated string to strpbrk()
    
    While what "tmp" points to has been cleared at the end of the first
    iteration of parseNetDevLine()'s main loop, this is too late for the
    first iteration's invocation of strpbrk() (copying the interface name).
    Properly nul-terminate the string at population time instead, removing
    the late clearing.
    
    While there also eliminate a confusing (because of being wrong) comment:
    A regex parsing error would be handled one further scope outwards. Here
    we're dealing with field 1 vs any of the later fields.
    
    Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
    Reviewed-by: Juergen Gross <jgross@xxxxxxxx>
    Acked-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
---
 tools/libs/stat/xenstat_linux.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/libs/stat/xenstat_linux.c b/tools/libs/stat/xenstat_linux.c
index 05d0b07553..cbba54aa83 100644
--- a/tools/libs/stat/xenstat_linux.c
+++ b/tools/libs/stat/xenstat_linux.c
@@ -169,6 +169,7 @@ static int parseNetDevLine(char *line, char *iface, 
unsigned long long *rxBytes,
                                                        matches[i].rm_so + 1) * 
sizeof(char));
                                for (x = matches[i].rm_so; x < 
matches[i].rm_eo; x++)
                                        tmp[x - matches[i].rm_so] = line[x];
+                               tmp[x - matches[i].rm_so] = 0;
 
                                /* We populate all the fields from 
/proc/net/dev line */
                                if (i > 1) {
@@ -225,15 +226,11 @@ static int parseNetDevLine(char *line, char *iface, 
unsigned long long *rxBytes,
                                                        break;
                                        }
                                }
-                               else
-                               /* There were errors when parsing this directly 
in RE. strpbrk() helps */
-                               if (iface != NULL) {
+                               else if (iface != NULL) {
                                        char *tmp2 = strpbrk(tmp, 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
                                        if (tmp2 != NULL)
                                                strcpy(iface, tmp2);
                                }
-
-                               memset(tmp, 0, matches[i].rm_eo - 
matches[i].rm_so);
                        }
                }
        }
--
generated by git-patchbot for /home/xen/git/xen.git#master



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.