tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: rpcgen(1), issues with System XVI



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 20.09.2015 16:29, Christos Zoulas wrote:
> In article <55FE948F.70009%gmx.com@localhost>, Kamil Rytarowski
> <n54%gmx.com@localhost> wrote:
>> -=-=-=-=-=-
>> 
>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256
>> 
>> I was playing with System XVI [1] and I have found two bugs in
>> rpcgen(1) : - - NULL pointer dereference in
>> rpc_hout.c:pargdef(), - - generating invalid symbol names for
>> header guards.
>> 
>> The patches are attached to this mail.
>> 
>> OK to commit?
> 
> malloc sets errno, why use errx?
> 

I didn't want to be better the the rest of this file, eg:

        res = alloc(strlen(file) + strlen(ext) + 1);
        if (res == NULL) {
                errx(1, "Out of memory");
        }


Where rpc_util.h contains:
#define alloc(size)		((char *)malloc((size_t)(size)))

However there are places when we check for it and use EXIT_FAILURE.

I rediffed the patch. I also changed the parsing logic for the file
extension - stop at the final dot and leave the rest as it is, since
it will be truncated later.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJV/szwAAoJEEuzCOmwLnZs36UP/2MmGScZYILE9x2Yg/64BwX8
WvC63xLd394a+9kUWY/zyyM4Rkq2rgqxjczqnBmIyBawCK1AdqaTaxzkiMKEx+X2
MxAY70at+p2khn0J+Kmska3632djkb5BlQugJsXieOvTglEWFakKQ15u9FaNSk7I
uuLFrAb6lvs7b/H6A9o0FIN0t/plRB/PkIo5y66t+mWMZ96HhC8RxuhsxVZEqIPf
0QCnsynxFQUovw/zRhz6yorr/iz33KbrojYXU8lwzkpzXZDvV5GLWsgQMpIkznRU
QeT2/PklGMU1ckhHBD2sZIPnRJZ4A2wCXyoMCVc0KMlZYehmF5W5XmKLv4CYDDke
lBWUR7OYE0XfoWAmOEDFecN8PATA3GGzlMF0xxBhhPWJ6qDEeWwT+X7m9YMsIzw5
XMVzfATyGrKN0RZ3rx6s9IqL52KyV3Vldy/NS9EjZMXF0NJp/cFwzu6Hmym5H8z0
WIy1SN0u+uSpNybvwQkmKPHL1BHWm+iFh+rBizarZ04gM7mp1GgbCnurwM0dSUrw
NC2ocWcL4UK/Demdrl649lNGbm2Xgafnjwg3NxVZCFObcYsY0QpQGhspXAItmYFT
MgK/My7wPAVDSoc5eyYaMKKiHQcCbC2V2kM8St9IHEm1gtsqPuX+Aj8roDhSBW0i
u1EY6zeKy0dFWD7LIVr0
=jrZC
-----END PGP SIGNATURE-----
Index: rpc_main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/rpcgen/rpc_main.c,v
retrieving revision 1.42
diff -u -r1.42 rpc_main.c
--- rpc_main.c	9 May 2015 23:12:57 -0000	1.42
+++ rpc_main.c	20 Sep 2015 15:10:16 -0000
@@ -495,21 +495,56 @@
 generate_guard(const char *pathname)
 {
 	const char *filename;
-	char *guard, *tmp, *tmp2;
+	char *guard, *tmp, *tmp2, *extdot;
 
 	filename = strrchr(pathname, '/');	/* find last component */
 	filename = ((filename == 0) ? pathname : filename + 1);
 	guard = strdup(filename);
-	/* convert to upper case */
-	tmp = guard;
-	while (*tmp) {
-		*tmp = toupper((unsigned char)*tmp);
-		tmp++;
+	if (guard == NULL) {
+		err(EXIT_FAILURE, "strdup");
 	}
+	extdot = strrchr(guard, '.');
 
+	/*
+	 * Convert to valid C symbol name and make it upper case.
+	 * Map non alphanumerical characters to '_'.
+	 *
+	 * Leave extension as it is. It will be handled in extendfile().
+	 */
+	for (tmp = guard; *tmp; tmp++) {
+		if (islower((unsigned char)*tmp))
+			*tmp = toupper((unsigned char)*tmp);
+		else if (isupper((unsigned char)*tmp))
+			continue;
+		else if (isdigit((unsigned char)*tmp))
+			continue;
+		else if (*tmp == '_')
+			continue;
+		else if (tmp == extdot)
+			break;
+		else
+			*tmp = '_';
+	}
+
+	/*
+	 * Can't have a '_' or '.' at the front of a symbol name, beacuse it
+	 * will end up as "__".
+	 *
+	 * Prefix it with "RPCGEN_".
+	 */
+	if (guard[0] == '_' || guard[0] == '.') {
+		if (asprintf(&tmp2, "RPCGEN_%s", guard) == -1) {
+			err(EXIT_FAILURE, "asprintf");
+		}
+		free(guard);
+		guard = tmp2;
+	}
+
+	/* Replace the file extension */
 	tmp2 = extendfile(guard, "_H_RPCGEN");
 	free(guard);
 	guard = tmp2;
+
 	return (guard);
 }
 


Home | Main Index | Thread Index | Old Index