Call it from a few simple places.
/* Call calloc(3), exiting on NULL return. */
void *mh_xcalloc(size_t nelem, size_t elsize);
/* Call calloc(3), exiting on NULL return. */
void *mh_xcalloc(size_t nelem, size_t elsize);
+/* Duplicate a NUL-terminated string, exit on failure. */
+char *mh_xstrdup(char *src);
+
/* Call free(3), if ptr isn't NULL. */
void mh_xfree(void *ptr);
/* Call free(3), if ptr isn't NULL. */
void mh_xfree(void *ptr);
while ((t = token(tokval)) && t != MACH && t != DEFAULT) {
switch(t) {
case LOGIN:
while ((t = token(tokval)) && t != MACH && t != DEFAULT) {
switch(t) {
case LOGIN:
- if (token(tokval) && *aname == 0) {
- *aname = mh_xmalloc((size_t) strlen(tokval) + 1);
- strcpy(*aname, tokval);
- }
+ if (token(tokval) && *aname == 0)
+ *aname = mh_xstrdup(tokval);
adios(NULL, "Remove password or correct file "
"permissions.");
}
adios(NULL, "Remove password or correct file "
"permissions.");
}
- if (token(tokval) && *apass == 0) {
- *apass = mh_xmalloc((size_t) strlen(tokval) + 1);
- strcpy(*apass, tokval);
- }
+ if (token(tokval) && *apass == 0)
+ *apass = mh_xstrdup(tokval);
- *aname = mh_xmalloc((size_t) strlen(myname) + 1);
- strcpy (*aname, myname);
+ *aname = mh_xstrdup(myname);
- *apass = mh_xmalloc((size_t) strlen(mypass) + 1);
- strcpy (*apass, mypass);
+ *aname = mh_xstrdup(mypass);
+/* Duplicate a NUL-terminated string, exit on failure. */
+char *mh_xstrdup(char *src)
+{
+ size_t n;
+ char *dest;
+
+ n = strlen(src) + 1; /* Ignore possibility of overflow. */
+ dest = mh_xmalloc(n);
+ memcpy(dest, src, n);
+
+ return dest;
+}
+
/* Call free(3), if ptr isn't NULL. */
void mh_xfree(void *ptr)
{
/* Call free(3), if ptr isn't NULL. */
void mh_xfree(void *ptr)
{