Commit Graph

13 Commits

Author SHA1 Message Date
Peter Jones d5a32255de misc: Make grub_strtol() "end" pointers have safer const qualifiers
Currently the string functions grub_strtol(), grub_strtoul(), and
grub_strtoull() don't declare the "end" pointer in such a way as to
require the pointer itself or the character array to be immutable to the
implementation, nor does the C standard do so in its similar functions,
though it does require us not to change any of it.

The typical declarations of these functions follow this pattern:

long
strtol(const char * restrict nptr, char ** restrict endptr, int base);

Much of the reason for this is historic, and a discussion of that
follows below, after the explanation of this change.  (GRUB currently
does not include the "restrict" qualifiers, and we name the arguments a
bit differently.)

The implementation is semantically required to treat the character array
as immutable, but such accidental modifications aren't stopped by the
compiler, and the semantics for both the callers and the implementation
of these functions are sometimes also helped by adding that requirement.

This patch changes these declarations to follow this pattern instead:

long
strtol(const char * restrict nptr,
       const char ** const restrict endptr,
       int base);

This means that if any modification to these functions accidentally
introduces either an errant modification to the underlying character
array, or an accidental assignment to endptr rather than *endptr, the
compiler should generate an error.  (The two uses of "restrict" in this
case basically mean strtol() isn't allowed to modify the character array
by going through *endptr, and endptr isn't allowed to point inside the
array.)

It also means the typical use case changes to:

  char *s = ...;
  const char *end;
  long l;

  l = strtol(s, &end, 10);

Or even:

  const char *p = str;
  while (p && *p) {
	  long l = strtol(p, &p, 10);
	  ...
  }

This fixes 26 places where we discard our attempts at treating the data
safely by doing:

  const char *p = str;
  long l;

  l = strtol(p, (char **)&ptr, 10);

It also adds 5 places where we do:

  char *p = str;
  while (p && *p) {
	  long l = strtol(p, (const char ** const)&p, 10);
	  ...
	  /* more calls that need p not to be pointer-to-const */
  }

While moderately distasteful, this is a better problem to have.

With one minor exception, I have tested that all of this compiles
without relevant warnings or errors, and that /much/ of it behaves
correctly, with gcc 9 using 'gcc -W -Wall -Wextra'.  The one exception
is the changes in grub-core/osdep/aros/hostdisk.c , which I have no idea
how to build.

Because the C standard defined type-qualifiers in a way that can be
confusing, in the past there's been a slow but fairly regular stream of
churn within our patches, which add and remove the const qualifier in many
of the users of these functions.  This change should help avoid that in
the future, and in order to help ensure this, I've added an explanation
in misc.h so that when someone does get a compiler warning about a type
error, they have the fix at hand.

The reason we don't have "const" in these calls in the standard is
purely anachronistic: C78 (de facto) did not have type qualifiers in the
syntax, and the "const" type qualifier was added for C89 (I think; it
may have been later).  strtol() appears to date from 4.3BSD in 1986,
which means it could not be added to those functions in the standard
without breaking compatibility, which is usually avoided.

The syntax chosen for type qualifiers is what has led to the churn
regarding usage of const, and is especially confusing on string
functions due to the lack of a string type.  Quoting from C99, the
syntax is:

 declarator:
  pointer[opt] direct-declarator
 direct-declarator:
  identifier
  ( declarator )
  direct-declarator [ type-qualifier-list[opt] assignment-expression[opt] ]
  ...
  direct-declarator [ type-qualifier-list[opt] * ]
  ...
 pointer:
  * type-qualifier-list[opt]
  * type-qualifier-list[opt] pointer
 type-qualifier-list:
  type-qualifier
  type-qualifier-list type-qualifier
 ...
 type-qualifier:
  const
  restrict
  volatile

So the examples go like:

const char foo;			// immutable object
const char *foo;		// mutable pointer to object
char * const foo;		// immutable pointer to mutable object
const char * const foo;		// immutable pointer to immutable object
const char const * const foo; 	// XXX extra const keyword in the middle
const char * const * const foo; // immutable pointer to immutable
				//   pointer to immutable object
const char ** const foo;	// immutable pointer to mutable pointer
				//   to immutable object

Making const left-associative for * and right-associative for everything
else may not have been the best choice ever, but here we are, and the
inevitable result is people using trying to use const (as they should!),
putting it at the wrong place, fighting with the compiler for a bit, and
then either removing it or typecasting something in a bad way.  I won't
go into describing restrict, but its syntax has exactly the same issue
as with const.

Anyway, the last example above actually represents the *behavior* that's
required of strtol()-like functions, so that's our choice for the "end"
pointer.

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-02-28 12:41:29 +01:00
Vladimir 'phcoder' Serbinenko 17334a6b6a Rewrite spkmodem to use PIT for timing. Double the speed. 2013-01-17 20:06:52 +01:00
Vladimir 'phcoder' Serbinenko 7d462559da Add new command pcidump. 2013-01-16 20:44:11 +01:00
Colin Watson ca3a74469a Remove nested functions from PCI iterators.
* grub-core/bus/pci.c (grub_pci_iterate): Add hook_data argument,
passed to hook.  Update all callers to pass appropriate hook data.
* grub-core/bus/emu/pci.c (grub_pci_iterate): Likewise.
* include/grub/pci.h (grub_pci_iteratefunc_t): Add data argument.
Remove NESTED_FUNC_ATTR from here and from all users.
(grub_pci_iterate): Update prototype.
* grub-core/bus/cs5536.c (grub_cs5536_find: hook): Make static
instead of nested.  Rename to ...
(grub_cs5536_find_iter): ... this.
* grub-core/kern/efi/mm.c (stop_broadcom: find_card): Likewise.
* grub-core/kern/mips/loongson/init.c (init_pci: set_card):
Likewise.
* grub-core/kern/vga_init.c (grub_qemu_init_cirrus: find_card):
Likewise.
* grub-core/video/bochs.c (grub_video_bochs_setup: find_card):
Likewise.
* grub-core/video/cirrus.c (grub_video_cirrus_setup: find_card):
Likewise.
* grub-core/video/efi_uga.c (find_framebuf: find_card): Likewise.
* grub-core/video/radeon_fuloong2e.c
(grub_video_radeon_fuloong2e_setup: find_card): Likewise.
* grub-core/video/sis315pro.c (grub_video_sis315pro_setup:
find_card): Likewise.
* grub-core/video/sm712.c (grub_video_sm712_setup: find_card):
Likewise.
2013-01-13 01:10:41 +00:00
Vladimir 'phcoder' Serbinenko 9c4b5c13e6 Improve gettext support. Stylistic fixes and error handling fixes while
on it.
2012-02-08 19:26:01 +01:00
Vladimir 'phcoder' Serbinenko 4a9f8346c9 * grub-core/commands/ls.c: Gettextize.
* grub-core/commands/setpci.c: Likewise.
        * grub-core/commands/videotest.c: Likewise.
        * grub-core/disk/geli.c: Likewise.
        * grub-core/kern/mm.c: Likewise.
        * grub-core/lib/relocator.c: Likewise.
        * grub-core/loader/efi/appleloader.c: Likewise.
        * grub-core/loader/i386/xnu.c: Likewise.
        * grub-core/loader/ia64/efi/linux.c: Likewise.
        * grub-core/loader/xnu.c: Likewise.
        * grub-core/net/dns.c: Likewise.
        * grub-core/net/net.c: Likewise.
        * grub-core/script/lexer.c: Likewise.
        * grub-core/script/parser.y: Likewise.
        * grub-core/script/yylex.l: Likewise.
        * util/getroot.c: Likewise.
        * util/grub-setup.c: Likewise.
2012-02-03 11:56:49 +01:00
Vladimir 'phcoder' Serbinenko 10f0117bf9 * grub-core/commands/efi/fixvideo.c: Gettextize.
* grub-core/commands/hashsum.c: Likewise.
	* grub-core/commands/i386/cmostest.c: Likewise.
	* grub-core/commands/i386/pc/drivemap.c: Likewise.
	* grub-core/commands/i386/pc/lsapm.c: Likewise.
	* grub-core/commands/i386/pc/sendkey.c: Likewise.
	* grub-core/commands/lsmmap.c: Likewise.
	* grub-core/commands/menuentry.c: Likewise.
	* grub-core/commands/mips/loongson/lsspd.c: Likewise.
	* grub-core/commands/setpci.c: Likewise.
	* grub-core/loader/i386/bsd.c: Likewise.
	* grub-core/loader/i386/linux.c: Likewise.
	* util/getroot.c: Likewise.
	* util/grub-editenv.c: Likewise.
	* util/grub-fstest.c: Likewise.
	* util/grub-mkfont.c: Likewise.
	* util/grub-mkimage.c: Likewise.
	* util/grub-mkpasswd-pbkdf2.c: Likewise.
	* util/grub-pe2elf.c: Likewise.
	* util/grub-probe.c: Likewise.
	* util/grub-setup.c: Likewise.
	* util/ieee1275/ofpath.c: Likewise.
	* util/misc.c: Likewise.
	* util/raid.c: Likewise.
2011-11-12 00:34:14 +01:00
Vladimir 'phcoder' Serbinenko e745cf0ca6 Implement automatic module license checking according to new GNU
guidelines.

	* grub-core/kern/dl.c (grub_dl_check_license): New function.
	(grub_dl_load_core): Use grub_dl_check_license.
	* include/grub/dl.h (GRUB_MOD_SECTION): New macro.
	(GRUB_MOD_LICENSE): Likewise.
	(GRUB_MOD_DUAL_LICENSE): Likewise.
	All modules updated.
2011-04-11 23:01:51 +02:00
Vladimir 'phcoder' Serbinenko 7d4e39d65a * grub-core/bus/usb/usb.c (attach_hooks): Make static.
* grub-core/bus/usb/usbhub.c (hubs): Likewise.
	* grub-core/commands/hashsum.c (aliases): Likewise.
	* grub-core/commands/setpci.c (pci_registers): Likewise.
	* grub-core/disk/usbms.c (attach_hook): Likewise.
	* grub-core/fs/zfs/zfs.c (decomp_table): Likewise.
	(zio_checksum_table): Likewise.
	* grub-core/gettext/gettext.c (grub_gettext_msg_list): Likewise.
	* grub-core/gfxmenu/gfxmenu.c (cached_view): Likewise.
	* grub-core/lib/legacy_parse.c (legacy_commands): Likewise.
	* grub-core/lib/relocator.c (leftovers): Likewise.
	(extra_blocks): Likewise.
	* grub-core/loader/i386/bsd.c (relocator): Likewise.
	* grub-core/loader/i386/multiboot_mbi.c (modules): Likewise.
	(modules_last): Likewise.
	* grub-core/loader/i386/xnu.c (table_aliases): Likewise.
	(devices): Likewise.
	* grub-core/loader/multiboot_mbi2.c (modules): Likewise.
	(modules_last): Likewise.
	* grub-core/normal/auth.c (users): Likewise.
	* grub-core/normal/context.c (initial_menu): Likewise.
	(current_menu): Likewise.
	* grub-core/normal/crypto.c (crypto_specs): Likewise.
	* grub-core/term/serial.c (grub_serial_ports): Likewise.
	(grub_serial_terminfo_input_template): Likewise.
	(grub_serial_terminfo_output_template): Likewise.
	(grub_serial_terminfo_input): Likewise.
	(grub_serial_terminfo_output): Likewise.
	(registered): Likewise.
	* grub-core/term/usb_keyboard.c (attach_hook): Likewise.
2011-03-23 12:05:13 +01:00
Vladimir 'phcoder' Serbinenko ed80f7d586 * include/grub/command.h (GRUB_COMMAND_FLAG_CMDLINE): Removed. All
users updated.
	(GRUB_COMMAND_FLAG_MENU): Likewise.
	(GRUB_COMMAND_FLAG_BOTH): Likewise.
	(GRUB_COMMAND_FLAG_TITLE): Removed.
	(GRUB_COMMAND_FLAG_NO_ECHO): Likewise.
	(GRUB_COMMAND_FLAG_EXTCMD): Moved into enum.
	(GRUB_COMMAND_FLAG_DYNCMD): Likewise.
	(GRUB_COMMAND_FLAG_BLOCKS): Likewise.
	(grub_command_flags_t): New enum. All users updated.
2010-09-14 23:06:01 +02:00
BVK Chaitanya 928bad4708 merge with mainline 2010-08-26 09:30:11 +05:30
BVK Chaitanya a762e3c335 merge with mainline 2010-06-01 17:52:31 +05:30
BVK Chaitanya 8c41176882 automake commit without merge history 2010-05-06 11:34:04 +05:30