glyphs: fix types

The code was written on top of buffers of `char[26]`, and then was
changed to use `char *`, yet was still using `sizeof buf`, which
became `sizeof (char *)` instead of `sizeof (char[26])`.

Reported by Dagobert Michelsen.
https://lists.gnu.org/r/bug-bison/2020-07/msg00023.html

* src/glyphs.h, src/glyphs.c: Get rid of uses of `char *`, use only
glyph_buffer_t.
This commit is contained in:
Akim Demaille
2020-07-19 16:05:15 +02:00
parent b28d67b6b0
commit 744da03955
3 changed files with 28 additions and 30 deletions

4
NEWS
View File

@@ -2,6 +2,10 @@ GNU Bison NEWS
* Noteworthy changes in release ?.? (????-??-??) [?] * Noteworthy changes in release ?.? (????-??-??) [?]
** Bug fixes
Portability issues.
* Noteworthy changes in release 3.6.92 (2020-07-19) [beta] * Noteworthy changes in release 3.6.92 (2020-07-19) [beta]

View File

@@ -28,24 +28,17 @@
#include <mbswidth.h> #include <mbswidth.h>
#include <unicodeio.h> #include <unicodeio.h>
// In gnulib/lib/unicodeio.h unicode_to_mb uses a buffer of 25 bytes.
typedef char glyph_buffer_t[26];
glyph_buffer_t arrow;
static glyph_buffer_t arrow_buf;
const char *arrow;
int arrow_width; int arrow_width;
static glyph_buffer_t down_arrow_buf; glyph_buffer_t down_arrow;
const char *down_arrow;
int down_arrow_width; int down_arrow_width;
static glyph_buffer_t dot_buf; glyph_buffer_t dot;
const char *dot;
int dot_width; int dot_width;
static glyph_buffer_t empty_buf; glyph_buffer_t empty;
const char *empty;
int empty_width; int empty_width;
const char *derivation_separator = " "; const char *derivation_separator = " ";
@@ -53,8 +46,7 @@ int derivation_separator_width = 1;
typedef struct typedef struct
{ {
const char **glyph; glyph_buffer_t *pbuf;
char *buf;
const char *fallback; const char *fallback;
} callback_arg_t; } callback_arg_t;
@@ -63,8 +55,8 @@ static long
on_success (const char *buf, size_t buflen, void *callback_arg) on_success (const char *buf, size_t buflen, void *callback_arg)
{ {
callback_arg_t *arg = (callback_arg_t *) callback_arg; callback_arg_t *arg = (callback_arg_t *) callback_arg;
assert (buflen + 1 < sizeof arg->buf); assert (buflen + 1 < sizeof *arg->pbuf);
*stpncpy (arg->buf, buf, buflen) = '\0'; *stpncpy (*arg->pbuf, buf, buflen) = '\0';
return 1; return 1;
} }
@@ -73,19 +65,17 @@ on_failure (unsigned code MAYBE_UNUSED, const char *msg MAYBE_UNUSED,
void *callback_arg) void *callback_arg)
{ {
callback_arg_t *arg = (callback_arg_t *) callback_arg; callback_arg_t *arg = (callback_arg_t *) callback_arg;
assert (strlen (arg->fallback) + 1 < sizeof arg->buf); assert (strlen (arg->fallback) + 1 < sizeof *arg->pbuf);
strcpy (arg->buf, arg->fallback); strcpy (*arg->pbuf, arg->fallback);
return 0; return 0;
} }
static bool static bool
glyph_set (const char **glyph, glyph_set (glyph_buffer_t *glyph, int *width,
char buf[26], int *width,
unsigned code, const char *fallback) unsigned code, const char *fallback)
{ {
callback_arg_t arg = { glyph, buf, fallback }; callback_arg_t arg = { glyph, fallback };
int res = unicode_to_mb (code, on_success, on_failure, &arg); int res = unicode_to_mb (code, on_success, on_failure, &arg);
*glyph = buf;
*width = mbswidth (*glyph, 0); *width = mbswidth (*glyph, 0);
return res; return res;
} }
@@ -93,11 +83,11 @@ glyph_set (const char **glyph,
void void
glyphs_init (void) glyphs_init (void)
{ {
glyph_set (&arrow, arrow_buf, &arrow_width, 0x2192, "->"); glyph_set (&arrow, &arrow_width, 0x2192, "->");
glyph_set (&dot, dot_buf, &dot_width, 0x2022, "."); glyph_set (&dot, &dot_width, 0x2022, ".");
glyph_set (&down_arrow, down_arrow_buf, &down_arrow_width, 0x21b3, "`->"); glyph_set (&down_arrow, &down_arrow_width, 0x21b3, "`->");
glyph_set (&empty, empty_buf, &empty_width, 0x03b5, "%empty"); glyph_set (&empty, &empty_width, 0x03b5, "%empty");
strncat (down_arrow_buf, " ", sizeof down_arrow_buf - strlen (down_arrow_buf) - 1); strncat (down_arrow, " ", sizeof down_arrow - strlen (down_arrow) - 1);
down_arrow_width += 1; down_arrow_width += 1;
} }

View File

@@ -23,20 +23,24 @@
/* Initialize the following variables. */ /* Initialize the following variables. */
void glyphs_init (void); void glyphs_init (void);
/* In gnulib/lib/unicodeio.h unicode_to_mb uses a buffer of 25 bytes.
In down_arrow, we append one space. */
typedef char glyph_buffer_t[26];
/* "→", separates the lhs of a rule from its rhs. */ /* "→", separates the lhs of a rule from its rhs. */
extern const char *arrow; extern glyph_buffer_t arrow;
extern int arrow_width; extern int arrow_width;
/* "•", a point in an item (aka, a dotted rule). */ /* "•", a point in an item (aka, a dotted rule). */
extern const char *dot; extern glyph_buffer_t dot;
extern int dot_width; extern int dot_width;
/* "↳ ", below an lhs to announce the rhs. */ /* "↳ ", below an lhs to announce the rhs. */
extern const char *down_arrow; extern glyph_buffer_t down_arrow;
extern int down_arrow_width; extern int down_arrow_width;
/* "ε", an empty rhs. */ /* "ε", an empty rhs. */
extern const char *empty; extern glyph_buffer_t empty;
extern int empty_width; extern int empty_width;
/* " ", separate symbols in the rhs of a derivation. */ /* " ", separate symbols in the rhs of a derivation. */