mini XML里mxmlSaveString中的一个bug

原作者已确认,Bug ID: 524
http://www.msweet.org/bugs.php?L+Z3
API文档对该函数的描述如下:

/*
 * 'mxmlSaveString()' - Save an XML node tree to a string.
 *
 * This function returns the total number of bytes that would be
 * required for the string but only copies (bufsize - 1) characters
 * into the specified buffer.
 */

int                 /* O - Size of string */
mxmlSaveString(mxml_node_t    *node,    /* I - Node to write */
               char           *buffer,  /* I - String buffer */
               int            bufsize,  /* I - Size of string buffer */
               mxml_save_cb_t cb)   /* I - Whitespace callback or MXML_NO_CALLBACK */
{
  int   col;                /* Final column */
  char  *ptr[2];            /* Pointers for putc_cb */
  _mxml_global_t *global = _mxml_global();
                    /* Global data */


 /*
  * Write the node...
  */

  ptr[0] = buffer;
  ptr[1] = buffer + bufsize;

  if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc, global)) < 0)
    return (-1);

  if (col > 0)
    mxml_string_putc('\n', ptr);

 /*
  * Nul-terminate the buffer...
  */

  if (ptr[0] >= ptr[1])
    buffer[bufsize - 1] = '\0';
  else
    ptr[0][0] = '\0';

 /*
  * Return the number of characters...
  */

  return (ptr[0] - buffer);
}

但是,如果像下面这样调用(这样调用的目的是统计节点中的字符数,并不是真正的想去存字符),就会导致段错误:

`
FILE *fp = fopen("test.xml", "r");
mxml_node_t *top = mxmlLoadFile(NULL,fp,NULL);
int num = mxmlSaveString(top,NULL,0,NULL);

这是由于,源码中下面这一句
buffer[bufsize - 1] = ‘\0’;
入参buffer被写入,但是由于我们传入的是NULL,所以挂了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值