diff options
author | Jay Phelps <hello@jayphelps.com> | 2017-02-27 19:54:16 -0800 |
---|---|---|
committer | Ben Smith <binjimin@gmail.com> | 2017-02-27 19:54:16 -0800 |
commit | 9e1654912d3ead429706caa4f152331fe0317d1e (patch) | |
tree | d357b933e8fd4a8783f728c103de604fd334014e /src | |
parent | b44543c99c8f044f8106e3d46f055a9e2380d9fb (diff) | |
download | wabt-9e1654912d3ead429706caa4f152331fe0317d1e.tar.gz wabt-9e1654912d3ead429706caa4f152331fe0317d1e.tar.bz2 wabt-9e1654912d3ead429706caa4f152331fe0317d1e.zip |
Use perror to output error messages when reading files (#318)
Use wabt_snprintf instead of unsafe sprintf
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-writer.cc | 4 | ||||
-rw-r--r-- | src/common.cc | 16 |
2 files changed, 13 insertions, 7 deletions
diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 2117c8e5..172fc9fc 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -626,8 +626,8 @@ static void write_global_header(Context* ctx, const WabtGlobal* global) { static void write_reloc_section(Context* ctx, RelocSection* reloc_section) { char section_name[128]; - sprintf(section_name, "%s.%s", WABT_BINARY_SECTION_RELOC, - reloc_section->name); + wabt_snprintf(section_name, sizeof(section_name), "%s.%s", + WABT_BINARY_SECTION_RELOC, reloc_section->name); begin_custom_section(ctx, section_name, LEB_SECTION_SIZE_GUESS); wabt_write_u32_leb128(&ctx->stream, reloc_section->section_code, "reloc section type"); diff --git a/src/common.cc b/src/common.cc index f2ab97d7..830a17ac 100644 --- a/src/common.cc +++ b/src/common.cc @@ -20,10 +20,13 @@ #include <stdio.h> #include <stdint.h> #include <string.h> +#include <limits.h> #if COMPILER_IS_MSVC #include <fcntl.h> #include <io.h> +#include <stdlib.h> +#define PATH_MAX _MAX_PATH #endif WabtOpcodeInfo g_wabt_opcode_info[WABT_NUM_OPCODES]; @@ -120,29 +123,32 @@ WabtResult wabt_read_file(const char* filename, size_t* out_size) { FILE* infile = fopen(filename, "rb"); if (!infile) { - fprintf(stderr, "unable to read file: %s\n", filename); + const char* format = "unable to read file %s"; + char msg[PATH_MAX + sizeof(format)]; + wabt_snprintf(msg, sizeof(msg), format, filename); + perror(msg); return WABT_ERROR; } if (fseek(infile, 0, SEEK_END) < 0) { - fprintf(stderr, "fseek to end failed.\n"); + perror("fseek to end failed"); return WABT_ERROR; } long size = ftell(infile); if (size < 0) { - fprintf(stderr, "ftell failed.\n"); + perror("ftell failed"); return WABT_ERROR; } if (fseek(infile, 0, SEEK_SET) < 0) { - fprintf(stderr, "fseek to beginning failed.\n"); + perror("fseek to beginning failed"); return WABT_ERROR; } void* data = wabt_alloc(size); if (size != 0 && fread(data, size, 1, infile) != 1) { - fprintf(stderr, "fread failed.\n"); + perror("fread failed"); return WABT_ERROR; } |