Remove '_s' functions, as they are not portable enough

This was discovered when the code was integrated with PCem, and tested by others.
This commit is contained in:
shermp
2020-11-15 23:05:52 +13:00
parent a73d10432e
commit 928870b3a6
4 changed files with 12 additions and 9 deletions

View File

@@ -98,7 +98,7 @@ static int mvhd_gen_par_loc(MVHDSparseHeader* header,
char child_dir[MVHD_MAX_PATH_BYTES] = {0};
size_t child_dir_len;
if (strlen(child_path) < sizeof child_dir) {
strcpy_s(child_dir, MVHD_MAX_PATH_BYTES, child_path);
strcpy(child_dir, child_path);
} else {
*err = MVHD_ERR_PATH_LEN;
rv = -1;

View File

@@ -171,7 +171,7 @@ static bool mvhd_parent_path_exists(struct MVHDPaths* paths, uint32_t plat_code)
if (plat_code == MVHD_DIF_LOC_W2RU && *paths->w2ru_path) {
cwk_ret = cwk_path_join((const char*)paths->dir_path, (const char*)paths->w2ru_path, paths->joined_path, sizeof paths->joined_path);
} else if (plat_code == MVHD_DIF_LOC_W2KU && *paths->w2ku_path) {
strncpy_s(paths->joined_path, sizeof paths->joined_path, (const char*)paths->w2ku_path, sizeof paths->joined_path);
memcpy(paths->joined_path, paths->w2ku_path, (sizeof paths->joined_path) - 1);
cwk_ret = 0;
} else if (plat_code == 0) {
cwk_ret = cwk_path_join((const char*)paths->dir_path, (const char*)paths->file_name, paths->joined_path, sizeof paths->joined_path);
@@ -182,7 +182,8 @@ static bool mvhd_parent_path_exists(struct MVHDPaths* paths, uint32_t plat_code)
f = mvhd_fopen((const char*)paths->joined_path, "rb", &ferr);
if (f != NULL) {
/* We found a file at the requested path! */
strncpy_s(tmp_open_path, sizeof tmp_open_path, paths->joined_path, sizeof tmp_open_path);
memcpy(tmp_open_path, paths->joined_path, (sizeof paths->joined_path) - 1);
tmp_open_path[sizeof tmp_open_path - 1] = '\0';
fclose(f);
return true;
} else {
@@ -225,7 +226,7 @@ static char* mvhd_get_diff_parent_path(MVHDMeta* vhdm, int* err) {
*err = MVHD_ERR_PATH_LEN;
goto paths_cleanup;
}
strncpy_s(paths->dir_path, sizeof paths->dir_path, vhdm->filename, dirlen);
memcpy(paths->dir_path, vhdm->filename, dirlen);
/* Get the filename field from the sparse header. */
utf_outlen = (int)sizeof paths->file_name;
utf_inlen = (int)sizeof vhdm->sparse.par_utf16_name;
@@ -373,7 +374,8 @@ MVHDMeta* mvhd_open(const char* path, bool readonly, int* err) {
*err = MVHD_ERR_PATH_LEN;
goto cleanup_vhdm;
}
strcpy_s(vhdm->filename, sizeof vhdm->filename, path);
//This is safe, as we've just checked for potential overflow above
strcpy(vhdm->filename, path);
vhdm->f = readonly ? mvhd_fopen((const char*)vhdm->filename, "rb", err) : mvhd_fopen((const char*)vhdm->filename, "rb+", err);
if (vhdm->f == NULL) {
/* note, mvhd_fopen sets err for us */

View File

@@ -139,8 +139,8 @@ FILE* mvhd_fopen(const char* path, const char* mode, int* err) {
int path_res = UTF8ToUTF16LE((unsigned char*)new_path, &new_path_len, (const unsigned char*)path, (int*)&path_len);
int mode_res = UTF8ToUTF16LE((unsigned char*)mode_str, &new_mode_len, (const unsigned char*)mode, (int*)&mode_len);
if (path_res > 0 && mode_res > 0) {
errno_t wfopen_err = _wfopen_s(&f, new_path, mode_str);
if (wfopen_err != 0 || f == NULL) {
f = _wfopen(new_path, mode_str);
if (f == NULL) {
mvhd_errno = errno;
*err = MVHD_ERR_FILE;
}
@@ -320,4 +320,4 @@ uint32_t mvhd_file_mod_timestamp(const char* path, int *err) {
}
return mvhd_epoch_to_vhd_ts(file_stat.st_mtime);
#endif
}
}

View File

@@ -3,6 +3,7 @@
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include "minivhd_internal.h"
#include "minivhd.h"
#define MVHD_START_TS 946684800
@@ -132,4 +133,4 @@ uint32_t mvhd_crc32(const void* data, size_t n_bytes);
* 'err' will be set to non-zero on error
*/
uint32_t mvhd_file_mod_timestamp(const char* path, int *err);
#endif
#endif