From 6d7999d5b747283314c0f3833a3d9515f674a446 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Fri, 27 Feb 2015 19:37:24 +0100 Subject: btrfs-progs: use less memory for pretty_size_mode buffers Anand reports that the static buffers used for pertty size strings cause a stack overflow on SPARC. Zach proposed to change the printf format to wrap the number and the suffix into a macro. This would require to change all callsites of pretty_size* and is not very convienient to write. This patch replaces the per-call-site static buffers with a limited number for slots that would be used on each invokation of pretty_size and wrap around. The number of array slots shall be 10 for now, in current codebase there are no more than 2 calls to pretty_size in a single argument list. Reported-by: Anand Jain CC: Zach Brown Signed-off-by: David Sterba --- utils.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index b498381..e89707c 100644 --- a/utils.c +++ b/utils.c @@ -1479,6 +1479,24 @@ out: return ret; } +/* + * Note: this function uses a static per-thread buffer. Do not call this + * function more than 10 times within one argument list! + */ +const char *pretty_size_mode(u64 size, unsigned mode) +{ + static __thread int ps_index = 0; + static __thread char ps_array[10][32]; + char *ret; + + ret = ps_array[ps_index]; + ps_index++; + ps_index %= 10; + (void)pretty_size_snprintf(size, ret, 32, mode); + + return ret; +} + static const char* unit_suffix_binary[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}; static const char* unit_suffix_decimal[] = -- cgit v1.2.1