Skip to content

Commit

Permalink
Use vasprintf when available.
Browse files Browse the repository at this point in the history
Use strlcpy when available.

Add xcode project.
  • Loading branch information
michaelrsweet committed Mar 29, 2017
1 parent 1272c3d commit 6ac1fb2
Show file tree
Hide file tree
Showing 12 changed files with 1,958 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ org.minixml.atom
org.minixml.docset
org.minixml.xar
temp1.xmlfd
test.xmlfd
testmxml
test1.xml
xcode/mxml.xcodeproj/project.xcworkspace
xcode/mxml.xcodeproj/xcuserdata
9 changes: 8 additions & 1 deletion config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@


/*
* Do we have the snprintf() and vsnprintf() functions?
* Do we have the *printf() functions?
*/

#undef HAVE_SNPRINTF
#undef HAVE_VASPRINTF
#undef HAVE_VSNPRINTF


Expand All @@ -57,6 +58,7 @@
*/

#undef HAVE_STRDUP
#undef HAVE_STRLCPY


/*
Expand All @@ -75,6 +77,11 @@ extern char *_mxml_strdup(const char *);
# define strdup _mxml_strdup
# endif /* !HAVE_STRDUP */

# ifndef HAVE_STRLCPY
extern size_t _mxml_strlcpy(char *, const char *, size_t);
# define strlcpy _mxml_strlcpy
# endif /* !HAVE_STRLCPY */

extern char *_mxml_strdupf(const char *, ...);
extern char *_mxml_vstrdupf(const char *, va_list);

Expand Down
11 changes: 6 additions & 5 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -3899,12 +3899,13 @@ esac


if test "x$use_ansi" != xyes; then
for ac_func in strdup
for ac_func in strdup strlcpy
do :
ac_fn_c_check_func "$LINENO" "strdup" "ac_cv_func_strdup"
if test "x$ac_cv_func_strdup" = xyes; then :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_STRDUP 1
#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
_ACEOF

fi
Expand All @@ -3913,7 +3914,7 @@ done
fi

if test "x$use_vsnprintf" != xyes; then
for ac_func in snprintf vsnprintf
for ac_func in snprintf vasprintf vsnprintf
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ AC_C_INLINE

dnl Checks for string functions.
if test "x$use_ansi" != xyes; then
AC_CHECK_FUNCS(strdup)
AC_CHECK_FUNCS(strdup strlcpy)
fi

if test "x$use_vsnprintf" != xyes; then
AC_CHECK_FUNCS(snprintf vsnprintf)
AC_CHECK_FUNCS(snprintf vasprintf vsnprintf)
fi

dnl Check for "long long" support...
Expand Down
16 changes: 8 additions & 8 deletions mxml-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ mxmlSaveString(mxml_node_t *node, /* I - Node to write */
* Return the number of characters...
*/

return (ptr[0] - buffer);
return ((int)(ptr[0] - buffer));
}


