Real Madrid warmed up for Wednesday's Champions League game against Juventus with victory in La Liga when Cristiano Ronaldo's stoppage-time penalty set the seal on a 2-0 win at home to Malaga on Saturday.
It was hardly the sparkling performance coach Carlo Ancelotti wanted to dispel doubts about Real's stuttering start to their domestic campaign but was enough to trim the gap with leaders Barcelona and Atletico Madrid to two points.
Champions Barca and King's Cup holders Atletico have both won all eight of their La Liga outings and play at Osasuna and Espanyol respectively later on Saturday.
The visitors did well to keep Real at bay until halftime but fell behind in the first minute of the second half when Angel Di Maria whipped the ball over from the right and it evaded Malaga goalkeeper Willy Caballero's despairing dive before nestling in the back of the net.
Caballero was in inspired form and pulled off a brilliant stop to deny Ronaldo eight minutes from time and made another stunning save to palm away a low shot from substitute Jese two minutes later.
Real's record signing Gareth Bale, who has been out for several weeks with a thigh strain, came on for Alvaro Morata with some 15 minutes left, suggesting he could play a part against the Italian champions on Wednesday.
Bale won a late penalty when the referee ruled he was fouled by Weligton in the area and Ronaldo dispatched the spot kick low to Caballero's right for his eighth La Liga goal of the season.
Real have 22 points from nine matches, while Barca and Atletico can equal the best start in La Liga history if they maintain their perfect records.