Browse Source

Merge pull request #30 from LefterisJP/fix_windows_warnings

Fix msvc compile warnings
cl-refactor
Gav Wood 10 years ago
parent
commit
3e025ed423
  1. 4
      internal.c
  2. 2
      io.c
  3. 39
      io.h
  4. 12
      io_posix.c
  5. 13
      io_win32.c

4
internal.c

@ -87,7 +87,7 @@ ethash_cache *ethash_cache_new(ethash_params const *params, ethash_h256_t const
if (!ret) { if (!ret) {
return NULL; return NULL;
} }
ret->mem = malloc(params->cache_size); ret->mem = malloc((size_t)params->cache_size);
if (!ret->mem) { if (!ret->mem) {
goto fail_free_cache; goto fail_free_cache;
} }
@ -362,7 +362,7 @@ ethash_full_t ethash_full_new(ethash_params const* params,
} }
ret->cache = (ethash_cache*)cache; ret->cache = (ethash_cache*)cache;
ret->data = malloc(params->full_size); ret->data = malloc((size_t)params->full_size);
if (!ret->data) { if (!ret->data) {
goto fail_free_full; goto fail_free_full;
} }

2
io.c

@ -36,7 +36,7 @@ static bool ethash_io_write_file(char const *dirname,
if (!fullname) { if (!fullname) {
return false; return false;
} }
FILE *f = fopen(fullname, "wb"); FILE *f = ethash_fopen(fullname, "wb");
if (!f) { if (!f) {
goto free_name; goto free_name;
} }

39
io.h

@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include "ethash.h" #include "ethash.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -84,6 +85,37 @@ bool ethash_io_write(char const *dirname,
uint8_t **data, uint8_t **data,
uint64_t *data_size); uint64_t *data_size);
/**
* An fopen wrapper for no-warnings crossplatform fopen.
*
* Msvc compiler considers fopen to be insecure and suggests to use their
* alternative. This is a wrapper for this alternative. Another way is to
* #define _CRT_SECURE_NO_WARNINGS, but disabling all security warnings does
* not sound like a good idea.
*
* @param file_name The path to the file to open
* @param mode Opening mode. Check fopen()
* @return The FILE* or NULL in failure
*/
FILE *ethash_fopen(const char *file_name, const char *mode);
/**
* An stncat wrapper for no-warnings crossplatform strncat.
*
* Msvc compiler considers strncat to be insecure and suggests to use their
* alternative. This is a wrapper for this alternative. Another way is to
* #define _CRT_SECURE_NO_WARNINGS, but disabling all security warnings does
* not sound like a good idea.
*
* @param des Destination buffer
* @param dest_size Maximum size of the destination buffer. This is the
* extra argument for the MSVC secure strncat
* @param src Souce buffer
* @param count Number of bytes to copy from source
* @return If all is well returns the dest buffer. If there is an
* error returns NULL
*/
char *ethash_strncat(char *dest, size_t dest_size, const char *src, size_t count);
static inline void ethash_io_serialize_info(uint32_t revision, static inline void ethash_io_serialize_info(uint32_t revision,
ethash_h256_t seed_hash, ethash_h256_t seed_hash,
char *output) char *output)
@ -97,15 +129,16 @@ static inline char *ethash_io_create_filename(char const *dirname,
char const* filename, char const* filename,
size_t filename_length) size_t filename_length)
{ {
size_t dirlen = strlen(dirname);
// in C the cast is not needed, but a C++ compiler will complain for invalid conversion // in C the cast is not needed, but a C++ compiler will complain for invalid conversion
char *name = (char*)malloc(strlen(dirname) + filename_length); char *name = (char*)malloc(dirlen + filename_length + 1);
if (!name) { if (!name) {
return NULL; return NULL;
} }
name[0] = '\0'; name[0] = '\0';
strcat(name, dirname); ethash_strncat(name, dirlen + filename_length + 1, dirname, dirlen);
strcat(name, filename); ethash_strncat(name, dirlen + filename_length + 1, filename, filename_length);
return name; return name;
} }

12
io_posix.c

@ -27,6 +27,16 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
FILE *ethash_fopen(const char *file_name, const char *mode)
{
return fopen(file_name, mode);
}
char *ethash_strncat(char *dest, size_t dest_size, const char *src, size_t count)
{
return strlen(dest) + count + 1 <= dest_size ? strncat(dest, src, count) : NULL;
}
enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_h256_t seedhash) enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_h256_t seedhash)
{ {
char read_buffer[DAG_MEMO_BYTESIZE]; char read_buffer[DAG_MEMO_BYTESIZE];
@ -45,7 +55,7 @@ enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_h256_t seedhash)
} }
// try to open memo file // try to open memo file
FILE *f = fopen(memofile, "rb"); FILE *f = ethash_fopen(memofile, "rb");
if (!f) { if (!f) {
// file does not exist, so no checking happens. All is fine. // file does not exist, so no checking happens. All is fine.
ret = ETHASH_IO_MEMO_MISMATCH; ret = ETHASH_IO_MEMO_MISMATCH;

13
io_win32.c

@ -24,6 +24,17 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
FILE *ethash_fopen(const char *file_name, const char *mode)
{
FILE *f;
return fopen_s(&f, file_name, mode) == 0 ? f : NULL;
}
char *ethash_strncat(char *dest, size_t dest_size, const char *src, size_t count)
{
return strncat_s(dest, dest_size, src, count) == 0 ? dest : NULL;
}
enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_h256_t seedhash) enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_h256_t seedhash)
{ {
char read_buffer[DAG_MEMO_BYTESIZE]; char read_buffer[DAG_MEMO_BYTESIZE];
@ -42,7 +53,7 @@ enum ethash_io_rc ethash_io_prepare(char const *dirname, ethash_h256_t seedhash)
} }
// try to open memo file // try to open memo file
FILE *f = fopen(memofile, "rb"); FILE *f = ethash_fopen(memofile, "rb");
if (!f) { if (!f) {
// file does not exist, so no checking happens. All is fine. // file does not exist, so no checking happens. All is fine.
ret = ETHASH_IO_MEMO_MISMATCH; ret = ETHASH_IO_MEMO_MISMATCH;

Loading…
Cancel
Save