@@ -209,6 +209,23 @@ Local<Array> AddrTTLToArray(
209209 return Array::New (env->isolate (), ttls.out (), naddrttls);
210210}
211211
212+ int GetAnswerCountForTTLBuffer (const unsigned char * buf, int len) {
213+ static constexpr int kDNSAnswerCountOffset = 6 ;
214+ static constexpr int kAresDefaultTTLBufferLength = 256 ;
215+ if (len <= kDNSAnswerCountOffset + 1 ) {
216+ return kAresDefaultTTLBufferLength ;
217+ }
218+
219+ const int answer_count = (static_cast <int >(buf[kDNSAnswerCountOffset ]) << 8 ) |
220+ static_cast <int >(buf[kDNSAnswerCountOffset + 1 ]);
221+ return answer_count == 0 ? 1 : answer_count;
222+ }
223+
224+ template <typename T>
225+ std::vector<T> MakeAddrTTLBuffer (const unsigned char * buf, int len) {
226+ return std::vector<T>(GetAnswerCountForTTLBuffer (buf, len));
227+ }
228+
212229Maybe<int > ParseGeneralReply (Environment* env,
213230 const unsigned char * buf,
214231 int len,
@@ -1105,11 +1122,12 @@ Maybe<int> AnyTraits::Parse(QueryAnyWrap* wrap,
11051122 int type, status, old_count;
11061123
11071124 /* Parse A records or CNAME records */
1108- ares_addrttl addrttls[256 ];
1109- int naddrttls = arraysize (addrttls);
1125+ std::vector<ares_addrttl> addrttls =
1126+ MakeAddrTTLBuffer<ares_addrttl>(buf, len);
1127+ int naddrttls = static_cast <int >(addrttls.size ());
11101128
11111129 type = ns_t_cname_or_a;
1112- if (!ParseGeneralReply (env, buf, len, &type, ret, addrttls, &naddrttls)
1130+ if (!ParseGeneralReply (env, buf, len, &type, ret, addrttls. data () , &naddrttls)
11131131 .To (&status)) {
11141132 return Nothing<int >();
11151133 }
@@ -1182,11 +1200,13 @@ Maybe<int> AnyTraits::Parse(QueryAnyWrap* wrap,
11821200 }
11831201
11841202 /* Parse AAAA records */
1185- ares_addr6ttl addr6ttls[256 ];
1186- int naddr6ttls = arraysize (addr6ttls);
1203+ std::vector<ares_addr6ttl> addr6ttls =
1204+ MakeAddrTTLBuffer<ares_addr6ttl>(buf, len);
1205+ int naddr6ttls = static_cast <int >(addr6ttls.size ());
11871206
11881207 type = ns_t_aaaa;
1189- if (!ParseGeneralReply (env, buf, len, &type, ret, addr6ttls, &naddr6ttls)
1208+ if (!ParseGeneralReply (
1209+ env, buf, len, &type, ret, addr6ttls.data (), &naddr6ttls)
11901210 .To (&status)) {
11911211 return Nothing<int >();
11921212 }
@@ -1374,20 +1394,22 @@ Maybe<int> ATraits::Parse(QueryAWrap* wrap,
13741394 HandleScope handle_scope (env->isolate ());
13751395 Context::Scope context_scope (env->context ());
13761396
1377- ares_addrttl addrttls[256 ];
1378- int naddrttls = arraysize (addrttls), status;
1397+ std::vector<ares_addrttl> addrttls =
1398+ MakeAddrTTLBuffer<ares_addrttl>(buf, len);
1399+ int naddrttls = static_cast <int >(addrttls.size ()), status;
13791400 Local<Array> ret = Array::New (env->isolate ());
13801401
13811402 int type = ns_t_a;
1382- if (!ParseGeneralReply (env, buf, len, &type, ret, addrttls, &naddrttls)
1403+ if (!ParseGeneralReply (env, buf, len, &type, ret, addrttls. data () , &naddrttls)
13831404 .To (&status)) {
13841405 return Nothing<int >();
13851406 }
13861407 if (status != ARES_SUCCESS ) {
13871408 return Just<int >(status);
13881409 }
13891410
1390- Local<Array> ttls = AddrTTLToArray<ares_addrttl>(env, addrttls, naddrttls);
1411+ Local<Array> ttls =
1412+ AddrTTLToArray<ares_addrttl>(env, addrttls.data (), naddrttls);
13911413
13921414 wrap->CallOnComplete (ret, ttls);
13931415 return Just<int >(ARES_SUCCESS );
@@ -1406,20 +1428,22 @@ Maybe<int> AaaaTraits::Parse(QueryAaaaWrap* wrap,
14061428 HandleScope handle_scope (env->isolate ());
14071429 Context::Scope context_scope (env->context ());
14081430
1409- ares_addr6ttl addrttls[256 ];
1410- int naddrttls = arraysize (addrttls), status;
1431+ std::vector<ares_addr6ttl> addrttls =
1432+ MakeAddrTTLBuffer<ares_addr6ttl>(buf, len);
1433+ int naddrttls = static_cast <int >(addrttls.size ()), status;
14111434 Local<Array> ret = Array::New (env->isolate ());
14121435
14131436 int type = ns_t_aaaa;
1414- if (!ParseGeneralReply (env, buf, len, &type, ret, addrttls, &naddrttls)
1437+ if (!ParseGeneralReply (env, buf, len, &type, ret, addrttls. data () , &naddrttls)
14151438 .To (&status)) {
14161439 return Nothing<int >();
14171440 }
14181441 if (status != ARES_SUCCESS ) {
14191442 return Just<int >(status);
14201443 }
14211444
1422- Local<Array> ttls = AddrTTLToArray<ares_addr6ttl>(env, addrttls, naddrttls);
1445+ Local<Array> ttls =
1446+ AddrTTLToArray<ares_addr6ttl>(env, addrttls.data (), naddrttls);
14231447
14241448 wrap->CallOnComplete (ret, ttls);
14251449 return Just<int >(ARES_SUCCESS );
0 commit comments