@@ -1767,35 +1767,42 @@ static size_t read_entry(
17671767 git_index_entry * * out , const void * buffer , size_t buffer_size )
17681768{
17691769 size_t path_length , entry_size ;
1770- uint16_t flags_raw ;
17711770 const char * path_ptr ;
1772- const struct entry_short * source = buffer ;
1771+ struct entry_short source ;
17731772 git_index_entry entry = {{0 }};
17741773
17751774 if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size )
17761775 return 0 ;
17771776
1778- entry .ctime .seconds = (git_time_t )ntohl (source -> ctime .seconds );
1779- entry .ctime .nanoseconds = ntohl (source -> ctime .nanoseconds );
1780- entry .mtime .seconds = (git_time_t )ntohl (source -> mtime .seconds );
1781- entry .mtime .nanoseconds = ntohl (source -> mtime .nanoseconds );
1782- entry .dev = ntohl (source -> dev );
1783- entry .ino = ntohl (source -> ino );
1784- entry .mode = ntohl (source -> mode );
1785- entry .uid = ntohl (source -> uid );
1786- entry .gid = ntohl (source -> gid );
1787- entry .file_size = ntohl (source -> file_size );
1788- git_oid_cpy (& entry .id , & source -> oid );
1789- entry .flags = ntohs (source -> flags );
1777+ /* buffer is not guaranteed to be aligned */
1778+ memcpy (& source , buffer , sizeof (struct entry_short ));
1779+
1780+ entry .ctime .seconds = (git_time_t )ntohl (source .ctime .seconds );
1781+ entry .ctime .nanoseconds = ntohl (source .ctime .nanoseconds );
1782+ entry .mtime .seconds = (git_time_t )ntohl (source .mtime .seconds );
1783+ entry .mtime .nanoseconds = ntohl (source .mtime .nanoseconds );
1784+ entry .dev = ntohl (source .dev );
1785+ entry .ino = ntohl (source .ino );
1786+ entry .mode = ntohl (source .mode );
1787+ entry .uid = ntohl (source .uid );
1788+ entry .gid = ntohl (source .gid );
1789+ entry .file_size = ntohl (source .file_size );
1790+ git_oid_cpy (& entry .id , & source .oid );
1791+ entry .flags = ntohs (source .flags );
17901792
17911793 if (entry .flags & GIT_IDXENTRY_EXTENDED ) {
1792- const struct entry_long * source_l = ( const struct entry_long * ) source ;
1793- path_ptr = source_l -> path ;
1794+ uint16_t flags_raw ;
1795+ size_t flags_offset ;
17941796
1795- flags_raw = ntohs (source_l -> flags_extended );
1796- memcpy (& entry .flags_extended , & flags_raw , 2 );
1797+ flags_offset = offsetof(struct entry_long , flags_extended );
1798+ memcpy (& flags_raw , (const char * ) buffer + flags_offset ,
1799+ sizeof (flags_raw ));
1800+ flags_raw = ntohs (flags_raw );
1801+
1802+ memcpy (& entry .flags_extended , & flags_raw , sizeof (flags_raw ));
1803+ path_ptr = (const char * ) buffer + offsetof(struct entry_long , path );
17971804 } else
1798- path_ptr = source -> path ;
1805+ path_ptr = ( const char * ) buffer + offsetof( struct entry_short , path ) ;
17991806
18001807 path_length = entry .flags & GIT_IDXENTRY_NAMEMASK ;
18011808
@@ -1846,14 +1853,12 @@ static int read_header(struct index_header *dest, const void *buffer)
18461853
18471854static size_t read_extension (git_index * index , const char * buffer , size_t buffer_size )
18481855{
1849- const struct index_extension * source ;
18501856 struct index_extension dest ;
18511857 size_t total_size ;
18521858
1853- source = (const struct index_extension * )(buffer );
1854-
1855- memcpy (dest .signature , source -> signature , 4 );
1856- dest .extension_size = ntohl (source -> extension_size );
1859+ /* buffer is not guaranteed to be aligned */
1860+ memcpy (& dest , buffer , sizeof (struct index_extension ));
1861+ dest .extension_size = ntohl (dest .extension_size );
18571862
18581863 total_size = dest .extension_size + sizeof (struct index_extension );
18591864
0 commit comments