Bug 25244 - --print-memory-usage, division by zero if MEMORY length is zero
Summary: --print-memory-usage, division by zero if MEMORY length is zero
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.34
: P2 enhancement
Target Milestone: 2.34
Assignee: Alan Modra
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-03 14:55 UTC by Pekka Seppänen
Modified: 2019-12-05 11:23 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed: 2019-12-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pekka Seppänen 2019-12-03 14:55:53 UTC
Hi.

lang_print_memory_usage() at ld/ldlang.c calculates and displays the memory region usage as follows:

9282       percent = used_length * 100.0 / r->length;
9284       printf ("    %6.2f%%\n", percent);

If the memory region length is zero (denoted by r->length), this will cause division by zero.  Likely no other harm is done, except not all printf implementations adhere to e.g. IEEE 1003.1 (POSIX specifications), causing some implementation specific format to be shown when a NaN is passed (or -NaN, given that the floating point implementation behaves as such).

E.g. MinGW targets (as they use MS Visual C library) display `[-]1.#J' (`[-]1#IND' truncated to %6.2f).  This might cause issues if a tool that uses the output data does not except this kind of behaviour (wouldn't call this undefined by any means).
Comment 1 Sourceware Commits 2019-12-05 07:09:19 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1769380a11463b907263ef5443de5d14559f2095

commit 1769380a11463b907263ef5443de5d14559f2095
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Dec 5 16:37:25 2019 +1030

    PR25244, --print-memory-usage, division by zero if MEMORY length is zero
    
    	PR 25244
    	* ldlang.c (lang_print_memory_usage): Don't print percent used
    	when length is zero.
Comment 2 Alan Modra 2019-12-05 07:18:35 UTC
Fixed.
Comment 3 Pekka Seppänen 2019-12-05 07:28:44 UTC
I think there's a slight issue with this patch:  It also omits the linefeed for that particular memory region, should the length be zero.
Comment 4 Sourceware Commits 2019-12-05 11:21:44 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2410edcd3176d81d0ab8d24afe69f0d59649f69e

commit 2410edcd3176d81d0ab8d24afe69f0d59649f69e
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Dec 5 21:29:21 2019 +1030

    Re: PR25244, --print-memory-usage, division by zero if MEMORY length is zero
    
    Do print the linefeed when length is zero.
    
    	PR 25244
    	* ldlang.c (lang_print_memory_usage): Correct last patch.
Comment 5 Alan Modra 2019-12-05 11:23:59 UTC
(In reply to Pekka Seppänen from comment #3)
> I think there's a slight issue with this patch:  It also omits the linefeed
> for that particular memory region, should the length be zero.

Thanks for noticiing.