@@ -231,7 +231,10 @@ impl Default for BlockHeader {
231
231
BlockHeader {
232
232
version : HeaderVersion ( 1 ) ,
233
233
height : 0 ,
234
- timestamp : DateTime :: < Utc > :: from_utc ( NaiveDateTime :: from_timestamp ( 0 , 0 ) , Utc ) ,
234
+ timestamp : DateTime :: < Utc > :: from_utc (
235
+ NaiveDateTime :: from_timestamp_opt ( 0 , 0 ) . unwrap ( ) ,
236
+ Utc ,
237
+ ) ,
235
238
prev_hash : ZERO_HASH ,
236
239
prev_root : ZERO_HASH ,
237
240
output_root : ZERO_HASH ,
@@ -288,16 +291,29 @@ fn read_block_header<R: Reader>(reader: &mut R) -> Result<BlockHeader, ser::Erro
288
291
let ( output_mmr_size, kernel_mmr_size) = ser_multiread ! ( reader, read_u64, read_u64) ;
289
292
let pow = ProofOfWork :: read ( reader) ?;
290
293
291
- if timestamp > chrono:: NaiveDate :: MAX . and_hms ( 0 , 0 , 0 ) . timestamp ( )
292
- || timestamp < chrono:: NaiveDate :: MIN . and_hms ( 0 , 0 , 0 ) . timestamp ( )
294
+ if timestamp
295
+ > chrono:: NaiveDate :: MAX
296
+ . and_hms_opt ( 0 , 0 , 0 )
297
+ . unwrap ( )
298
+ . timestamp ( )
299
+ || timestamp
300
+ < chrono:: NaiveDate :: MIN
301
+ . and_hms_opt ( 0 , 0 , 0 )
302
+ . unwrap ( )
303
+ . timestamp ( )
293
304
{
294
305
return Err ( ser:: Error :: CorruptedData ) ;
295
306
}
296
307
308
+ let ts = NaiveDateTime :: from_timestamp_opt ( timestamp, 0 ) ;
309
+ if ts. is_none ( ) {
310
+ return Err ( ser:: Error :: CorruptedData ) ;
311
+ }
312
+
297
313
Ok ( BlockHeader {
298
314
version,
299
315
height,
300
- timestamp : DateTime :: < Utc > :: from_utc ( NaiveDateTime :: from_timestamp ( timestamp , 0 ) , Utc ) ,
316
+ timestamp : DateTime :: < Utc > :: from_utc ( ts . unwrap ( ) , Utc ) ,
301
317
prev_hash,
302
318
prev_root,
303
319
output_root,
@@ -645,8 +661,13 @@ impl Block {
645
661
let version = consensus:: header_version ( height) ;
646
662
647
663
let now = Utc :: now ( ) . timestamp ( ) ;
648
- let timestamp = DateTime :: < Utc > :: from_utc ( NaiveDateTime :: from_timestamp ( now, 0 ) , Utc ) ;
649
664
665
+ let ts = NaiveDateTime :: from_timestamp_opt ( now, 0 ) ;
666
+ if ts. is_none ( ) {
667
+ return Err ( Error :: Other ( "Converting Utc::now() into timestamp" . into ( ) ) ) ;
668
+ }
669
+
670
+ let timestamp = DateTime :: < Utc > :: from_utc ( ts. unwrap ( ) , Utc ) ;
650
671
// Now build the block with all the above information.
651
672
// Note: We have not validated the block here.
652
673
// Caller must validate the block as necessary.
0 commit comments