Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

elf: clean up fill_note_info()

Introduce a helper function elf_note_info_init() to help fill_note_info()
to do initializations, also fix the potential memory leaks.

[akpm@linux-foundation.org: remove NUM_NOTES]
Signed-off-by: WANG Cong <amwang@redhat.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: David Howells <dhowells@redhat.com>
Cc: Roland McGrath <roland@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Amerigo Wang and committed by
Linus Torvalds
0cf062d0 d9588725

+30 -22
+30 -22
fs/binfmt_elf.c
··· 1711 1711 int numnote; 1712 1712 }; 1713 1713 1714 - static int fill_note_info(struct elfhdr *elf, int phdrs, 1715 - struct elf_note_info *info, 1716 - long signr, struct pt_regs *regs) 1714 + static int elf_note_info_init(struct elf_note_info *info) 1717 1715 { 1718 - #define NUM_NOTES 6 1719 - struct list_head *t; 1720 - 1721 - info->notes = NULL; 1722 - info->prstatus = NULL; 1723 - info->psinfo = NULL; 1724 - info->fpu = NULL; 1725 - #ifdef ELF_CORE_COPY_XFPREGS 1726 - info->xfpu = NULL; 1727 - #endif 1716 + memset(info, 0, sizeof(*info)); 1728 1717 INIT_LIST_HEAD(&info->thread_list); 1729 1718 1730 - info->notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), 1731 - GFP_KERNEL); 1719 + /* Allocate space for six ELF notes */ 1720 + info->notes = kmalloc(6 * sizeof(struct memelfnote), GFP_KERNEL); 1732 1721 if (!info->notes) 1733 1722 return 0; 1734 1723 info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL); 1735 1724 if (!info->psinfo) 1736 - return 0; 1725 + goto notes_free; 1737 1726 info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL); 1738 1727 if (!info->prstatus) 1739 - return 0; 1728 + goto psinfo_free; 1740 1729 info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL); 1741 1730 if (!info->fpu) 1742 - return 0; 1731 + goto prstatus_free; 1743 1732 #ifdef ELF_CORE_COPY_XFPREGS 1744 1733 info->xfpu = kmalloc(sizeof(*info->xfpu), GFP_KERNEL); 1745 1734 if (!info->xfpu) 1746 - return 0; 1735 + goto fpu_free; 1747 1736 #endif 1737 + return 1; 1738 + #ifdef ELF_CORE_COPY_XFPREGS 1739 + fpu_free: 1740 + kfree(info->fpu); 1741 + #endif 1742 + prstatus_free: 1743 + kfree(info->prstatus); 1744 + psinfo_free: 1745 + kfree(info->psinfo); 1746 + notes_free: 1747 + kfree(info->notes); 1748 + return 0; 1749 + } 1748 1750 1749 - info->thread_status_size = 0; 1751 + static int fill_note_info(struct elfhdr *elf, int phdrs, 1752 + struct elf_note_info *info, 1753 + long signr, struct pt_regs *regs) 1754 + { 1755 + struct list_head *t; 1756 + 1757 + if (!elf_note_info_init(info)) 1758 + return 0; 1759 + 1750 1760 if (signr) { 1751 1761 struct core_thread *ct; 1752 1762 struct elf_thread_status *ets; ··· 1816 1806 #endif 1817 1807 1818 1808 return 1; 1819 - 1820 - #undef NUM_NOTES 1821 1809 } 1822 1810 1823 1811 static size_t get_note_info_size(struct elf_note_info *info)