public
Description: Cross language RPC
Homepage: http://developers.facebook.com/thrift/
Clone URL: git://github.com/kevinclark/thrift.git
Search Repo:
rb: Bubble up errors from calling borrow and consume so they can be 
handled
kevinclark (author)
Mon Apr 28 16:14:31 -0700 2008
commit  dbfe4000510bd38105a4be05b24912f4f330343f
tree    0eac4cbe8b21c01645318a499d3e47632b072fdb
parent  86eb40f9944e7fb330760e17d9517b1164582790
...
12
13
14
 
15
16
17
...
392
393
394
395
396
397
398
399
...
500
501
502
503
504
 
 
505
506
507
508
509
 
510
511
512
513
514
515
516
517
518
519
520
...
527
528
529
530
 
 
 
 
 
 
 
 
 
 
 
531
532
 
 
 
 
 
 
 
 
 
 
 
533
 
 
 
534
535
536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
537
538
 
539
540
 
541
 
 
 
 
542
543
544
545
546
547
548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549
550
551
 
 
552
553
554
...
569
570
571
572
 
 
 
 
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
...
581
582
583
584
585
586
587
 
 
588
589
590
591
592
593
 
 
 
 
 
594
595
596
597
598
599
 
 
 
 
 
600
601
602
603
604
605
 
 
 
 
 
606
607
608
609
610
611
612
613
614
615
 
 
616
617
618
619
620
 
 
621
 
 
 
 
622
623
 
624
625
626
627
628
629
 
 
 
630
631
632
 
 
 
 
633
 
 
634
635
636
637
 
638
639
640
 
641
642
643
644
645
646
 
 
 
 
 
647
648
649
650
 
 
 
651
 
 
652
653
654
655
656
 
657
658
659
660
661
662
663
 
 
 
664
665
666
667
668
 
669
670
671
672
673
674
 
 
 
 
 
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
...
681
682
683
684
 
685
686
687
 
 
 
 
 
 
688
689
 
690
691
 
692
 
 
693
694
695
696
 
 
 
 
 
 
 
 
 
 
697
698
699
 
 
 
 
 
 
 
 
 
 
700
701
702
 
 
 
 
 
 
 
 
 
 
703
704
705
 
 
 
 
 
 
 
 
 
 
706
707
708
 
 
 
 
 
 
 
 
 
 
709
710
711
 
 
 
 
 
 
 
 
712
713
714
715
 
716
717
718
719
720
721
722
723
 
 
 
724
725
726
727
...
728
729
730
731
 
732
733
734
735
 
 
 
736
737
738
739
740
741
742
743
...
744
745
746
747
748
 
 
 
 
 
 
 
 
 
 
749
750
751
752
753
754
 
 
755
756
757
758
759
 
760
761
762
763
764
765
766
 
 
 
 
 
767
768
769
770
771
 
 
772
773
774
775
 
776
777
778
779
780
781
782
...
780
781
782
783
 
 
 
 
 
784
785
786
 
 
787
788
789
790
791
 
792
 
 
793
794
 
 
 
 
 
 
 
 
 
 
 
795
796
797
...
806
807
808
809
 
 
 
 
810
811
812
813
814
...
814
815
816
817
 
 
 
 
818
819
820
821
822
823
 
 
 
 
 
824
825
 
 
 
 
 
 
 
826
827
828
...
849
850
851
852
 
 
 
 
 
853
854
855
856
857
858
...
880
881
882
883
 
884
885
 
 
 
 
 
 
886
887
888
 
889
890
891
892
893
894
895
896
897
 
 
 
 
 
 
 
 
 
 
 
 
 
898
899
900
...
12
13
14
15
16
17
18
...
393
394
395
 
396
397
398
399
...
500
501
502
 
 
503
504
505
506
507
508
 
509
510
511
512
513
514
515
516
517
518
519
520
...
527
528
529
 
530
531
532
533
534
535
536
537
538
539
540
541
 
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
 
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
 
583
584
 
