[PATCH][ARM] Use snprintf rather than sprintf where possible

Kyrill Tkachov kyrylo.tkachov@arm.com
Mon Nov 9 11:36:00 GMT 2015


Hi all,

Judging by the thread at https://gcc.gnu.org/ml/gcc-patches/2015-10/msg01912.html
I looked at replacing calls to sprintf with calls to snprintf in the arm backend.
We use them a lot to print assembly mnemonics into static char buffers.
This patch replaces the calls with snprintf and adds a size argument equal to the size
of the buffer used. This way, if any of the format strings changes/increases past the size
of the allocated buffer, snprintf will truncate it (and the assembler will catch it) rather
than trying to write past the end of the buffer with unexpected results.

I managed to replace all uses of sprintf in the arm backend except the one in aout.h:
#define ASM_GENERATE_INTERNAL_LABEL(STRING, PREFIX, NUM)  \
   sprintf (STRING, "*%s%s%u", LOCAL_LABEL_PREFIX, PREFIX, (unsigned int)(NUM))

Here, ASM_GENERATE_INTERNAL_LABEL is used in various places in the midend to print labels
to static buffers. I've seen those buffers have sizes ranging from 12 chars to 256 chars.
The size of the buffer that ASM_GENERATE_INTERNAL_LABEL can depend on is not mandated in the
documentation or passed down to the macro, so I think this is a bit dangerous. In practice, however,
I don't think we print labels that long that that would cause an issue.

Bootstrapped and tested on arm-none-linux-gnueabihf.

Ok for trunk?

Thanks,
Kyrill

2015-11-09  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     * config/arm/arm.c (arm_set_fixed_optab_libfunc): Use snprintf
     rather than sprintf.
     (arm_set_fixed_conv_libfunc): Likewise.
     (arm_option_override): Likewise.
     (neon_output_logic_immediate): Likewise.
     (neon_output_shift_immediate): Likewise.
     (arm_output_multireg_pop): Likewise.
     (vfp_output_vstmd): Likewise.
     (output_move_vfp): Likewise.
     (output_move_neon): Likewise.
     (output_return_instruction): Likewise.
     (arm_elf_asm_cdtor): Likewise.
     (arm_output_shift): Likewise.
     (arm_output_iwmmxt_shift_immediate): Likewise.
     (arm_output_iwmmxt_tinsr): Likewise.
     * config/arm/neon.md (*neon_mov<mode>, VDX): Likewise.
     (*neon_mov<mode>, VQXMOV): Likewise.
     (neon_vc<cmp_op><mode>_insn): Likewise.
     (neon_vc<cmp_op_unsp><mode>_insn_unspec): Likewise.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: arm-sprintf.patch
Type: text/x-patch
Size: 13921 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20151109/0f6b4134/attachment.bin>


More information about the Gcc-patches mailing list