[PATCH] switch from sprintf to asprintf and snprintf
sprintf is potentially dangerous especially with gettext, when messages may be larger than coder would expect. I attach the patch to fix it -- Regards Vladimir 'φ-coder/phcoder' Serbinenko === modified file 'commands/ls.c' --- commands/ls.c 2009-12-25 23:50:59 + +++ commands/ls.c 2009-12-29 09:04:06 + @@ -86,7 +86,7 @@ int print_files_long (const char *filename, const struct grub_dirhook_info *info) { - char pathname[grub_strlen (dirname) + grub_strlen (filename) + 1]; + char *pathname; if ((! all) && (filename[0] == '.')) return 0; @@ -96,9 +96,12 @@ grub_file_t file; if (dirname[grub_strlen (dirname) - 1] == '/') - grub_sprintf (pathname, "%s%s", dirname, filename); + pathname = grub_asprintf ("%s%s", dirname, filename); else - grub_sprintf (pathname, "%s/%s", dirname, filename); + pathname = grub_asprintf ("%s/%s", dirname, filename); + + if (!pathname) + return 1; /* XXX: For ext2fs symlinks are detected as files while they should be reported as directories. */ @@ -130,8 +133,9 @@ grub_uint32_t whole, fraction; whole = grub_divmod64 (fsize, 100, &fraction); - grub_sprintf (buf, "%u.%02u%c", whole, fraction, -grub_human_sizes[units]); + grub_snprintf (buf, sizeof (buf), + "%u.%02u%c", whole, fraction, + grub_human_sizes[units]); grub_printf ("%-12s", buf); } else === modified file 'commands/memrw.c' --- commands/memrw.c 2009-12-25 23:50:59 + +++ commands/memrw.c 2009-12-29 09:04:06 + @@ -61,7 +61,7 @@ if (cmd->state[0].set) { - grub_sprintf (buf, "%x", value); + grub_snprintf (buf, sizeof (buf), "%x", value); grub_env_set (cmd->state[0].arg, buf); } else === modified file 'commands/parttool.c' --- commands/parttool.c 2009-12-25 23:50:59 + +++ commands/parttool.c 2009-12-29 09:04:06 + @@ -182,12 +182,11 @@ { char *filename; - filename = grub_malloc (grub_strlen (prefix) + sizeof ("/parttool.lst")); + filename = grub_asprintf ("%s/parttool.lst", prefix); if (filename) { grub_file_t file; - grub_sprintf (filename, "%s/parttool.lst", prefix); file = grub_file_open (filename); if (file) { === modified file 'commands/search.c' --- commands/search.c 2009-12-25 23:50:59 + +++ commands/search.c 2009-12-29 09:04:06 + @@ -33,7 +33,6 @@ FUNC_NAME (const char *key, const char *var, int no_floppy) { int count = 0; - char *buf = NULL; grub_fs_autoload_hook_t saved_autoload; auto int iterate_device (const char *name); @@ -48,24 +47,20 @@ #ifdef DO_SEARCH_FILE { - grub_size_t len; - char *p; + char *buf; grub_file_t file; - len = grub_strlen (name) + 2 + grub_strlen (key) + 1; - p = grub_realloc (buf, len); - if (! p) + buf = grub_asprintf ("(%s)%s", name, key); + if (! buf) return 1; - buf = p; - grub_sprintf (buf, "(%s)%s", name, key); - file = grub_file_open (buf); if (file) { found = 1; grub_file_close (file); } + grub_free (buf); } #else { @@ -135,8 +130,6 @@ else grub_device_iterate (iterate_device); - grub_free (buf); - if (grub_errno == GRUB_ERR_NONE && count == 0) grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key); } === modified file 'commands/xnu_uuid.c' --- commands/xnu_uuid.c 2009-12-25 23:50:59 + +++ commands/xnu_uuid.c 2009-12-29 09:04:06 + @@ -349,18 +349,18 @@ grub_memcpy (hashme.prefix, hash_prefix, sizeof (hashme.prefix)); md5 ((char *) &hashme, sizeof (hashme), (char *) xnu_uuid); - grub_sprintf (uuid_string, - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - (unsigned int) xnu_uuid[0], (unsigned int) xnu_uuid[1], - (unsigned int) xnu_uuid[2], (unsigned int) xnu_uuid[3], - (unsigned int) xnu_uuid[4], (unsigned int) xnu_uuid[5], - (unsigned int) ((xnu_uuid[6] & 0xf) | 0x30), - (unsigned int) xnu_uuid[7], - (unsigned int) ((xnu_uuid[8] & 0x3f) | 0x80), - (unsigned int) xnu_uuid[9], - (unsigned int) xnu_uuid[10], (unsigned int) xnu_uuid[11], - (unsigned int) xnu_uuid[12], (unsigned int) xnu_uuid[13], - (unsigned int) xnu_uuid[14], (unsigned int) xnu_uuid[15]); + grub_snprintf (uuid_string, sizeof (uuid_string), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + (unsigned int) xnu_uuid[0], (unsigned int) xnu_uuid[1], + (unsigned int) xnu_uuid[2], (unsigned int) xnu_uuid[3], + (unsigned int) xnu_uuid[4], (unsigned int) xnu_uuid[5], + (unsigned int) ((xnu_uuid[6] & 0xf) | 0x30), + (unsigned int) xnu_uuid[7], + (unsigned int) ((xnu_uuid[8] & 0x3f) | 0x80), + (unsigned int) xnu_uuid[9], + (unsigned int) xnu_uuid[10], (unsigned int) xnu_uuid[11], + (unsigned int) xnu_uuid[12], (unsigned int) xnu_uuid[13], + (unsigned int) xnu_uuid[14], (unsigned int) xnu_uuid[15]); for (ptr = uuid_string; *ptr; ptr++) *ptr = grub_toupper (*ptr); if (argc == 1) ===
Compilation under MacOSX
Hi all, here the news: the main trunk (mainline) can be compile on Mac OSX (snow leopard) but i need to add a LDFLAGS="-lintl" so utils like grub-mkimage can link. I think the _( gettext macro is not expand on linux when grub-mkimage is compile under linux because they are no reference of the gettext library in the executable. But in MacOSX i have this error: gcc-4.2 -o grub-mkimage grub_mkimage-gnulib_progname.o grub_mkimage-util_i386_pc_grub_mkimage.o grub_mkimage-util_misc.o grub_mkimage-util_resolve.o grub_mkimage-lib_LzmaEnc.o grub_mkimage-lib_LzFind.o Undefined symbols: "_libintl_gettext", referenced from: _compress_kernel in grub_mkimage-util_i386_pc_grub_mkimage.o _compress_kernel in grub_mkimage-util_i386_pc_grub_mkimage.o _generate_image in grub_mkimage-util_i386_pc_grub_mkimage.o _generate_image in grub_mkimage-util_i386_pc_grub_mkimage.o _generate_image in grub_mkimage-util_i386_pc_grub_mkimage.o _generate_image in grub_mkimage-util_i386_pc_grub_mkimage.o _usage in grub_mkimage-util_i386_pc_grub_mkimage.o _usage in grub_mkimage-util_i386_pc_grub_mkimage.o _main in grub_mkimage-util_i386_pc_grub_mkimage.o "_libintl_textdomain", referenced from: _main in grub_mkimage-util_i386_pc_grub_mkimage.o "_libintl_bindtextdomain", referenced from: _main in grub_mkimage-util_i386_pc_grub_mkimage.o ld: symbol(s) not found adding the LDFLAGS="-lintl" fix the problem For the experimental trunk, i have an undefined symbol: util/grub-mkpasswd-pbkdf2.c:218: warning: implicit declaration of function ‘getline’ gcc-4.2 -o grub-mkpasswd-pbkdf2 grub_mkpasswd_pbkdf2-gnulib_progname.o grub_mkpasswd_pbkdf2-util_grub_mkpasswd_pbkdf2.o grub_mkpasswd_pbkdf2-lib_crypto.o grub_mkpasswd_pbkdf2-lib_libgcrypt_grub_cipher_sha512.o grub_mkpasswd_pbkdf2-lib_pbkdf2.o grub_mkpasswd_pbkdf2-util_misc.o grub_mkpasswd_pbkdf2-kern_err.o -L/opt/local/lib -lintl Undefined symbols: "_getline", referenced from: _main in grub_mkpasswd_pbkdf2-util_grub_mkpasswd_pbkdf2.o _main in grub_mkpasswd_pbkdf2-util_grub_mkpasswd_pbkdf2.o ld: symbol(s) not found collect2: ld returned 1 exit status make: *** [grub-mkpasswd-pbkdf2] Error 1 seems that the getline function is not defined anywhere. Best regards, Yves Blusseau ___ Grub-devel mailing list Grub-devel@gnu.org http://lists.gnu.org/mailman/listinfo/grub-devel
Re: grub.info
Bruce O. Benson wrote: If I: - ...merged the contents of both existing documents into a new one... - ...that adhered to the outline you enumerated... - ...assigned copyright to FSF... - ...rendered in texi, html, and pdf. ...Would that be useful for someone to check in to trunk/docs? I don't wanna get deep into a piece of work like that if it's not wanted. Well I volunteered to do the texi. That's not my favorite format, but it's the Gnu standard. If I was choosing formats, I'd do it in XML and generate text, html, and pdf from the same source. Currently I've been studying code to try to get the appropriate background for the texi, but I do have a preliminary text file started, but it really is only an outline right now. I don't have a schedule established. but was thinking I'd have sometihng for review in the next month or two. -- Bruce ___ Grub-devel mailing list Grub-devel@gnu.org http://lists.gnu.org/mailman/listinfo/grub-devel
Re: Compilation under MacOSX
Hi Yves, the main trunk (mainline) can be compile on Mac OSX (snow leopard) but i need to add a LDFLAGS="-lintl" so utils like grub-mkimage can link. I experienced a similar problem when compiling trunk on NetBSD: http://savannah.gnu.org/bugs/?28356 Adding LDFLAGS="-lintl" also solved the problem, but a better (?) option might be to use the variable LIBINTL that is set by the macro AM_GNU_GETTEXT (see the above bug report for some details and for a possible patch). Best, Grégoire ___ Grub-devel mailing list Grub-devel@gnu.org http://lists.gnu.org/mailman/listinfo/grub-devel
Re: grub.info
On Tue, Dec 29, 2009 at 10:37 AM, Bruce Dubbs wrote: > Bruce O. Benson wrote: > > If I: >> - ...merged the contents of both existing documents into a new one... >> - ...that adhered to the outline you enumerated... >> - ...assigned copyright to FSF... >> - ...rendered in texi, html, and pdf. >> >> ...Would that be useful for someone to check in to trunk/docs? I don't >> wanna get deep into a piece of work like that if it's not wanted. >> > > Well I volunteered to do the texi. That's not my favorite format, but it's > the Gnu standard. If I was choosing formats, I'd do it in XML and generate > text, html, and pdf from the same source. > > Currently I've been studying code to try to get the appropriate background > for the texi, but I do have a preliminary text file started, but it really > is only an outline right now. I don't have a schedule established. but was > thinking I'd have sometihng for review in the next month or two. > > Someone on the list can chime in if they want and are able to check my work in to svn (in whole or part). Then I can release copyright. ___ Grub-devel mailing list Grub-devel@gnu.org http://lists.gnu.org/mailman/listinfo/grub-devel