Expand Down Expand Up @@ -1016,7 +1016,7 @@ mxml_fd_read(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */
* Read from the file descriptor...
*/

while ((bytes = read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)
while ((bytes = (int)read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)
#ifdef EINTR
if (errno != EAGAIN && errno != EINTR)
#else
Expand Down Expand Up @@ -1068,7 +1068,7 @@ mxml_fd_write(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */
*/

for (ptr = buf->buffer; ptr < buf->current; ptr += bytes)
if ((bytes = write(buf->fd, ptr, buf->current - ptr)) < 0)
if ((bytes = (int)write(buf->fd, ptr, buf->current - ptr)) < 0)
return (-1);

/*
Expand Down Expand Up @@ -1352,9 +1352,9 @@ mxml_get_entity(mxml_node_t *parent, /* I - Parent node */
if (entity[0] == '#')
{
if (entity[1] == 'x')
ch = strtol(entity + 2, NULL, 16);
ch = (int)strtol(entity + 2, NULL, 16);
else
ch = strtol(entity + 1, NULL, 10);
ch = (int)strtol(entity + 1, NULL, 10);
}
else if ((ch = mxmlEntityGetValue(entity)) < 0)
mxml_error("Entity name \"%s;\" not supported under parent <%s>!",
Expand Down Expand Up @@ -1446,7 +1446,7 @@ mxml_load_data(
switch (type)
{
case MXML_INTEGER :
node = mxmlNewInteger(parent, strtol(buffer, &bufptr, 0));
node = mxmlNewInteger(parent, (int)strtol(buffer, &bufptr, 0));
break;

case MXML_OPAQUE :
Expand Down Expand Up @@ -2761,7 +2761,7 @@ mxml_write_node(mxml_node_t *node, /* I - Node to write */
i > 0;
i --, attr ++)
{
width = strlen(attr->name);
width = (int)strlen(attr->name);

if (attr->value)
width += strlen(attr->value) + 3;
Expand Down Expand Up @@ -2932,7 +2932,7 @@ mxml_write_node(mxml_node_t *node, /* I - Node to write */
if ((newline = strrchr(data, '\n')) == NULL)
col += strlen(data);
else
col = strlen(newline);
col = (int)strlen(newline);

free(data);
break;
Expand Down
3 changes: 2 additions & 1 deletion mxml-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ mxmlIndexFind(mxml_index_t *ind, /* I - Index to search */
{
#ifdef DEBUG
puts(" returning NULL...");
printf(" ind->attr=\"%s\"\n", ind->attr ? ind->attr : "(null)");
if (ind)
printf(" ind->attr=\"%s\"\n", ind->attr ? ind->attr : "(null)");
#endif /* DEBUG */

return (NULL);
Expand Down
49 changes: 49 additions & 0 deletions mxml-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,53 @@ _mxml_strdupf(const char *format, /* I - Printf-style format string */
*/

va_start(ap, format);
#ifdef HAVE_VASPRINTF
vasprintf(&s, format, ap);
#else
s = _mxml_vstrdupf(format, ap);
#endif /* HAVE_VASPRINTF */
va_end(ap);

return (s);
}


#ifndef HAVE_STRLCPY
/*
* '_mxml_strlcpy()' - Safely copy a string.
*/

size_t /* O - Number of bytes copied */
_mxml_strlcpy(char *dst, /* I - Destination buffer */
const char *src, /* I - Source string */
size_t dstsize) /* I - Size of destinatipon buffer */
{
size_t srclen; /* Length of source string */


/*
* Figure out how much room is needed...
*/

dstsize --;

srclen = strlen(src);

/*
* Copy the appropriate amount...
*/

if (srclen > dstsize)
srclen = dstsize;

memmove(dst, src, srclen);
dst[srclen] = '\0';

return (srclen);
}
#endif /* !HAVE_STRLCPY */


#ifndef HAVE_VSNPRINTF
/*
* '_mxml_vsnprintf()' - Format a string into a fixed size buffer.
Expand Down Expand Up @@ -423,6 +463,14 @@ char * /* O - New string pointer */
_mxml_vstrdupf(const char *format, /* I - Printf-style format string */
va_list ap) /* I - Pointer to additional arguments */
{
#ifdef HAVE_VASPRINTF
char *s; /* String */

vasprintf(&s, format, ap);

return (s);

#else
int bytes; /* Number of bytes required */
char *buffer, /* String buffer */
temp[256]; /* Small buffer for first vsnprintf */
Expand Down Expand Up @@ -459,4 +507,5 @@ _mxml_vstrdupf(const char *format, /* I - Printf-style format string */
*/

return (buffer);
#endif /* HAVE_VASPRINTF */
}
11 changes: 5 additions & 6 deletions mxmldoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ add_variable(mxml_node_t *parent, /* I - Parent node */
if (node->value.text.whitespace && bufptr > buffer)
*bufptr++ = ' ';

strcpy(bufptr, node->value.text.string);
strlcpy(bufptr, node->value.text.string, sizeof(buffer) - (size_t)(bufptr - buffer));

next = node->next;
mxmlDelete(node);
Expand Down Expand Up @@ -673,7 +673,7 @@ add_variable(mxml_node_t *parent, /* I - Parent node */
if (node->value.text.whitespace && bufptr > buffer)
*bufptr++ = ' ';

strcpy(bufptr, node->value.text.string);
strlcpy(bufptr, node->value.text.string, sizeof(buffer) - (size_t)(bufptr - buffer));

next = node->next;
mxmlDelete(node);
Expand All @@ -686,7 +686,7 @@ add_variable(mxml_node_t *parent, /* I - Parent node */
* Handle "type name"...
*/

strcpy(buffer, type->last_child->value.text.string);
strlcpy(buffer, type->last_child->value.text.string, sizeof(buffer));
mxmlDelete(type->last_child);
}

Expand Down Expand Up @@ -797,8 +797,7 @@ get_comment_info(
return ("<span class=\"info\">&nbsp;DEPRECATED&nbsp;</span>");
else if (!strncmp(ptr, "@since ", 7))
{
strncpy(since, ptr + 7, sizeof(since) - 1);
since[sizeof(since) - 1] = '\0';
strlcpy(since, ptr + 7, sizeof(since));

if ((ptr = strchr(since, '@')) != NULL)
*ptr = '\0';
Expand Down Expand Up @@ -1215,7 +1214,7 @@ scan_file(const char *filename, /* I - Filename */
if (node->value.text.whitespace && bufptr > buffer)
*bufptr++ = ' ';

strcpy(bufptr, node->value.text.string);
strlcpy(bufptr, node->value.text.string, sizeof(buffer) - (size_t)(bufptr - buffer));

next = node->next;
mxmlDelete(node);
Expand Down
5 changes: 5 additions & 0 deletions testmxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,9 @@ main(int argc, /* I - Number of command-line args */
}
}

if (getenv("TEST_DELAY") != NULL)
sleep(atoi(getenv("TEST_DELAY")));

/*
* Return...
*/
Expand Down Expand Up @@ -736,6 +739,8 @@ sax_cb(mxml_node_t *node, /* I - Current node */
};


(void)data;

/*
* This SAX callback just counts the different events.
*/
Expand Down
Loading

0 comments on commit 6ac1fb2

Please sign in to comment.