0
回答
哈,我可以说这是个bug吗?flex 2.5.36
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

不知道最新的flex这段代码怎么处理的,但针对 2.5.36想来是不对的。下面代码来源于 regex.c文件 flex 2.5.36的工程。使用 regmatch_dup是能把完整字符串复制出来,而regmatch_cpy会漏掉一个。错误发生在,snprintf(dest,regmatch_len(m),"%s",src);的"%s"上,会将长度中最后一个存储位置,写\0。regmatch_len返回的是实际匹配到的字符串的长度。

/** Extract a copy of the match, or NULL if no match.
 * @param m A match as returned by regexec().
 * @param src The source string that was passed to regexec().
 * @return The allocated string.
 */
char   *regmatch_dup (regmatch_t * m, const char *src)
{
	char   *str;
	int     len;

	if (m == NULL || m->rm_so < 0)
		return NULL;
	len = m->rm_eo - m->rm_so;
	str = (char *) flex_alloc ((len + 1) * sizeof (char));
	if (!str)
		flexfatal(_("Unable to allocate a copy of the match"));
	strncpy (str, src + m->rm_so, len);
	str[len] = 0;
	return str;
}

/** Copy the match.
 * @param m A match as returned by regexec().
 * @param dest The destination buffer.
 * @param src The source string that was passed to regexec().
 * @return dest
 */
char   *regmatch_cpy (regmatch_t * m, char *dest, const char *src)
{
	if (m == NULL || m->rm_so < 0) {
		if (dest)
			dest[0] = '\0';
		return dest;
	}

	snprintf (dest, regmatch_len(m), "%s", src + m->rm_so);
    return dest;
}

/** Get the length in characters of the match.
 * @param m A match as returned by regexec().
 * @param src The source string that was passed to regexec().
 * @return The length of the match.
 */
int regmatch_len (regmatch_t * m)
{
	if (m == NULL || m->rm_so < 0) {
		return 0;
	}

	return m->rm_eo - m->rm_so;
}



提这个啥意思呢,两方面:

1、bug处处有。。。。看到一个算一个咯,没空去核对最新的版本的,大家如果用2.5.36及之前的,这个地方要注意一下。。。

2、开源的软件拿来用用无所谓,sudo apt-get了,开源的源码拿来就不是直接ctrl+c ,ctrl+v了,不洗洗不行的。。

举报
中山野鬼
发帖于3年前 0回/254阅
顶部