Article 6G0E7 CodeSOD: Legacy Horrors

CodeSOD: Legacy Horrors

by
Remy Porter
from The Daily WTF on (#6G0E7)

Today is Halloween, a day filled with chills, horrors, and Jamie Lee Curtis. An interesting aspect of horror movies is how often the roots of the horror lurk in the past. Michael Meyers had been in an asylum for decades before his infamous Halloween rampage. Midsommar represents a centuries old tradition. Barbarian is rooted in sins committed a generation prior. Freddy Krueger was the manifestation of the sins of our protagonists' parents. Hell, even Dracula is a menace that had been lurking for centuries before our story begins.

In honor of that, we're going to look at some code from Davide. Like so much classic horror, the seeds of this sin were planted many, many years before Davide arrived.

In 1991, Microsoft released their first version of Visual Basic. The language evolved until 1998, with the release of VB6. Mainstream support ended in 2005, extended support ended in 2008, but like true horror, VB6 has not truly died. The development tools continue to run on all 32-bit versions of Windows.

Back in those olden days, Davide's predecessors decided to implement an Enterprise Resource Planning system in VB6. It grew, and evolved, and became something that couldn't be controlled anymore- it was 2.5M lines of code. It entangled itself into the company, taking over every core business function, and rapidly becoming indispensible.

We can compare it to so many monsters of horror- the shapeshifting Thing, gradually replacing parts of the company with itself. The fungus from The Last of Us, taking over the brain of the company. We can compare it to The Blob, which may also be the most accurate description of the coding practices used in building it.

Here's some code that evaluates a formula entered by the users:

Option ExplicitDim AA$(26)Dim VV#(26) 'valori delle variabiliDim B$(26)Dim OP$(26)Dim RANGO(26) As IntegerDim P$(27)Dim OPN#(2), COPN$(26)Dim CV%Public scrCtl As MSScriptControl.ScriptControlDim bCreated As Boolean ' gi creato lo script controlDim strUltCodart As String ' ultimo cod. articolo (per seganlazione su che articolo c' una formula sbagliata)Dim strUltFormula As StringDim nEsito As IntegerPublic Function FormulaControlla(strFormula As String) As Boolean17000 Rem --- Input = FORMULA$, Output = AA$(__),CV%,OK%=1 se FORMULA$ non buona Dim Formula$, f$, CP%, CN%, FLAGP%, c$, OK%, LX%, s%, A$, B$, AB%, BB%, BRANGO%, CRANGO$ FormulaControlla = True '--- con l'introd. di msscript non controlla pi niente .... Exit Function '------------------------- Formula$ = strFormula17010 f$ = Formula$ + Chr$(0): CP% = 0: CN% = 0: CV% = 0: FLAGP% = 0: c$ = "": OK% = 017020 LX% = Len(Formula$)17030 For s% = 1 To LX%17040 A$ = Mid$(f$, s%, 1): B$ = Mid$(f$, s% + 1, 1)17050 AB% = Asc(A$): BB% = Asc(B$)17060 If A$ >= "0" And A$ <= "9" Then If (B$ >= "a" And B$ <= "z") Or B$ = "(" Then OK% = 1: GoTo 17230 Else CN% = CN% + 1: GoTo 1723017070 If A$ = "*" Or A$ = "/" Or A$ = "+" Or A$ = "-" Then If B$ = "*" Or B$ = "/" Or B$ = "+" Or B$ = "-" Or B$ = Chr$(0) Or B$ = ")" Then OK% = 1: GoTo 17230 Else GoTo 1713017080 If A$ = "(" Then If B$ = ")" Or B$ = "*" Or B$ = "/" Or B$ = "+" Or B$ = Chr$(0) Then OK% = 1: GoTo 17230 Else CP% = CP% + 1: GoTo 1713017090 If A$ = ")" Then If B$ = "(" Or (B$ >= "0" And B$ <= "9") Or (B$ >= "a" And B$ <= "z") Or B$ = "." Then OK% = 1: GoTo 17230 Else CP% = CP% - 1: GoTo 1713017100 If A$ >= "a" And A$ <= "z" Then If (B$ >= "0" And B$ <= "9") Or (B$ >= "a" And B$ <= "z") Or B$ = "(" Or B$ = "." Then OK% = 1: GoTo 17230 Else GoTo 1713017110 If A$ = "." Then If Not (B$ >= "0" And B$ <= "9") Then OK% = 1: GoTo 17230 Else CN% = CN% + 1: FLAGP% = FLAGP% + 1: GoTo 1723017120 OK% = 1: GoTo 1723017130 '---- crea AA$ (__)17140 If CN% = 0 Then GoTo 1718017150 If FLAGP% > 1 Then OK% = 1: GoTo 1723017160 CV% = CV% + 1: AA$(CV%) = Mid$(f$, s% - CN%, CN%)17170 CN% = 0: FLAGP% = 017180 CV% = CV% + 1: AA$(CV%) = A$17190 If A$ = "*" Or A$ = "/" Or A$ = "+" Or A$ = "-" Then GoTo 17200 Else GoTo 1723017200 If A$ = "-" And c$ = "(" Or A$ = "-" And s% = 1 Then BRANGO% = 3 + 3 * CP%: CRANGO$ = Right$(" " + str$(BRANGO%), 2): AA$(CV%) = ">" + CRANGO$: GoTo 1723017210 If A$ = "-" Or A$ = "+" Then BRANGO% = 1 + 3 * CP%: CRANGO$ = Right$(" " + str$(BRANGO%), 2): AA$(CV%) = AA$(CV%) + CRANGO$17220 If A$ = "*" Or A$ = "/" Then BRANGO% = 2 + 3 * CP%: CRANGO$ = Right$(" " + str$(BRANGO%), 2): AA$(CV%) = AA$(CV%) + CRANGO$17230 '---- ultima fase17240 If OK% = 1 Then s% = LX%17250 c$ = A$17260 Next s%17270 If CN% = 0 Then GoTo 1731017280 If FLAGP% > 1 Then OK% = 1: GoTo 1731017290 CV% = CV% + 1: AA$(CV%) = Mid$(f$, s% - CN%, CN%)17300 CN% = 0: FLAGP% = 017310 If CN% > 0 Or FLAGP% <> 0 Or CP% <> 0 Then OK% = 117320 If OK% = 1 Then FormulaControlla = False Else FormulaControlla = TrueEnd FunctionPublic Sub FormulaGenPolacca()18800 Rem --- ROUTINE che, data la NOTAZIONE INTERA18810 Rem --- GENERA LA NOTAZIONE POLACCA18820 Rem --- Input = AA$(__), CV%, Output = P$(__) Dim i As Integer, j As Integer, K As Integer, CONTA As Integer, MAXI%, l As Integer Dim NUOVA%, KK%, M As Integer18830 i = 0: j = 1: l = 0: K = 1: CONTA = 0: MAXI% = CV%18835 If NUOVA% = 1 Then NUOVA% = 0: For KK% = 1 To 26: VV#(KK%) = 0: Next KK%18840 For KK% = 1 To 26: B$(KK%) = "": OP$(KK%) = "": RANGO(KK%) = 0: P$(KK%) = "": Next KK%: P$(26) = ""18850 Rem18860 If CONTA > 0 Then CONTA = CONTA + 118870 i = i + 118880 If AA$(i) = "(" Or AA$(i) = ")" Then If i = MAXI% Then l = l + 1: OP$(l) = "T": RANGO(l) = 0: GoTo 18910 Else GoTo 1887018890 If AA$(i) >= "a" And AA$(i) <= "z" Or Asc(AA$(i)) = 46 Or Asc(AA$(i)) > 47 And Asc(AA$(i)) < 58 Then If i = MAXI% Then B$(j) = AA$(i): l = l + 1: OP$(l) = "T": RANGO(l) = 0: GoTo 18910 Else B$(j) = AA$(i): GoTo 1887018900 l = l + 1: OP$(l) = Left$(AA$(i), 1): RANGO(l) = Val(Right$(AA$(i), 2))18910 If B$(j) <> "" Then P$(K) = B$(j): K = K + 1: j = j + 118920 If l = 1 And i = MAXI% Then GoTo 1902018930 If l = 1 Then GoTo 1885018940 If RANGO(l) > RANGO(l - 1) Then CONTA = CONTA + 1: GoTo 1885018950 If CONTA = 0 Then P$(K) = OP$(l - 1): K = K + 1: RANGO(l - 1) = 0: GoTo 1902018960 For M = 1 To CONTA + 118970 If M = l Then M = CONTA + 1: GoTo 1901018980 If RANGO(l - M) = 0 Then GoTo 1901018990 If RANGO(l) > RANGO(l - M) Then GoTo 1901019000 P$(K) = OP$(l - M): K = K + 1: RANGO(l - M) = 019010 Next M19020 If OP$(l) = "T" Then P$(K) = OP$(l) Else GoTo 1885019030 'End SubPublic Function FormulaValutaPolacca() As Double19500 Rem --- ROUTINE che, data la NOTAZIONE POLACCA19510 Rem --- CALCOLA il VALORE DELLA FORMULA19515 Rem --- Input = P$(__), Output = RISULTATO#,ERRORE%=1 se Division by zero Dim K As Integer, ERRORE%, OPR$, Q%, NO%, R As Integer, ABC$, RIS#, Risultato#, X$19520 FormulaValutaPolacca = 0: K = 0: ERRORE% = 019530 K = K + 119540 If P$(K) >= "a" And P$(K) <= "z" Or Asc(P$(K)) = 46 Or Asc(P$(K)) > 47 And Asc(P$(K)) < 58 Then GoTo 1953019550 OPR$ = P$(K): P$(K) = "": Q% = 1: While P$(K - Q%) = "": Q% = Q% + 1: Wend: COPN$(2) = P$(K - Q%): P$(K - Q%) = ""19560 If OPR$ <> ">" And OPR$ <> "T" Then NO% = 1: Q% = 2: While P$(K - Q%) = "": Q% = Q% + 1: Wend: COPN$(1) = P$(K - Q%): P$(K - Q%) = "" Else NO% = 219570 Rem-----19580 For R = 2 To NO% Step -119590 If Not (COPN$(R) >= "a" And COPN$(R) <= "z") Then OPN#(R) = Val(COPN$(R)): GoTo 1961019600 ABC$ = COPN$(R): OPN#(R) = FormulaAssegna(ABC$)19610 Next R19620 Rem-----19630 Rem-----19640 If OPR$ = "T" Then RIS# = OPN#(2): GoTo 1971019650 If OPR$ = ">" Then RIS# = -OPN#(2): GoTo 1970019660 If OPR$ = "*" Or OPR$ = "x" Then RIS# = OPN#(1) * OPN#(2): GoTo 1970019670 If OPR$ = ":" Or OPR$ = "/" Then If OPN#(2) <> 0 Then RIS# = OPN#(1) / OPN#(2): GoTo 19700 Else Risultato# = 0: ERRORE% = 1: GoTo 1973019680 If OPR$ = "+" Then RIS# = OPN#(1) + OPN#(2): GoTo 1970019690 If OPR$ = "-" Then RIS# = OPN#(1) - OPN#(2)19700 Rem-------19710 If OPR$ = "T" Then GoTo 19720 Else X$ = str$(RIS#): P$(K) = Right$(X$, Len(X$) - 1): GoTo 1953019720 Risultato# = RIS#19730 FormulaValutaPolacca = Risultato#:19740 If ERRORE = 1 Then MsgBox "Attenzione: Divisione per 0 nella valutazione della formula.", vbCritical, bsTtlErrEnd FunctionPublic Function FormulaAssegna(ABC$) As Double19800 Rem --- assegna i valori alle variabili operatori -------19801 Rem --- input ABC$ , globale vv#() output VV# ------- Dim NN%, BVV#19810 NN% = Asc(ABC$) - 96 If NN% = 1 Or NN% = 2 Or NN% = 3 Or NN% = 11 Or NN% = 14 Then BVV# = VV#(NN%): FormulaAssegna = BVV#: GoTo Fine End If19830 If VV#(NN%) <> 0 Then BVV# = VV#(NN%): FormulaAssegna = BVV#: GoTo Fine End If19860 BVV# = CDbl(InputBox("Inserire il valore della variabile <" + ABC$ + "> =", "Richiesta valore parametro", "0"))19880 VV#(NN%) = BVV#19900 FormulaAssegna = BVV#Fine:End FunctionPublic Sub FormulaChiama(strFormula As String) Dim bOk As Boolean Dim Risultato#30200 Rem --- CALCOLO QUANTITA' (da FORMULA$)30205 '30210 bOk = FormulaControlla(strFormula): If bOk = False Then Risultato# = 0 Else FormulaGenPolacca: Risultato# = FormulaValutaPolaccaEnd Sub

The only glimmer of hope is the first line- at least they used Option Explicit, which makes variable declarations required (instead of just creating a variable anytime you use one). Everything else is horror. VB6 did not require line numbers, which implies that either the developer responsible cut their teeth in older versions of BASIC, or worse: this code started its life in an older version of basic. Or, possibly even worse: the developer couldn't imagine flow control without GoTo, because there are a lot of them.

While this code was clearly originally written in Italian, the fact that it's in another (natural) language isn't what makes the code impossible for me to understand. The code itself is cryptic and complicated enough that I don't want to decipher it, for fear that solving this puzzle invites Cenobites to tear me apart.

Davide adds: "The woeful part of the story is that it actually works most of the time."

True horrors lurk in this world, nightmares that we can't hope to escape, legacy code that needs to be maintained, and lies in wait for vulnerable developers. Once ensnared in the grasp of this monster, can any developer truly hope to escape?

And finally, for a bonus story of technological horrors, I told a creepy campfire tale about Thomas Midgley, Jr. on stage this weekend. Pardon the cellphone video. The Man Who Sold the World.

otter-icon.png [Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments