Commit Graph

27 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 16940e077c * grub-core/commands/videoinfo.c: Use "paletted" rather than "packed
pixel".
2013-05-07 14:44:05 +02:00
Vladimir 'phcoder' Serbinenko 3056d3e752 Remove nested functions from videoinfo iterators. 2013-03-01 11:11:36 +01:00
Vladimir 'phcoder' Serbinenko 6cce6f3864 * grub-core/commands/videoinfo.c (hook): Show pitch. 2012-06-05 12:03:36 +02:00
Vladimir 'phcoder' Serbinenko e8e0566b0c * grub-core/commands/videoinfo.c: Add TRANSLATORS comments.
* grub-core/commands/xnu_uuid.c: Likewise.
	* grub-core/loader/efi/appleloader.c: Likewise.
	* grub-core/script/execute.c: Likewise.
	* grub-core/script/main.c: Likewise.
	* util/grub-mkfont.c: Likewise.
2012-03-10 13:19:46 +01:00
Vladimir 'phcoder' Serbinenko 6e69da9ccb * grub-core/commands/videoinfo.c: Add TRANSLATORS comments.
* grub-core/commands/videotest.c: Likewise.
	* grub-core/loader/i386/linux.c: Likewise.
2012-03-06 15:04:33 +01:00
Vladimir 'phcoder' Serbinenko 805a8dccc8 * grub-core/commands/gptsync.c: Fix typographic quoting.
* grub-core/commands/ieee1275/suspend.c: Likewise.
	* grub-core/commands/parttool.c: Likewise.
	* grub-core/commands/search_wrap.c: Likewise.
	* grub-core/commands/videoinfo.c: Likewise.
	* grub-core/gfxmenu/gui_label.c: Likewise.
	* grub-core/hello/hello.c: Likewise.
	* grub-core/kern/emu/main.c: Likewise.
	* grub-core/net/net.c: Likewise.
	* grub-core/normal/menu.c: Likewise.
	* grub-core/normal/menu_text.c: Likewise.
	* grub-core/normal/misc.c: Likewise.
	* util/grub-editenv.c: Likewise.
	* util/grub-install.in: Likewise.
	* util/grub-kbdcomp.in: Likewise.
	* util/grub-mkconfig.in: Likewise.
	* util/grub-mknetdir.in: Likewise.
	* util/grub-mkrescue.in: Likewise.
	* util/grub-mkstandalone.in: Likewise.
	* util/grub-reboot.in: Likewise.
	* util/grub-set-default.in: Likewise.
	* util/grub-setup.c: Likewise.
	* util/powerpc/ieee1275/grub-mkrescue.in: Likewise.
2012-03-03 13:05:08 +01:00
Vladimir 'phcoder' Serbinenko 40211ab884 * grub-core/commands/acpihalt.c: Add TRANSLATORS comments.
* grub-core/commands/keystatus.c: Likewise.
	* grub-core/commands/loadenv.c: Likewise.
	* grub-core/commands/probe.c: Likewise.
	* grub-core/commands/regexp.c: Likewise.
	* grub-core/commands/true.c: Likewise.
	* grub-core/commands/videoinfo.c: Likewise.
	* grub-core/disk/cryptodisk.c: Likewise.
	* grub-core/disk/ldm.c: Likewise.
	* grub-core/disk/loopback.c: Likewise.
	* grub-core/disk/luks.c: Likewise.
	* grub-core/fs/zfs/zfsinfo.c: Likewise.
	* grub-core/kern/disk.c: Likewise.
	* grub-core/kern/emu/hostdisk.c: Likewise.
2012-03-03 12:59:28 +01:00
Vladimir 'phcoder' Serbinenko 49ce9e50eb * grub-core/commands/videoinfo.c (hook): Replace "Direct"
with "Direct color" and "Packed" with "Packed pixel".
	(grub_cmd_videoinfo): Simplify legend.