585
586
587
588
589
590
591
592
593
 
 
 
 
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
...
630
631
632
 
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
...
645
646
647
 
 
 
 
648
649
650
651
 
 
 
 
652
653
654
655
656
657
658
 
 
 
 
659
660
661
662
663
664
665
 
 
 
 
666
667
668
669
670
671
672
 
 
 
 
 
 
 
 
673
674
675
676
 
 
 
677
678
679
680
681
682
683
684
 
685
686
687
688
689
690
 
691
692
693
694
 
 
695
696
697
698
699
700
701
702
703
704
 
705
706
707
 
708
709
710
711
712
713
 
714
715
716
717
718
719
720
721
 
722
723
724
725
726
727
728
729
730
731
 
732
733
734
735
736
 
 
 
737
738
739
740
741
742
743
 
744
745
746
747
748
 
 
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
...
760
761
762
 
763
764
765
 
766
767
768
769
770
771
772
 
773
774
 
775
776
777
778
779
780
 
 
781
782
783
784
785
786
787
788
789
790
791
 
 
792
793
794
795
796
797
798
799
800
801
802
 
 
803
804
805
806
807
808
809
810
811
812
813
 
 
814
815
816
817
818
819
820
821
822
823
824
 
 
825
826
827
828
829
830
831
832
833
834
835
836
 
837
838
839
840
841
842
843
844
845
846
847
 
848
849
850
851
852
853
854
 
 
855
856
857
858
859
860
861
...
862
863
864
 
865
866
867
868
 
869
870
871
872
873
874
875
876
877
878
879
...
880
881
882
 
 
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
 
898
899
900
901
902
903
 
904
905
906
907
908
909
910
 
911
912
913
914
915
916
917
918
919
 
920
921
922
923
924
 
925
926
927
928
929
930
931
932
...
930
931
932
 
933
934
935
936
937
938
939
 
940
941
942
943
944
945
 
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
...
974
975
976
 
977
978
979
980
981
982
983
984
985
...
985
986
987
 
988
989
990
991
992
993
994
995
996
 
997
998
999
1000
1001
1002
 
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
...
1033
1034
1035
 
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
...
1068
1069
1070
 
1071
1072
 
1073
1074
1075
1076
1077
1078
1079
1080
 
1081
1082
1083
1084
1085
1086
 
 
 
 
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
0
@@ -12,6 +12,7 @@
0
 
0
 #ifndef HAVE_STRLCPY
0
 
0
+static
0
 size_t
0
 strlcpy (char *dst, const char *src, size_t dst_sz)
