@@ -84,45 +84,66 @@ private IccProfileHeader ReadHeader(IccDataReader reader)
8484 private IccTagDataEntry [ ] ReadTagData ( IccDataReader reader )
8585 {
8686 IccTagTableEntry [ ] tagTable = this . ReadTagTable ( reader ) ;
87- var entries = new IccTagDataEntry [ tagTable . Length ] ;
87+ var entries = new List < IccTagDataEntry > ( tagTable . Length ) ;
8888 var store = new Dictionary < uint , IccTagDataEntry > ( ) ;
89- for ( int i = 0 ; i < tagTable . Length ; i ++ )
89+
90+ foreach ( IccTagTableEntry tag in tagTable )
9091 {
9192 IccTagDataEntry entry ;
92- uint offset = tagTable [ i ] . Offset ;
93- if ( store . ContainsKey ( offset ) )
93+ if ( store . ContainsKey ( tag . Offset ) )
9494 {
95- entry = store [ offset ] ;
95+ entry = store [ tag . Offset ] ;
9696 }
9797 else
9898 {
99- entry = reader . ReadTagDataEntry ( tagTable [ i ] ) ;
100- store . Add ( offset , entry ) ;
99+ try
100+ {
101+ entry = reader . ReadTagDataEntry ( tag ) ;
102+ }
103+ catch
104+ {
105+ // Ignore tags that could not be read
106+ continue ;
107+ }
108+
109+ store . Add ( tag . Offset , entry ) ;
101110 }
102111
103- entry . TagSignature = tagTable [ i ] . Signature ;
104- entries [ i ] = entry ;
112+ entry . TagSignature = tag . Signature ;
113+ entries . Add ( entry ) ;
105114 }
106115
107- return entries ;
116+ return entries . ToArray ( ) ;
108117 }
109118
110119 private IccTagTableEntry [ ] ReadTagTable ( IccDataReader reader )
111120 {
112121 reader . SetIndex ( 128 ) ; // An ICC header is 128 bytes long
113122
114123 uint tagCount = reader . ReadUInt32 ( ) ;
115- var table = new IccTagTableEntry [ tagCount ] ;
116124
125+ // Prevent creating huge arrays because of corrupt profiles.
126+ // A normal profile usually has 5-15 entries
127+ if ( tagCount > 100 )
128+ {
129+ return new IccTagTableEntry [ 0 ] ;
130+ }
131+
132+ var table = new List < IccTagTableEntry > ( ( int ) tagCount ) ;
117133 for ( int i = 0 ; i < tagCount ; i ++ )
118134 {
119135 uint tagSignature = reader . ReadUInt32 ( ) ;
120136 uint tagOffset = reader . ReadUInt32 ( ) ;
121137 uint tagSize = reader . ReadUInt32 ( ) ;
122- table [ i ] = new IccTagTableEntry ( ( IccProfileTag ) tagSignature , tagOffset , tagSize ) ;
138+
139+ // Exclude entries that have nonsense values and could cause exceptions further on
140+ if ( tagOffset < reader . DataLength && tagSize < reader . DataLength - 128 )
141+ {
142+ table . Add ( new IccTagTableEntry ( ( IccProfileTag ) tagSignature , tagOffset , tagSize ) ) ;
143+ }
123144 }
124145
125- return table ;
146+ return table . ToArray ( ) ;
126147 }
127148 }
128149}
0 commit comments