ソースを参照

[base64] Avoid overrunning input data buffer

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12年前
コミット
40e68e1119
1個のファイルの変更7行の追加2行の削除
  1. 7
    2
      src/core/base64.c

+ 7
- 2
src/core/base64.c ファイルの表示

@@ -54,11 +54,16 @@ void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
54 54
 	uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
55 55
 	size_t raw_bit_len = ( 8 * len );
56 56
 	unsigned int bit;
57
+	unsigned int byte;
58
+	unsigned int shift;
57 59
 	unsigned int tmp;
58 60
 
59 61
 	for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
60
-		tmp = ( ( raw_bytes[ bit / 8 ] << ( bit % 8 ) ) |
61
-			( raw_bytes[ bit / 8 + 1 ] >> ( 8 - ( bit % 8 ) ) ) );
62
+		byte = ( bit / 8 );
63
+		shift = ( bit % 8 );
64
+		tmp = ( raw_bytes[byte] << shift );
65
+		if ( ( byte + 1 ) < len )
66
+			tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
62 67
 		tmp = ( ( tmp >> 2 ) & 0x3f );
63 68
 		*(encoded_bytes++) = base64[tmp];
64 69
 	}

読み込み中…
キャンセル
保存