2012-03-02 15:05:30 +01:00
Vladimir 'phcoder' Serbinenko 67093bc0ed Another round of string clarification and adding TRANSLATORS comments. 2012-02-26 17:28:05 +01:00
Vladimir 'phcoder' Serbinenko d61386e21d Improve string. Gettextize. 2012-02-12 15:25:25 +01: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 6e0632e28c * grub-core/commands/acpihalt.c: Gettextized.
* grub-core/commands/cacheinfo.c: Likewise.
	* grub-core/commands/cmp.c: Likewise.
	* grub-core/commands/efi/loadbios.c: Likewise.
	* grub-core/commands/gptsync.c: Likewise.
	* grub-core/commands/ieee1275/suspend.c: Likewise.
	* grub-core/commands/legacycfg.c: Likewise.
	* grub-core/commands/memrw.c: Likewise.
	* grub-core/commands/minicmd.c: Likewise.
	* grub-core/commands/parttool.c: Likewise.
	* grub-core/commands/time.c: Likewise.
	* grub-core/commands/videoinfo.c: Likewise.
	* grub-core/disk/geli.c: Likewise.
	* grub-core/disk/i386/pc/biosdisk.c: Likewise.
	* grub-core/disk/luks.c: Likewise.
	* grub-core/disk/lvm.c: Likewise.
	* grub-core/font/font_cmd.c: Likewise.
	* grub-core/fs/zfs/zfscrypt.c: Likewise.
	* grub-core/fs/zfs/zfsinfo.c: Likewise.
	* grub-core/gfxmenu/view.c: Likewise.
	* grub-core/kern/emu/hostdisk.c: Likewise.
	* grub-core/kern/emu/main.c: Likewise.
	* grub-core/kern/emu/misc.c: Likewise.
	* grub-core/kern/emu/mm.c: Likewise.
	* grub-core/kern/mips/arc/init.c: Likewise.
	* grub-core/kern/mips/loongson/init.c: Likewise.
	* grub-core/kern/partition.c: Likewise.
	* grub-core/lib/i386/halt.c: Likewise.
	* grub-core/lib/mips/arc/reboot.c: Likewise.
	* grub-core/lib/mips/loongson/reboot.c: Likewise.
	* grub-core/loader/i386/pc/chainloader.c: Likewise.
	* grub-core/loader/i386/xnu.c: Likewise.
	* grub-core/loader/multiboot.c: Likewise.
	* grub-core/net/bootp.c: Likewise.
	* grub-core/net/net.c: Likewise.
	* grub-core/normal/term.c: Likewise.
	* grub-core/partmap/bsdlabel.c: Likewise.
	* grub-core/parttool/msdospart.c: Likewise.
	* grub-core/term/gfxterm.c: Likewise.
	* grub-core/term/terminfo.c: Likewise.
	* grub-core/video/i386/pc/vbe.c: Likewise.
	* util/grub-menulst2cfg.c: Likewise.
	* util/grub-mkdevicemap.c: Likewise.
	* util/grub-mklayout.c: Likewise.
	* util/grub-mkrelpath.c: Likewise.
	* util/grub-script-check.c: Likewise.
	* util/ieee1275/grub-ofpathname.c: Likewise.
	* util/resolve.c: Likewise.
2011-11-11 21:44:56 +01:00
Colin Watson 8fc4fa45c5 Preferred resolution detection for VBE.
* grub-core/video/video.c (grub_video_edid_checksum): New function.
(grub_video_edid_preferred_mode): Likewise.  Try EDID followed by
the Flat Panel extension, in line with the X.org VESA driver.
* grub-core/video/i386/pc/vbe.c (grub_vbe_bios_get_flat_panel_info):
New function.
(grub_vbe_bios_get_ddc_capabilities): Likewise.
(grub_vbe_bios_read_edid): Likewise.
(grub_vbe_get_preferred_mode): Likewise.
(grub_video_vbe_setup): When the mode is "auto", try to get the
preferred mode from VBE, and use the largest mode that is no larger
than the preferred mode (some BIOSes expose a preferred mode that is
not in their mode list!).  If this fails, fall back to 640x480 as a
safe conservative choice.
(grub_video_vbe_get_edid): New function.
(grub_video_vbe_adapter): Add get_edid.
* include/grub/video.h (struct grub_vbe_edid_info): New structure.
(struct grub_video_adapter): Add get_edid.
(grub_video_edid_checksum): Add prototype.
(grub_video_edid_preferred_mode): Likewise.
* include/grub/i386/pc/vbe.h (struct grub_vbe_flat_panel_info): New
structure.

* grub-core/commands/videoinfo.c (print_edid): New function.
(grub_cmd_videoinfo): Print EDID if available.

* util/grub.d/00_header.in (GRUB_GFXMODE): Default to "auto".  This
is more appropriate on a wider range of platforms than 640x480.
* docs/grub.texi (Simple configuration): Update GRUB_GFXMODE
documentation.
2011-07-21 19:46:44 +01:00
Colin Watson f9b75e8a67 * grub-core/commands/videoinfo.c (hook): Indicate current video mode
with `*'.
(grub_cmd_videoinfo): Fetch current video mode.
2011-06-27 10:47:02 +01:00
Colin Watson e806ce60ee merge trunk 2011-05-18 16:53:36 +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
Colin Watson 015e21571c check that adapter->get_edid is non-NULL 2010-12-14 19:03:28 +00:00
Colin Watson 129185cfaa move more EDID-handling functions to generic code, and make videoinfo display EDID information 2010-12-14 18:03:34 +00:00
Vladimir 'phcoder' Serbinenko 3f8fcb6a24 Support vbeprobe MODE 2010-09-15 14:37:28 +02:00
Vladimir 'phcoder' Serbinenko 4dd58a6edd Change video_mode_type to an enum, fix collisions and add a bit more info 2010-09-12 02:09:09 +02:00
Vladimir 'phcoder' Serbinenko d2467d2361 Add Hercules, Planar and YUV to videoinfo (not tested) 2010-09-12 01:45:58 +02:00
Vladimir 'phcoder' Serbinenko 03199f1909 Fix order of dimensions in videoinfo 2010-09-03 22:50:11 +02:00
Vladimir 'phcoder' Serbinenko 4ab5f27548 Remove vbetest and vbeinfo in favour of videotest and videoinfo 2010-09-03 22:21:48 +02:00
Vladimir 'phcoder' Serbinenko 380c39cb25 Show adapter-specific info 2010-09-03 22:11:22 +02:00
Vladimir 'phcoder' Serbinenko 4787931fe0 Show mode id 2010-09-03 21:40:40 +02:00
Vladimir 'phcoder' Serbinenko 540e2fe185 Initial videoinfo implementation 2010-09-03 21:19:22 +02:00