projects
/
open-adventure.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
a044f10
)
Use strdup() instead of strncpy().
author
Jason S. Ninneman
<jsn@mbar.us>
Mon, 12 Jun 2017 16:21:24 +0000
(09:21 -0700)
committer
Eric S. Raymond
<esr@thyrsus.com>
Mon, 12 Jun 2017 21:36:06 +0000
(21:36 +0000)
advent.h
patch
|
blob
|
history
misc.c
patch
|
blob
|
history
diff --git
a/advent.h
b/advent.h
index f831a7b487d8a472bf00ca6748790e05374961e4..d34310663c8f148a4efb7882862249c91b04e789 100644
(file)
--- a/
advent.h
+++ b/
advent.h
@@
-87,6
+87,7
@@
extern lcg_state lcgstate;
#define READ_MODE "rb"
#define WRITE_MODE "wb"
extern void* xmalloc(size_t);
#define READ_MODE "rb"
#define WRITE_MODE "wb"
extern void* xmalloc(size_t);
+extern char* xstrdup(const char*);
extern void packed_to_token(long, char token[6]);
extern void newspeak(char*);
extern void PSPEAK(vocab_t,int);
extern void packed_to_token(long, char token[6]);
extern void newspeak(char*);
extern void PSPEAK(vocab_t,int);
diff --git
a/misc.c
b/misc.c
index d89b4058fbcfd003329b3a470b8fed765b7ba93e..6dd24e0c89f6ced0e9b67c7a162b3caf6e9b82e6 100644
(file)
--- a/
misc.c
+++ b/
misc.c
@@
-21,6
+21,17
@@
void* xmalloc(size_t size)
return(ptr);
}
return(ptr);
}
+char* xstrdup(const char* s)
+{
+ char* ptr = strdup(s);
+ if (ptr == NULL)
+ {
+ fprintf(stderr, "Out of memory!\n");
+ exit(EXIT_FAILURE);
+ }
+ return(ptr);
+}
+
void packed_to_token(long packed, char token[6])
{
// Unpack and map back to ASCII.
void packed_to_token(long packed, char token[6])
{
// Unpack and map back to ASCII.
@@
-60,8
+71,7
@@
void newspeak(char* msg)
printf("\n");
// Create a copy of our string, so we can edit it.
printf("\n");
// Create a copy of our string, so we can edit it.
- char* copy = (char*) xmalloc(strlen(msg) + 1);
- strncpy(copy, msg, strlen(msg) + 1);
+ char* copy = xstrdup(msg);
// Staging area for stringified parameters.
char parameters[5][100]; // FIXME: to be replaced with dynamic allocation
// Staging area for stringified parameters.
char parameters[5][100]; // FIXME: to be replaced with dynamic allocation