Fossil SCM

Fix possible misaligned pointer to a 16-bit object.

drh 2019-08-20 16:11 trunk
Commit f7c41be825ba568460bf43a63c551e1848e8f0f2fee533682d6083e1c02bad7a
1 file changed +6 -4
+6 -4
--- src/lookslike.c
+++ src/lookslike.c
@@ -363,19 +363,21 @@
363363
int starts_with_utf16_bom(
364364
const Blob *pContent, /* IN: Blob content to perform BOM detection on. */
365365
int *pnByte, /* OUT: The number of bytes used for the BOM. */
366366
int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */
367367
){
368
- const unsigned short *z = (unsigned short *)blob_buffer(pContent);
368
+ const unsigned char *z = (unsigned char *)blob_buffer(pContent);
369369
int bomSize = sizeof(unsigned short);
370370
int size = blob_size(pContent);
371
+ unsigned short i0;
371372
372373
if( size<bomSize ) goto noBom; /* No: cannot read BOM. */
373
- if( size>=(2*bomSize) && z[1]==0 ) goto noBom; /* No: possible UTF-32. */
374
- if( z[0]==0xfeff ){
374
+ if( size>=(2*bomSize) && z[2]==0 && z[3]==0 ) goto noBom;
375
+ memcpy(&i0, z, sizeof(i0));
376
+ if( i0==0xfeff ){
375377
if( pbReverse ) *pbReverse = 0;
376
- }else if( z[0]==0xfffe ){
378
+ }else if( i0==0xfffe ){
377379
if( pbReverse ) *pbReverse = 1;
378380
}else{
379381
static const int one = 1;
380382
noBom:
381383
if( pbReverse ) *pbReverse = *(char *) &one;
382384
--- src/lookslike.c
+++ src/lookslike.c
@@ -363,19 +363,21 @@
363 int starts_with_utf16_bom(
364 const Blob *pContent, /* IN: Blob content to perform BOM detection on. */
365 int *pnByte, /* OUT: The number of bytes used for the BOM. */
366 int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */
367 ){
368 const unsigned short *z = (unsigned short *)blob_buffer(pContent);
369 int bomSize = sizeof(unsigned short);
370 int size = blob_size(pContent);
 
371
372 if( size<bomSize ) goto noBom; /* No: cannot read BOM. */
373 if( size>=(2*bomSize) && z[1]==0 ) goto noBom; /* No: possible UTF-32. */
374 if( z[0]==0xfeff ){
 
375 if( pbReverse ) *pbReverse = 0;
376 }else if( z[0]==0xfffe ){
377 if( pbReverse ) *pbReverse = 1;
378 }else{
379 static const int one = 1;
380 noBom:
381 if( pbReverse ) *pbReverse = *(char *) &one;
382
--- src/lookslike.c
+++ src/lookslike.c
@@ -363,19 +363,21 @@
363 int starts_with_utf16_bom(
364 const Blob *pContent, /* IN: Blob content to perform BOM detection on. */
365 int *pnByte, /* OUT: The number of bytes used for the BOM. */
366 int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */
367 ){
368 const unsigned char *z = (unsigned char *)blob_buffer(pContent);
369 int bomSize = sizeof(unsigned short);
370 int size = blob_size(pContent);
371 unsigned short i0;
372
373 if( size<bomSize ) goto noBom; /* No: cannot read BOM. */
374 if( size>=(2*bomSize) && z[2]==0 && z[3]==0 ) goto noBom;
375 memcpy(&i0, z, sizeof(i0));
376 if( i0==0xfeff ){
377 if( pbReverse ) *pbReverse = 0;
378 }else if( i0==0xfffe ){
379 if( pbReverse ) *pbReverse = 1;
380 }else{
381 static const int one = 1;
382 noBom:
383 if( pbReverse ) *pbReverse = *(char *) &one;
384

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button