git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC PATCH 6/6] utf8.c: avoid char overflow

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jul 9, 2018, 20:04 UTC
Message-ID
<nycvar.QRO.7.76.6.1807092203110.75@tvgsbejvaqbjf.bet>
In-Reply-To
<0ceeb342fec1d0868b81cd64941df53c@drbeat.li>
Hi Beat,
On Mon, 9 Jul 2018, Beat Bolli wrote:
Show 46 quoted lines
> Am 09.07.2018 15:14, schrieb Johannes Schindelin:
> > 
> > On Sun, 8 Jul 2018, Beat Bolli wrote:
> > 
> > > In ISO C, char constants must be in the range -128..127. Change the BOM
> > > constants to unsigned char to avoid overflow.
> > > 
> > > Signed-off-by: Beat Bolli <dev+git@drbeat.li>
> > > ---
> > >  utf8.c | 10 +++++-----
> > >  1 file changed, 5 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/utf8.c b/utf8.c
> > > index d55e20c641..833ce00617 100644
> > > --- a/utf8.c
> > > +++ b/utf8.c
> > > @@ -561,15 +561,15 @@ char *reencode_string_len(const char *in, int insz,
> > >  #endif
> > > 
> > > static int has_bom_prefix(const char *data, size_t len,
> > > -			  const char *bom, size_t bom_len)
> > > +			  const unsigned char *bom, size_t bom_len)
> > >  {
> > > 	return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len);
> > >  }
> > > 
> > > -static const char utf16_be_bom[] = {0xFE, 0xFF};
> > > -static const char utf16_le_bom[] = {0xFF, 0xFE};
> > > -static const char utf32_be_bom[] = {0x00, 0x00, 0xFE, 0xFF};
> > > -static const char utf32_le_bom[] = {0xFF, 0xFE, 0x00, 0x00};
> > > +static const unsigned char utf16_be_bom[] = {0xFE, 0xFF};
> > > +static const unsigned char utf16_le_bom[] = {0xFF, 0xFE};
> > > +static const unsigned char utf32_be_bom[] = {0x00, 0x00, 0xFE, 0xFF};
> > > +static const unsigned char utf32_le_bom[] = {0xFF, 0xFE, 0x00, 0x00};
> > 
> > An alternative approach that might be easier to read (and avoids the
> > confusion arising from our use of (signed) chars for strings pretty much
> > everywhere):
> > 
> > #define FE ((char)0xfe)
> > #define FF ((char)0xff)
> > 
> > ...
> 
> I have tried this first (without the macros, though), and thought it looked
> really ugly.
Yep, I would totally agree that it would be very ugly without the macros.

Which is why I suggested the macros instead, in which case it looks relatively elegant to my eyes.

Ciao, Dscho

Previous: Junio C HamanoNext: Beat Bolli
Message 20 of 39 in “Compile cleanly in pedantic mode”
  1. 0/6 Compile cleanly in pedantic modeBeat Bolli, Jul 8, 2018
  2. 4/6 sequencer.c: avoid empty statements at top levelBeat Bolli, Jul 8, 2018
  3. Eric SunshineJul 8, 2018
  4. Philip OakleyJul 8, 2018
  5. ig@drbeat.liJul 9, 2018
  6. Junio C HamanoJul 9, 2018
  7. Beat BolliJul 9, 2018
  8. 2/6 refs/refs-internal.h: avoid forward declaration of an enumBeat Bolli, Jul 8, 2018
  9. Jeff KingJul 9, 2018
  10. Beat BolliJul 9, 2018
  11. Jeff KingJul 10, 2018
  12. 3/6 convert.c: replace "\e" escapes with "\033".Beat Bolli, Jul 8, 2018
  13. 6/6 utf8.c: avoid char overflowBeat Bolli, Jul 8, 2018
  14. Johannes SchindelinJul 9, 2018
  15. Beat BolliJul 9, 2018
  16. Beat BolliJul 9, 2018
  17. Junio C HamanoJul 9, 2018
  18. Beat BolliJul 9, 2018
  19. Junio C HamanoJul 9, 2018
  20. Johannes SchindelinJul 9, 2018
  21. 1/6 connect.h: avoid forward declaration of an enumBeat Bolli, Jul 8, 2018
  22. 5/6 string-list.c: avoid conversion from void * to function pointerBeat Bolli, Jul 8, 2018
  23. Johannes SchindelinJul 9, 2018
  24. Junio C HamanoJul 9, 2018
  25. 0/6 Compile cleanly in pedantic modeBeat Bolli, Jul 9, 2018
  26. Beat BolliJul 9, 2018
  27. Junio C HamanoJul 9, 2018
  28. Beat BolliJul 9, 2018
  29. Beat BolliJul 10, 2018
  30. Junio C HamanoJul 11, 2018
  31. Johannes SchindelinJul 12, 2018
  32. Junio C HamanoJul 12, 2018
  33. 1/6 connect.h: avoid forward declaration of an enumBeat Bolli, Jul 9, 2018
  34. 5/6 string-list.c: avoid conversion from void * to function pointerBeat Bolli, Jul 9, 2018
  35. 3/6 convert.c: replace "\e" escapes with "\033".Beat Bolli, Jul 9, 2018
  36. 2/6 refs/refs-internal.h: avoid forward declaration of an enumBeat Bolli, Jul 9, 2018
  37. 6/6 utf8.c: avoid char overflowBeat Bolli, Jul 9, 2018
  38. 4/6 sequencer.c: avoid empty statements at top levelBeat Bolli, Jul 9, 2018
  39. Junio C HamanoJul 9, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.