记第一次提交patch给开源项目的经历

近期由于工作的原因接触到了openldap的开源库,在一次偶然的代码review的情况下发现了一个内存泄漏的bug。对,不是测试发现的,是代码审核看出来的。

问题代码

该项目github源码路径为:https://github.com/openldap/openldap/blob/OPENLDAP_REL_ENG_2_4_49/libraries/liblunicode/ucstr.c

为方便看这里贴出来关键部分代码

struct berval * UTF8bvnormalize(
	struct berval *bv,
	struct berval *newbv,
	unsigned flags,
	void *ctx )
{
	int i, j, len, clen, outpos, ucsoutlen, outsize, last;
	char *out, *outtmp, *s;
	ac_uint4 *ucs, *p, *ucsout;

	static unsigned char mask[] = {
		0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };

	unsigned casefold = flags & LDAP_UTF8_CASEFOLD;
	unsigned approx = flags & LDAP_UTF8_APPROX;

	if ( bv == NULL ) {
		return NULL;
	}

	s = bv->bv_val;
	len = bv->bv_len;

	if ( len == 0 ) {
		return ber_dupbv_x( newbv, bv, ctx );
	}

	/* 当newbv为NULL时动态分配了内存 */
	if ( !newbv ) {
		newbv = ber_memalloc_x( sizeof(struct berval), ctx );
		if ( !newbv ) return NULL;
	}

	/* Should first check to see if string is already in proper
	 * normalized form. This is almost as time consuming as
	 * the normalization though.
	 */

	/* finish off everything up to character before first non-ascii */
	if ( LDAP_UTF8_ISASCII( s ) ) {
		if ( casefold ) {
			outsize = len + 7;
			out = (char *) ber_memalloc_x( outsize, ctx );
			if ( out == NULL ) {
				return NULL;    /* 异常退出未释放newbv */
			}
            ...
        }
        ...
    }
    ...
}

如注释中所描述的,UTF8bvnormalize()函数中类似的异常return还有好几处!

创建bug

作为一名基于百度开发的程序员,熟练的键入ldap并打开openldap的官网:https://www.openldap.org/,凭借着未过四级的优异英文水平,很快在首页上找到了bug跟踪入口:

记第一次提交patch给开源项目的经历_第1张图片

进去之后按提示,依次【File a Bug】,选择问题库【Open LDAP】,填写title Bug 9198 - libraries: memory leak in UTF8bvnormalize()及一些详细信息。

提交patch

从github上面clone下来openldap的源码,checkout到发现问题的最新版OPENLDAP_REL_ENG_2_4_49,修改之后commit一下。

使用如下命令生成一个补丁:

git format-patch HEAD^

 意为用当前最新的一次commit id的代码生成了补丁,生成的补丁就在当前目录下,名字类似这样的:0001-fix-Bug-9198-libraries-memory-leak-in-UTF8bvnormaliz.patch 。

再回到之前创建bug的页面,点【Add an attachment】将自己做好的补丁提交上去,备注必要信息,bug状态改成FIX即可。

在bug页面:https://bugs.openldap.org/show_bug.cgi?id=9198 可以查看我到提交的补丁。

如果审核通过的话,大概会被合入到最新分支吧。

你可能感兴趣的:(教程,github,c语言,openldap,ldap)