Skip to content

Commit

Permalink
METAR parsing infinite loop bugfix #599 (#600)
Browse files Browse the repository at this point in the history
METAR parsing infinite loop bugfix #599
  • Loading branch information
nabeelio committed Feb 29, 2020
1 parent b0f122a commit ea9ee98
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
24 changes: 21 additions & 3 deletions app/Services/Metar/AviationWeather.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Contracts\Metar;
use App\Support\HttpClient;
use function count;
use Exception;
use Illuminate\Support\Facades\Log;

/**
Expand Down Expand Up @@ -44,15 +46,31 @@ protected function metar($icao): string
try {
$res = $this->httpClient->get($url, []);
$xml = simplexml_load_string($res);
if (\count($xml->data->METAR->raw_text) === 0) {

$attrs = $xml->data->attributes();
if (!isset($attrs['num_results'])) {
return '';
}

$num_results = $attrs['num_results'];
if (empty($num_results)) {
return '';
}

$num_results = (int) $num_results;
if ($num_results === 0) {
return '';
}

if (count($xml->data->METAR->raw_text) === 0) {
return '';
}

return $xml->data->METAR->raw_text->__toString();
} catch (\Exception $e) {
} catch (Exception $e) {
Log::error('Error reading METAR: '.$e->getMessage());

throw $e;
return '';
}
}
}
1 change: 1 addition & 0 deletions app/Widgets/Weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Weather extends Widget
*/
public function run()
{
/** @var \App\Services\AirportService $airportSvc */
$airportSvc = app(AirportService::class);
$metar = $airportSvc->getMetar($this->config['icao']);

Expand Down
11 changes: 11 additions & 0 deletions tests/MetarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,15 @@ public function testHttpCallEmpty()

$this->assertNull($airportSvc->getMetar('idk'));
}

public function testHttpCallUnknown()
{
$this->mockXmlResponse('aviationweather/unknown.xml');

/** @var AirportService $airportSvc */
$airportSvc = app(AirportService::class);

$metar = $airportSvc->getMetar('7AK4');
$this->assertNull($metar);
}
}
Empty file.

0 comments on commit ea9ee98

Please sign in to comment.