@@ -2805,35 +2805,41 @@ JSON_read(int fd)
2805
2805
* Then read the JSON into a buffer and parse it. Return a parsed JSON
2806
2806
* structure, NULL if there was an error.
2807
2807
*/
2808
- if (Nread (fd , (char * ) & nsize , sizeof (nsize ), Ptcp ) >= 0 ) {
2809
- hsize = ntohl (nsize );
2810
- /* Allocate a buffer to hold the JSON */
2811
- strsize = hsize + 1 ; /* +1 for trailing NULL */
2812
- if (strsize ) {
2813
- str = (char * ) calloc (sizeof (char ), strsize );
2814
- if (str != NULL ) {
2815
- rc = Nread (fd , str , hsize , Ptcp );
2816
- if (rc >= 0 ) {
2817
- /*
2818
- * We should be reading in the number of bytes corresponding to the
2819
- * length in that 4-byte integer. If we don't the socket might have
2820
- * prematurely closed. Only do the JSON parsing if we got the
2821
- * correct number of bytes.
2822
- */
2823
- if (rc == hsize ) {
2824
- json = cJSON_Parse (str );
2825
- }
2826
- else {
2827
- printf ("WARNING: Size of data read does not correspond to offered length\n" );
2828
- }
2829
- }
2830
- }
2831
- free (str );
2808
+ rc = Nread (fd , (char * ) & nsize , sizeof (nsize ), Ptcp );
2809
+ if (rc == sizeof (nsize )) {
2810
+ hsize = ntohl (nsize );
2811
+ if (hsize > 0 && hsize <= MAX_PARAMS_JSON_STRING ) {
2812
+ /* Allocate a buffer to hold the JSON */
2813
+ strsize = hsize + 1 ; /* +1 for trailing NULL */
2814
+ if (strsize ) {
2815
+ str = (char * ) calloc (sizeof (char ), strsize );
2816
+ if (str != NULL ) {
2817
+ rc = Nread (fd , str , hsize , Ptcp );
2818
+ if (rc >= 0 ) {
2819
+ /*
2820
+ * We should be reading in the number of bytes corresponding to the
2821
+ * length in that 4-byte integer. If we don't the socket might have
2822
+ * prematurely closed. Only do the JSON parsing if we got the
2823
+ * correct number of bytes.
2824
+ */
2825
+ if (rc == hsize ) {
2826
+ json = cJSON_Parse (str );
2827
+ }
2828
+ else {
2829
+ warning ("JSON size of data read does not correspond to offered length" );
2830
+ }
2831
+ }
2832
+ free (str );
2833
+ }
2834
+ }
2832
2835
}
2833
2836
else {
2834
- printf ( "WARNING: Data length overflow\n " );
2837
+ warning ( "JSON data length overflow" );
2835
2838
}
2836
2839
}
2840
+ else {
2841
+ warning ("Failed to read JSON data size" );
2842
+ }
2837
2843
return json ;
2838
2844
}
2839
2845
0 commit comments