Skip to content

Commit

Permalink
replaced strlcat with compat function
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
mdhender committed Nov 14, 2023
1 parent 4424cde commit 993c57c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ add_executable(fh
src/update.c src/update.h
src/version.c src/version.h
src/cjson/cJSON.c src/cjson/cJSON.h
src/cjson/helpers.c src/cjson/helpers.h)
src/cjson/helpers.c src/cjson/helpers.h
src/memsafe.c src/memsafe.h)

target_link_libraries(fh ${USE_LIB_M})
3 changes: 2 additions & 1 deletion src/cjson/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
#include "helpers.h"
#include "../memsafe.h"


void jsonAddIntToArray(cJSON *array, const char *arrayName, int value) {
Expand Down Expand Up @@ -102,7 +103,7 @@ void jsonGetString(cJSON *obj, const char *property, char *dst, int size) {
fprintf(stderr, "jsonGetString: strlen %d exceeds limit %d\n", (int) strlen(item->valuestring) + 1, size);
exit(2);
}
strncpy(dst, item->valuestring, size);
zstrcpy(dst, item->valuestring, size);
}


Expand Down
25 changes: 25 additions & 0 deletions src/memsafe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Created by Michael Henderson on 11/14/23.
//

#include "memsafe.h"

// zstrcpy safely copies a c-string from src to dst, taking care
// to avoid overflow and to always terminate the destination.
// The "size" parameter is the size (in bytes) of the destination.
// if the source is shorter than the destination, the remainder
// of the destination will be filled with zeroes.
int zstrcpy(char *dst, const char *src, size_t size) {
int s = 0; // index into source buffer
int d = 0; // index into destination buffer
if (size > 0) {
for (; d < size; d++) {
dst[d] = src[s];
if (src[s] != 0) {
s++;
}
}
dst[size-1] = 0;
}
return s;
}
32 changes: 32 additions & 0 deletions src/memsafe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Far Horizons Game Engine
// Copyright (c) 2023 Michael D Henderson
// Copyright (C) 2021 Raven Zachary
// Copyright (C) 2019 Casey Link, Adam Piggott
// Copyright (C) 1999 Richard A. Morneau
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//
// Created by Michael Henderson on 11/14/23.
//

#ifndef FAR_HORIZONS_MEMSAFE_H
#define FAR_HORIZONS_MEMSAFE_H

#include <stddef.h>

// memory safe c-string copy, copied from strlcat
int zstrcpy(char *dst, const char *src, size_t size);

#endif //FAR_HORIZONS_MEMSAFE_H
3 changes: 2 additions & 1 deletion src/namplaio.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "nampla.h"
#include "namplaio.h"
#include "planetio.h"
#include "memsafe.h"


/* load named planet data from file and create empty slots for future use */
Expand Down Expand Up @@ -194,7 +195,7 @@ void save_nampla_data(struct nampla_data *namplaData, int numNamplas, FILE *fp)
for (int i = 0; i < numNamplas; i++) {
struct nampla_data *nampla = &namplaData[i];
binary_nampla_data_t *data = &binData[i];
strncpy((char *)(data->name), nampla->name, 32);
zstrcpy((char *)(data->name), nampla->name, 32);
data->x = nampla->x;
data->y = nampla->y;
data->z = nampla->z;
Expand Down
3 changes: 2 additions & 1 deletion src/shipio.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ship.h"
#include "shipio.h"
#include "item.h"
#include "memsafe.h"


/* load ship data from file and create empty slots for future use */
Expand Down Expand Up @@ -95,7 +96,7 @@ void save_ship_data(struct ship_data *shipData, int numShips, FILE *fp) {
for (int i = 0; i < numShips; i++) {
struct ship_data *s = &shipData[i];
binary_ship_data_t *sd = &binData[i];
strncpy((char *) (sd->name), s->name, 32);
zstrcpy((char *) (sd->name), s->name, 32);
sd->x = s->x;
sd->y = s->y;
sd->z = s->z;
Expand Down
2 changes: 1 addition & 1 deletion src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int versionCommand(int argc, char *argv[]) {
}
}

printf("7.5.7\n");
printf("7.5.9\n");

return 2;
}

0 comments on commit 993c57c

Please sign in to comment.