0
 {
0
@@ -392,7 +393,6 @@
0
   name_buf[0] = '@';
0
   strlcpy(&name_buf[1], spec->name, sizeof(name_buf) - 1);
0
   
0
- // TODO(kevinclark): Replace with the strlcpy method used in read
0
   VALUE value = rb_ivar_get(obj, rb_intern(name_buf));
0
   
0
   if (Qnil == value) {
0
0
@@ -500,13 +500,13 @@
0
 } field_header;
0
 
0
 typedef struct {
0
- int key_type;
0
- int val_type;
0
+ int8_t key_type;
0
+ int8_t val_type;
0
   int num_entries;
0
 } map_header;
0
 
0
 typedef struct {
0
- intt type;
0
+ int8_t type;
0
   int num_elements;
0
 } list_header;
0
 
0
0
0
0
0
0
0
0
0
@@ -527,28 +527,89 @@
0
 #define read_struct_begin(buf)
0
 #define read_struct_end(buf)
0
 
0
-static void consume(decode_buffer* buf, int32_t size) {
0
+// This prototype is required to be able to run a call through rb_protect
0
+// which rescues from ruby exceptions
0
+static VALUE protectable_consume(VALUE args) {
0
+ VALUE trans = rb_ary_entry(args, 0);
0
+ VALUE size = rb_ary_entry(args, 1);
0
+
0
+ return rb_funcall(trans, consume_bang_id, 1, size);
0
+}
0
+
0
+// Clears size bytes from the transport's string buffer
0
+static bool consume(decode_buffer* buf, int32_t size) {
0
   if (size != 0) {
0
- rb_funcall(buf->trans, consume_bang_id, 1, INT2FIX(size));
0
+ VALUE ret;
0
+ VALUE args = rb_ary_new3(2, buf->trans, INT2FIX(size));
0
+ int status = 0;
0
+
0
+ ret = rb_protect(protectable_consume, args, &status);
0
+
0
+ if (status) {
0
+ return false;
0
+ } else {
0
+ return true;
0
+ }
0
   }
0
+
0
+ // Nothing to consume, we're all good
0
+ return true;
0
 }
0
 
0
-static VALUE borrow(decode_buffer* buf, int32_t size) {
0
+// This prototype is required to be able to run a call through rb_protect
0
+// which rescues from ruby exceptions
0
+static VALUE protectable_borrow(VALUE args) {
0
+ VALUE trans = rb_ary_entry(args, 0);
0
+
0
+ switch(RARRAY(args)->len) {
0
+ case 1:
0
+ return rb_funcall(trans, borrow_id, 0);
0
+
0
+ case 2: {
0
+ VALUE size = rb_ary_entry(args, 1);
0
+ return rb_funcall(trans, borrow_id, 1, size);
0
+ }
0
+ }
0
+
0
+ return Qnil;
0
+}
0
+
0
+// Calls into the transport to get the available string buffer
0
+static bool borrow(decode_buffer* buf, int32_t size, VALUE* dst) {
0
+ int status = 0;
0
+ VALUE args;
0
+
0
   if (size == 0) {
0
- return rb_funcall(buf->trans, borrow_id, 0);
0
+ args = rb_ary_new3(1, buf->trans);
0
   } else {
0
- return rb_funcall(buf->trans, borrow_id, 1, INT2FIX(size));
0
+ args = rb_ary_new3(2, buf->trans, INT2FIX(size));
0
   }
0
+
0
+ *dst = rb_protect(protectable_borrow, args, &status);
0
+
0
+ return (status == 0);
0
 }
0
 
0
 // Refills the buffer by calling borrow. If buf->pos is nonzero that number of bytes
0
-// is cleared through consume
0
-static void fill_buffer(decode_buffer* buf, int32_t req_len) {
0
- consume(buf, buf->pos);
0
- VALUE refill = borrow(buf, req_len);
0
+// is cleared through consume.
0
+//
0
+// returns: 0 on success, non-zero on failure. On error buf is unchanged.
0
+static int fill_buffer(decode_buffer* buf, int32_t req_len) {
0
+ VALUE refill;
0
+
0
+ if (!consume(buf, buf->pos)) {
0
+ return -1;
0
+ }
0
+
0
+ if (!borrow(buf, req_len, &refill)) {
0
+ return -2;
0
+ }
0
+
0
   buf->data = StringValuePtr(refill);
0
   buf->len = RSTRING(refill)->len;
0
   buf->pos = 0;
0
+
0
+ return 0;
0
 }
0
 
0
 
0
@@ -569,7 +630,10 @@
0
       buf->pos += avail;
0
     }
0
 
0
- fill_buffer(buf, size);
0
+ if (fill_buffer(buf, size - avail) < 0) {
0
+ return false;
0
+ }
0
+
0
     memcpy(dst + avail, buf->data, size - avail);
0
     buf->pos += size - avail;
0
   }
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
@@ -581,97 +645,112 @@
0
 // Helpers for grabbing specific types from the buffer
0
 // -----------------------------------------------------------------------------
0
 
0
-static int8_t read_byte(decode_buffer* buf) {
0
- int8_t data;
0
- read_bytes(buf, &data, sizeof(int8_t));
0
- return data;
0
+static bool read_byte(decode_buffer* buf, int8_t* data) {
0
+ return read_bytes(buf, data, sizeof(int8_t));
0
 }
0
 
0
-static int16_t read_int16(decode_buffer* buf) {
0
- int16_t data;
0
- read_bytes(buf, &data, sizeof(int16_t));
0
- return ntohs(data);
0
+static bool read_int16(decode_buffer* buf, int16_t* data) {
0
+ bool success = read_bytes(buf, data, sizeof(int16_t));
0
+ *data = ntohs(*data);
0
+
0
+ return success;
0
 }
0
 
0
-static int32_t read_int32(decode_buffer* buf) {
0
- int32_t data;
0
- read_bytes(buf, &data, sizeof(int32_t));
0
- return ntohl(data);
0
+static bool read_int32(decode_buffer* buf, int32_t* data) {
0
+ bool success = read_bytes(buf, data, sizeof(int32_t));
0
+ *data = ntohl(*data);
0
+
0
+ return success;
0
 }
0
 
0
-static int64_t read_int64(decode_buffer* buf) {
0
- int64_t data;
0
- read_bytes(buf, &data, sizeof(int64_t));
0
- return ntohll(data);
0
+static bool read_int64(decode_buffer* buf, int64_t* data) {
0
+ bool success = read_bytes(buf, data, sizeof(int64_t));
0
+ *data = ntohll(*data);
0
+
0
+ return success;
0
 }
0
 
0
-static double read_double(decode_buffer* buf) {
0
- union {
0
- double f;
0
- int64_t t;
0
- } transfer;
0
-
0
- transfer.t = read_int64(buf);
0
- return transfer.f;
0
+static bool read_double(decode_buffer* buf, double* data) {
0
+ return read_int64(buf, (int64_t*)data);
0
 }
0
 
0
-static VALUE read_string(decode_buffer* buf) {
0
- VALUE ret_str;
0
- int len = read_int32(buf);
0
+static bool read_string(decode_buffer* buf, VALUE* data) {
0
+ int len;
0
   
0
+ if (!read_int32(buf, &len)) {
0
+ return false;
0
+ }
0
+
0
   if (buf->len - buf->pos >= len) {
0
- ret_str = rb_str_new(buf->data + buf->pos, len);
0
+ *data = rb_str_new(buf->data + buf->pos, len);
0
     buf->pos += len;
0
   }
0
   else {
0
     char* str;
0
     
0
- str = (char*) malloc(len);
0
+ if ((str = (char*) malloc(len)) == NULL) {
0
+ return false;
0
+ }
0
     
0
- read_bytes(buf, str, len);
0
- ret_str = rb_str_new(str, len);
0
+ if (!read_bytes(buf, str, len)) {
0
+ free(str);
0
+ return false;
0
+ }
0
     
0
+ *data = rb_str_new(str, len);
0
+
0
     free(str);
0
   }
0
   
0
- return ret_str;
0
+ return true;
0
 }
0
 
0
-static void read_field_begin(decode_buffer* buf, field_header* header) {
0
+static bool read_field_begin(decode_buffer* buf, field_header* header) {
0
 #ifdef __DEBUG__ // No need for this in prod since I set all the fields
0
   bzero(header, sizeof(field_header));
0
 #endif
0
 
0
   header->name = NULL;
0
- header->type = read_byte(buf);
0
+
0
+ if (!read_byte(buf, &header->type)) {
0
+ return false;
0
+ }
0
+
0
   if (header->type == T_STOP) {
0
     header->id = 0;
0
   } else {
0
- header->id = read_int16(buf);
0
+ if (!read_int16(buf, &header->id)) {
0
+ return false;
0
+ }
0
   }
0
+
0
+ return true;
0
 }
0
 
0
 #define read_field_end(buf)
0
 
0
-static void read_map_begin(decode_buffer* buf, map_header* header) {
0
+static bool read_map_begin(decode_buffer* buf, map_header* header) {
0
 #ifdef __DEBUG__ // No need for this in prod since I set all the fields
0
   bzero(header, sizeof(map_header));
0
 #endif
0
   
0
- header->key_type = read_byte(buf);
0
- header->val_type = read_byte(buf);
0
- header->num_entries = read_int32(buf);
0
+ return (read_byte(buf, &header->key_type) &&
0
+ read_byte(buf, &header->val_type) &&
0
+ read_int32(buf, &header->num_entries));
0
 }
0
 
0
 #define read_map_end(buf)
0
 
0
-static void read_list_begin(decode_buffer* buf, list_header* header) {
0
+static bool read_list_begin(decode_buffer* buf, list_header* header) {
0
 #ifdef __DEBUG__ // No need for this in prod since I set all the fields
0
   bzero(header, sizeof(list_header));
0
 #endif
0
   
0
- header->type = read_byte(buf);
0
- header->num_elements = read_int32(buf);
0
+ if (!read_byte(buf, &header->type) || !read_int32(buf, &header->num_elements)) {
0
+ return false;
0
+ } else {
0
+ return true;
0
+ }
0
 }
0
 
0
 #define read_list_end(buf)
0
0
0
0
0
0
0
0
0
0
0
0
0
@@ -681,46 +760,101 @@
0
 
0
 
0
 // High level reader function with ruby type coercion
0
-static VALUE read_type(int type, decode_buffer* buf) {
0
+static bool read_type(int type, decode_buffer* buf, VALUE* dst) {
0
   switch(type) {
0
     case T_BOOL: {
0
- int8_t byte = read_byte(buf);
0
+ int8_t byte;
0
+
0
+ if (!read_byte(buf, &byte)) {
0
+ return false;
0
+ }
0
+
0
       if (0 == byte) {
0
- return Qfalse;
0
+ *dst = Qfalse;
0
       } else {
0
- return Qtrue;
0
+ *dst = Qtrue;
0
       }
0
+
0
+ break;
0
     }
0
     
0
- case T_BYTE:
0
- return INT2FIX(read_byte(buf));
0
+ case T_BYTE: {
0
+ int8_t byte;
0
+
0
+ if (!read_byte(buf, &byte)) {
0
+ return false;
0
+ }
0
+
0
+ *dst = INT2FIX(byte);
0
+ break;
0
+ }
0
     
0
- case T_I16:
0
- return INT2FIX(read_int16(buf));
0
+ case T_I16: {
0
+ int16_t i16;
0
+
0
+ if (!read_int16(buf, &i16)) {
0
+ return false;
0
+ }
0
+
0
+ *dst = INT2FIX(i16);
0
+ break;
0
+ }
0
 
0
- case T_I32:
0
- return INT2NUM(read_int32(buf));
0
+ case T_I32: {
0
+ int32_t i32;
0
+
0
+ if (!read_int32(buf, &i32)) {
0
+ return false;
0
+ }
0
+
0
+ *dst = INT2NUM(i32);
0
+ break;
0
+ }
0
 
0
- case T_I64:
0
- return rb_ll2inum(read_int64(buf));
0
+ case T_I64: {
0
+ int64_t i64;
0
+
0
+ if (!read_int64(buf, &i64)) {
0
+ return false;
0
+ }
0
+
0
+ *dst = rb_ll2inum(i64);
0
+ break;
0
+ }
0
     
0
- case T_DBL:
0
- return rb_float_new(read_double(buf));
0
+ case T_DBL: {
0
+ double dbl;
0
+
0
+ if (!read_double(buf, &dbl)) {
0
+ return false;
0
+ }
0
+
0
+ *dst = rb_float_new(dbl);
0
+ break;
0
+ }
0
 
0
     case T_STR: {
0
- return read_string(buf);
0
+ VALUE str;
0
+
0
+ if (!read_string(buf, &str)) {
0
+ return false;
0
+ }
0
+
0
+ *dst = str;
0
+ break;
0
     }
0
   }
0
   
0
- return Qnil;
0
+ return true;
0
 }
0
 
0
 // TODO(kevinclark): Now that read_string does a malloc,
0
 // This maybe could be modified to avoid that, and the type coercion
0
 
0
 // Read the bytes but don't do anything with the value
0
-static void skip_type(int type, decode_buffer* buf) {
0
- read_type(type, buf);
0
+static bool skip_type(int type, decode_buffer* buf) {
0
+ VALUE v;
0
+ return read_type(type, buf, &v);
0
 }
0
 
0
 
0
0
@@ -728,11 +862,13 @@
0
 
0
 // Read the right thing from the buffer given the field spec
0
 // and return the ruby object
0
-static VALUE read_field(decode_buffer* buf, field_spec* spec) {
0
+static bool read_field(decode_buffer* buf, field_spec* spec, VALUE* dst) {
0
   switch (spec->type) {
0
     case T_STRCT: {
0
       VALUE obj = rb_class_new_instance(0, NULL, spec->data.class);
0
- return read_struct(obj, buf);
0
+
0
+ *dst = read_struct(obj, buf);
0
+ break;
0
     }
0
     
0
     case T_MAP: {
0
0
0
0
0
0
@@ -744,35 +880,49 @@
0
       hsh = rb_hash_new();
0
       
0
       for (i = 0; i < hdr.num_entries; ++i) {
0
- VALUE key = read_field(buf, spec->data.map->key);
0
- VALUE val = read_field(buf, spec->data.map->value);
0
+ VALUE key, val;
0
+
0
+ if (!read_field(buf, spec->data.map->key, &key)) {
0
+ return false;
0
+ }
0
+
0
+ if (!read_field(buf, spec->data.map->value, &val)) {
0
+ return false;
0
+ }
0
+
0
         rb_hash_aset(hsh, key, val);
0
       }
0
       
0
       read_map_end(buf);
0
       
0
- return hsh;
0
+ *dst = hsh;
0
+ break;
0
     }
0
     
0
     case T_LIST: {
0
       list_header hdr;
0
- VALUE arr;
0
+ VALUE arr, element;
0
       int i;
0
       
0
       read_list_begin(buf, &hdr);
0
       arr = rb_ary_new2(hdr.num_elements);
0
       
0
       for (i = 0; i < hdr.num_elements; ++i) {
0
- rb_ary_push(arr, read_field(buf, spec->data.element));
0
+ if (!read_field(buf, spec->data.element, &element)) {
0
+ return false;
0
+ }
0
+
0
+ rb_ary_push(arr, element);
0
       }
0
       
0
       read_list_end(buf);
0
       
0
- return arr;
0
+ *dst = arr;
0
+ break;
0
     }
0
     
0
     case T_SET: {
0
- VALUE set;
0
+ VALUE set, item;
0
       set_header hdr;
0
       int i;
0
       
0
0
0
0
0
@@ -780,18 +930,36 @@
0
       set = rb_hash_new();
0
       
0
       for (i = 0; i < hdr.num_elements; ++i) {
0
- rb_hash_aset(set, read_field(buf, spec->data.element), Qtrue);
0
+ if (!read_field(buf, spec->data.element, &item)) {
0
+ return false;
0
+ }
0
+
0
+ rb_hash_aset(set, item, Qtrue);
0
       }
0
       
0
- return set;
0
+ *dst = set;
0
+ break;
0
     }
0
     
0
     
0
     default:
0
- return read_type(spec->type, buf);
0
+ return read_type(spec->type, buf, dst);
0
   }
0
+
0
+ return true;
0
 }
0
 
0
+static void handle_read_error() {
0
+ // If it was an exception, reraise
0
+ if (!NIL_P(ruby_errinfo)) {
0
+ rb_exc_raise(ruby_errinfo);
0
+ } else {
0
+ // Something else went wrong, no idea what would call this yet
0
+ // So far, the only thing to cause failures underneath is ruby
0
+ // exceptions. Follow up on this regularly -- Kevin Clark (TODO)
0
+ rb_raise(rb_eStandardError, "[BUG] Something went wrong in the field reading, but not a ruby exception");
0
+ }
0
+}
0
 
0
 // Fill in the instance variables in an object (thrift struct)
0
 // from the decode buffer
0
@@ -806,7 +974,10 @@
0
   read_struct_begin(buf);
0
   
0
   while(true) {
0
- read_field_begin(buf, &f_header);
0
+ if (!read_field_begin(buf, &f_header)) {
0
+ handle_read_error();
0
+ }
0
+
0
     if (T_STOP == f_header.type) {
0
       break;
0
     }
0
0
0
@@ -814,15 +985,28 @@
0
     field = rb_hash_aref(fields, INT2FIX(f_header.id));
0
     
0
     if (NIL_P(field)) {
0
- skip_type(f_header.type, buf);
0
+ if (!skip_type(f_header.type, buf)) {
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
     }
0
     else {
0
       spec = parse_field_spec(field);
0
 
0
       if (spec->type != f_header.type) {
0
- skip_type(spec->type, buf);
0
+ if (!skip_type(spec->type, buf)) {
0
+ free_field_spec(spec);
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
       } else {
0
- value = read_field(buf, spec);
0
+ // Read busted somewhere (probably borrow/consume), bail
0
+ if (!read_field(buf, spec, &value)) {
0
+ free_field_spec(spec);
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
+
0
         name_buf[0] = '@';
0
         strlcpy(&name_buf[1], spec->name, sizeof(name_buf) - 1);
0
         
0
@@ -849,7 +1033,11 @@
0
 
0
   buf.pos = 0; // This needs to be set so an arbitrary number of bytes isn't consumed
0
   buf.trans = transport; // We need to hold this so the buffer can be refilled
0
- fill_buffer(&buf, 0);
0
+
0
+ if (fill_buffer(&buf, 0) < 0) {
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
 
0
 #ifdef __DEBUG__
0
   rb_p(rb_str_new2("Running decode binary with data:"));
0
0
0
0
@@ -880,21 +1068,35 @@
0
   
0
   buf.pos = 0; // This needs to be set so fill_buffer doesn't consume
0
   buf.trans = trans; // We need to hold this so the buffer can be refilled
0
- fill_buffer(&buf, 0);
0
+
0
 
0
- version = read_int32(&buf);
0
+ if (fill_buffer(&buf, 0) < 0 || !read_int32(&buf, &version)) {
0
+ // Consume whatever was read
0
+ consume(&buf, buf.pos);
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
   
0
   if ((version & VERSION_MASK) != VERSION_1) {
0
- ID tprotocol_exception = rb_intern("TProtocolException");
0
+ VALUE tprotocol_exception = rb_const_get(rb_cObject, rb_intern("TProtocolException"));
0
     VALUE exception = rb_funcall(tprotocol_exception, rb_intern("new"), 2, rb_const_get(tprotocol_exception, rb_intern("BAD_VERSION")), rb_str_new2("Missing version identifier"));
0
     rb_raise(exception, "");
0
   }
0
   
0
   type = version & 0x000000ff;
0
- name = read_string(&buf);
0
- seqid = read_int32(&buf);
0
-
0
- consume(&buf, buf.pos);
0
+
0
+ if (!read_string(&buf, &name) || !read_int32(&buf, &seqid)) {
0
+ // Consume whatever was read
0
+ consume(&buf, buf.pos);
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
+
0
+ // Consume whatever was read
0
+ if (consume(&buf, buf.pos) < 0) {
0
+ handle_read_error();
0
+ return Qnil;
0
+ }
0
   
0
   return rb_ary_new3(3, name, INT2FIX(type), INT2FIX(seqid));
0
 }
...
2
3
4
5
 
6
7
8
...
2
3
4
 
5
6
7
8
0
@@ -2,7 +2,7 @@
0
 require 'tbinaryprotocolaccelerated'
0
 
0
 =begin
0
-The only change required for a transport to support TFastBinaryProtocol is to implement 2 methods:
0
+The only change required for a transport to support TBinaryProtocolAccelerated is to implement 2 methods:
0
   * borrow(size), which takes an optional argument and returns atleast _size_ bytes from the transport,
0
                   or the default buffer size if no argument is given
0
   * consume!(size), which removes size bytes from the front of the buffer

Comments

    No one has commented yet.