[libvirt] [PATCH 0/2] Report errors in virNodeCountThreadSiblings

https://bugzilla.redhat.com/show_bug.cgi?id=1207849 Ján Tomko (2): Report errors in virNodeCountThreadSiblings Simplify virNodeCountThreadSiblings src/nodeinfo.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) -- 2.3.6

Use virFileReadAll which reports an error when the file is larger than the specified maximum. https://bugzilla.redhat.com/show_bug.cgi?id=1207849 --- src/nodeinfo.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 29f1aa7..9db3233 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -344,7 +344,6 @@ virNodeCountThreadSiblings(const char *dir, unsigned int cpu) { unsigned long ret = 0; char *path; - FILE *pathfp; char *str = NULL; size_t i; @@ -352,27 +351,16 @@ virNodeCountThreadSiblings(const char *dir, unsigned int cpu) dir, cpu) < 0) return 0; - pathfp = fopen(path, "r"); - if (pathfp == NULL) { + if (!virFileExists(path)) { /* If file doesn't exist, then pretend our only * sibling is ourself */ - if (errno == ENOENT) { - VIR_FREE(path); - return 1; - } - virReportSystemError(errno, _("cannot open %s"), path); - VIR_FREE(path); - return 0; + ret = 1; + goto cleanup; } - if (VIR_ALLOC_N(str, SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX) < 0) + if (virFileReadAll(path, SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX, &str) < 0) goto cleanup; - if (fgets(str, SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX, pathfp) == NULL) { - virReportSystemError(errno, _("cannot read from %s"), path); - goto cleanup; - } - i = 0; while (str[i] != '\0') { if (c_isdigit(str[i])) @@ -386,9 +374,7 @@ virNodeCountThreadSiblings(const char *dir, unsigned int cpu) cleanup: VIR_FREE(str); - VIR_FORCE_FCLOSE(pathfp); VIR_FREE(path); - return ret; } -- 2.3.6

Use a for cycle instead of while. Do not opencode c_isxdigit and virHexToBin. --- src/nodeinfo.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 9db3233..2fafe2d 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -361,15 +361,9 @@ virNodeCountThreadSiblings(const char *dir, unsigned int cpu) if (virFileReadAll(path, SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX, &str) < 0) goto cleanup; - i = 0; - while (str[i] != '\0') { - if (c_isdigit(str[i])) - ret += count_one_bits(str[i] - '0'); - else if (str[i] >= 'A' && str[i] <= 'F') - ret += count_one_bits(str[i] - 'A' + 10); - else if (str[i] >= 'a' && str[i] <= 'f') - ret += count_one_bits(str[i] - 'a' + 10); - i++; + for (i = 0; str[i] != '\0'; i++) { + if (c_isxdigit(str[i])) + ret += count_one_bits(virHexToBin(str[i])); } cleanup: -- 2.3.6

On 06/02/2015 02:53 PM, Ján Tomko wrote:
Use a for cycle instead of while. s/cycle/loop
Do not opencode c_isxdigit and virHexToBin. --- src/nodeinfo.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 9db3233..2fafe2d 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -361,15 +361,9 @@ virNodeCountThreadSiblings(const char *dir, unsigned int cpu) if (virFileReadAll(path, SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX, &str) < 0) goto cleanup;
- i = 0; - while (str[i] != '\0') { - if (c_isdigit(str[i])) - ret += count_one_bits(str[i] - '0'); - else if (str[i] >= 'A' && str[i] <= 'F') - ret += count_one_bits(str[i] - 'A' + 10); - else if (str[i] >= 'a' && str[i] <= 'f') - ret += count_one_bits(str[i] - 'a' + 10); - i++; + for (i = 0; str[i] != '\0'; i++) { + if (c_isxdigit(str[i])) + ret += count_one_bits(virHexToBin(str[i])); }
cleanup:
ACK to both with that minor adjustment in commit message. Erik

On Tue, Jun 02, 2015 at 02:53:43PM +0200, Ján Tomko wrote:
https://bugzilla.redhat.com/show_bug.cgi?id=1207849
Ján Tomko (2): Report errors in virNodeCountThreadSiblings Simplify virNodeCountThreadSiblings
src/nodeinfo.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
ACK series
-- 2.3.6
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (3)
-
Erik Skultety
-
Ján Tomko
-
Martin Kletzander