Pakistan 185 (Sarfraz 50, Olivier 5-51) and 153 for 3 (Shafiq 48*, Babar 17*) need another 228 to beat South Africa 262 and 303 (de Kock 129, Amla 71)
South Africa moved ever closer to a 3-0 clean sweep on the third day at Johannesburg, as Dale Steyn and Duanne Olivier bent their backs in the face of some stiff resistance to leave Pakistan's second innings in the balance at 153 for 3, and ensure that Quinton de Kock's superb fourth Test century is unlikely to go to waste.
Despite a doughty first-wicket stand of 67 between Shan Masood and Imam-ul-Haq, and another pair of combative knocks from Babar Azam and, especially, Asad Shafiq, Pakistan were firmly up against it at the close.
The loss of both openers in the space of three Steyn overs set the scene for another critical failure for Azhar Ali, their under-performing senior batsman, who completed a dismal series haul of 59 runs at 9.83 when he was bounced out for the fourth time in six innings by Olivier.
It was Olivier's 22nd wicket of the series, more than even Steyn has ever managed for South Africa in a three-match series, and it had to be hard-earned on a surface that appeared to lack the demons that had been in evidence while de Kock and Hashim Amla, in particular, were eking out their team's lead in the early part of the day.
In the absence once again of a frontline spinner to mix up South Africa's options, it took invention and nous to force their openings in the evening session - not least from Steyn, who had left the field clutching his shoulder in the first innings, but shook off any lingering doubts about his fitness with another bravura performance.
After failing to cause many problems to Pakistan's left-handers in a three-over foray before tea, Steyn returned from round the wicket to test their mettle with a wide line angled into the stumps. Imam, on 35, was first to blink, drawn into playing the original line and nicking off as Steyn straightened one off the pitch. And 14 Steyn deliveries later, Masood departed in fury to a similar ball, seemingly oblivious to the feathered inside-edge onto thigh-pad that condemned him on review.
For the second match running, however, Pakistan's batsmen had saved their best for last, and as Babar and Shafiq reprised the watchful shot selection that had given their hosts plenty to ponder in Cape Town, it was South Africa who were the most grateful to take refuge in the dressing room. A night's sleep, however, will remind both teams of the distance this chase still has to run. A fourth-day target of 228 remains more than Pakistan have managed in four of their five completed innings on this tour.
All of which made de Kock's earlier efforts with the bat all the more crucial. By the time South Africa's final wicket fell, 13 balls after de Kock had launched Shadab Khan once too often into the leg side to fall for an equal Test-best 129, their batsmen had completed a fine recovery from a wobbly 93 for 5 to a hefty total of 303 all out.
It is an indication of de Kock's supreme talent that, despite rattling along at a typically brisk strike-rate, this was one of the more circumspect performances of his burgeoning Test career. He had resumed his innings on 34 not out overnight, having barrelled along at a run a ball on the second evening, but slipped back into a more measured pace in the first session to combat a Pakistan attack that never lost heart despite their mounting challenge, but struggled to force the breakthroughs.
They managed two in the morning session: Amla, who had pressed along to 71 - five shy of Jacques Kallis' long-term record for most runs at the Wanderers - before Hasan Ali got one to pop into his gloves from a good length to end a sixth-wicket stand of 102; and Vernon Philander, who picked off three sweet boundaries in a well-compiled 14, before Mohammad Amir pinned him on the shin with a perfectly directed yorker.
De Kock stood firm throughout, picking off four boundaries before the break before stepping up an extra gear in the hour after lunch to move to the brink of his hundred.
But then he endured a disarmingly jittery spell - he had fallen twice in the 90s in consecutive Tests against New Zealand in March 2017 and not come so close to a hundred since. And so when, on 99, a clobbered straight drive off Faheem Ashraf deflected off Kagiso Rabada's boot at the non-striker's end and straight to the man at mid-on, de Kock had reason to fear his luck might not be in.
Another nervy declined single and a fierce smack to short cover ramped up the angst even more, but de Kock kept flinging the bat, and eventually connected with a cathartic drill through long-off to cue a release of pent-up emotion.
Sarfraz Ahmed, who would finish with a Pakistan-record ten dismissals for the match, missed a sharp fizzing edge soon afterwards, high to his left off the persevering Hasan, whereupon de Kock launched into a predictable fusillade of strokeplay.
He scorched his very next ball wide of mid-off before clearing his front leg in Hasan's next over and belting a lofted flog straight back over his head for another four. That massive six off Shadab then followed, before de Kock chanced his arm against the legspinner once too often. He picked out Hasan just inside the rope to depart to a deserved standing ovation.
The end of the innings followed quickly. Rabada had been a willing accomplice in a 79-run stand for the eighth wicket, but soon spliced a Faheem Ashraf bouncer to cover as he sought to up the ante. Duanne Olivier then nicked off to give Shadab his third wicket in an under-used role, and set the stage for Pakistan's response.