Fossil SCM
Fix possible misaligned pointer to a 16-bit object.
Commit
f7c41be825ba568460bf43a63c551e1848e8f0f2fee533682d6083e1c02bad7a
Parent
f146e21af9eaa2f…
1 file changed
+6
-4
+6
-4
| --- src/lookslike.c | ||
| +++ src/lookslike.c | ||
| @@ -363,19 +363,21 @@ | ||
| 363 | 363 | int starts_with_utf16_bom( |
| 364 | 364 | const Blob *pContent, /* IN: Blob content to perform BOM detection on. */ |
| 365 | 365 | int *pnByte, /* OUT: The number of bytes used for the BOM. */ |
| 366 | 366 | int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */ |
| 367 | 367 | ){ |
| 368 | - const unsigned short *z = (unsigned short *)blob_buffer(pContent); | |
| 368 | + const unsigned char *z = (unsigned char *)blob_buffer(pContent); | |
| 369 | 369 | int bomSize = sizeof(unsigned short); |
| 370 | 370 | int size = blob_size(pContent); |
| 371 | + unsigned short i0; | |
| 371 | 372 | |
| 372 | 373 | 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 ){ | |
| 375 | 377 | if( pbReverse ) *pbReverse = 0; |
| 376 | - }else if( z[0]==0xfffe ){ | |
| 378 | + }else if( i0==0xfffe ){ | |
| 377 | 379 | if( pbReverse ) *pbReverse = 1; |
| 378 | 380 | }else{ |
| 379 | 381 | static const int one = 1; |
| 380 | 382 | noBom: |
| 381 | 383 | if( pbReverse ) *pbReverse = *(char *) &one; |
| 382 | 384 |
| --- 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 |