while (sysctl(routeDumpName, 6, buffer, &byteCount, NULL, 0) != 0)
{
+ if (errno != ENOMEM)
+ {
+ return -1;
+ }
+
+ // If buffer is not big enough double size to avoid calling sysctl again
+ // as byteCount only gets estimated size when passed buffer is NULL.
+ // This only happens if routing table grows between first and second call.
+ size_t tmpEstimatedSize;
+ if (!multiply_s(byteCount, (size_t)2, &tmpEstimatedSize))
+ {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ byteCount = tmpEstimatedSize;
buffer = realloc(buffer, byteCount);
if (buffer == NULL)
{