summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavio Cruz <flaviocruz@gmail.com>2024-03-03 12:38:19 -0500
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2024-03-03 20:05:44 +0100
commit8734aa9a0a81da90520114b207d1e541ec1fa063 (patch)
tree1e2a776b757c4a494de984144b9fd68dbdef50f4
parentf0378fa54b56755cf982eb517a009fb3dcf89ae5 (diff)
libps: update ps_emit_nice_size_t to handle arbitrarily large size_t
Update argument types for sprint_frac_value to reflect how big they actually are so that GCC doesn't think it needs a larger buffer than necessary. Message-ID: <ZeS1i5u_OziWpApt@jupiter.tail36e24.ts.net>
-rw-r--r--libps/spec.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/libps/spec.c b/libps/spec.c
index 9f647034..60ae7fbf 100644
--- a/libps/spec.c
+++ b/libps/spec.c
@@ -19,6 +19,7 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <hurd.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert-backtrace.h>
@@ -437,12 +438,12 @@ ps_emit_num_blocks (struct proc_stat *ps, struct ps_fmt_field *field,
size_t
sprint_frac_value (char *buf,
- size_t value, int min_value_len,
- size_t frac, int frac_scale,
- int width)
+ uint16_t value, uint8_t min_value_len,
+ uint16_t frac, uint8_t frac_scale,
+ uint8_t width)
{
- int value_len = 0;
- int frac_len = 0;
+ uint8_t value_len = 0;
+ uint8_t frac_len = 0;
if (value >= 1000) /* the integer part */
value_len = 4; /* values 1000-1023 */
@@ -462,9 +463,9 @@ sprint_frac_value (char *buf,
frac /= 10;
if (frac_len > 0)
- sprintf (buf, "%zd.%0*zd", value, frac_len, frac);
+ sprintf (buf, "%" PRIu16 ".%0*" PRIu16, value, frac_len, frac);
else
- sprintf (buf, "%zd", value);
+ sprintf (buf, "%" PRIu16, value);
return strlen (buf);
}
@@ -492,11 +493,14 @@ error_t
ps_emit_nice_size_t (struct proc_stat *ps, struct ps_fmt_field *field,
struct ps_stream *stream)
{
- char buf[21];
+ char buf[20];
size_t value = FG_PROC_STAT (field, size_t)(ps);
- char *sfx = " KMG";
+ char *sfx = " KMGTPE";
size_t frac = 0;
+ _Static_assert (sizeof (size_t) <= 8,
+ "ps_emit_nice_size_t can only emit size_t up to 8 bytes long.");
+
while (value >= 1024)
{
frac = ((value & 0x3FF) * 1000) >> 10;