https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65089
--- Comment #17 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> --- Author: jvdelisle Date: Sat Nov 7 18:13:17 2015 New Revision: 229935 URL: https://gcc.gnu.org/viewcvs?rev=229935&root=gcc&view=rev Log: 2015-11-07 Jerry DeLisle <jvdeli...@gcc.gnu.org> Backport from trunk PR libgfortran/65089 * io/format.h (free_format): New function to free memory allocated for building format error messages. * io/format.c (format_error): Add checks before freeing memory to avoid potential segfaults and free formatting data when needed on error conditions. Always allocate and NULL terminate the string. * io/transfer.c (st_read_done, st_write_done): Use new free_format function to clean up memory allocations when done. Modified: branches/gcc-5-branch/libgfortran/ChangeLog branches/gcc-5-branch/libgfortran/io/format.c branches/gcc-5-branch/libgfortran/io/format.h branches/gcc-5-branch/libgfortran/io/transfer.c