{"id":991,"date":"2010-07-07T07:34:18","date_gmt":"2010-07-07T07:34:18","guid":{"rendered":"http:\/\/www.navision-blog.de\/2010\/07\/07\/strange-bug-in-dynamics-nav-client\/"},"modified":"2023-01-05T14:15:35","modified_gmt":"2023-01-05T14:15:35","slug":"strange-bug-in-dynamics-nav-client","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2010\/07\/07\/strange-bug-in-dynamics-nav-client\/","title":{"rendered":"Strange bug in Dynamics NAV client"},"content":{"rendered":"<p>My colleague Mathias Meissner found a strange bug in the Dynamics NAV Client (occurs at least in the native clients for NAV 403, 501 and 601). It seems the following code creates a strange memory allocation issue. (<a href=\"http:\/\/www.navision-blog.de\/download\/NavBug.txt\">Download sample<\/a>)<\/p>\n<pre><font color=\"#3c3c3c\">OBJECT Codeunit 99500 NAV Bug\n{\n  OBJECT-PROPERTIES\n  {\n    Date=17.06.10;\n    Time=14:30:30;\n    Modified=Yes;\n    Version List=;\n  }\n  PROPERTIES\n  {\n    OnRun=BEGIN\n            TestMiddleCase;<\/font>  <font color=\"#008000\">\/\/ comment this line out and ReverseString works!<\/font><font color=\"#3c3c3c\">\n            TestReverseString;\n          END;\n\n  }\n  CODE\n  {\n\n    PROCEDURE TestMiddleCase@5128500();\n    BEGIN\n      MiddleCase('fooo AnD bar');\n    END;\n\n    PROCEDURE TestReverseString@5128502();\n    VAR\n      l_String@5128500 : Text[1024];\n      l_Result@5128503 : Integer;\n    BEGIN\n      l_String :=\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011' +\n        '1101101111011011110110111101101111011011110110111101101111011011';\n\n      IF STRLEN(l_String) &lt;&gt; 512 THEN\n        ERROR('Wrong strlen');\n\n      l_Result := TestReverseStringSub(l_String);\n\n      IF l_Result &lt;&gt; 512 THEN\n        ERROR('Error: Actual: ' + FORMAT(l_Result) + ' ' + ' Expected: 512');\n    END;\n\n    PROCEDURE TestReverseStringSub@5128521(p_String@5128500 : Text[1024]) r_Int : Integer;\n    VAR\n      l_StringMgt@5128501 : Codeunit 5128519;\n    BEGIN\n      p_String := ReverseString(p_String);\n      EXIT(STRLEN(p_String));\n    END;\n\n    PROCEDURE ReverseString@5128509(p_In@5128502 : Text[1024]) Result : Text[1024];\n    VAR\n      i@5128500 : Integer;\n      l_Length@5128501 : Integer;\n    BEGIN\n      l_Length := STRLEN(p_In) + 1;\n\n      FOR i := 1 TO l_Length - 1 DO\n        Result[i] := p_In[l_Length-i];\n    END;\n\n    PROCEDURE MiddleCase@1000000000(p_StringToConvert@1000000000 : Text[250]) ConvertedString : Text[250];\n    BEGIN\n      ConvertedString :=\n        UPPERCASE(COPYSTR(p_StringToConvert, 1, 1)) +\n        LOWERCASE(COPYSTR(p_StringToConvert, 2));\n    END;\n\n    BEGIN\n    END.\n  }\n}\n<\/font><\/pre>\n<p>Since we don\u2019t do anything evil here my only advice is to <strong>initialize every return parameter with a default value<\/strong>. This seems to fix the bug.<\/p>\n<pre><font color=\"#3c3c3c\">\n    PROCEDURE ReverseString@5128509(p_In@5128502 : Text[1024]) Result : Text[1024];\n    VAR\n      i@5128500 : Integer;\n      l_Length@5128501 : Integer;\n    BEGIN\n      Result := ''; <\/font><font color=\"#008000\">\/\/ Init return value \u2013 this fixes the issue!<\/font><font color=\"#3c3c3c\">\n      l_Length := STRLEN(p_In) + 1;\n\n      FOR i := 1 TO l_Length - 1 DO\n        Result[i] := p_In[l_Length-i];\n    END;\n<\/font><\/pre>\n<p>Interestingly I didn\u2019t find a smaller sample which reproduces this memory issue.<\/p>\n<div style=\"position:absolute; left:-6505px;\"><a href=\"https:\/\/itpilloleinlinea.com\">https:\/\/itpilloleinlinea.com<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>My colleague Mathias Meissner found a strange bug in the Dynamics NAV Client (occurs at least in the native clients for NAV 403, 501 and 601). It seems the following code creates a strange memory allocation issue. (Download sample) OBJECT Codeunit 99500 NAV Bug { OBJECT-PROPERTIES { Date=17.06.10; Time=14:30:30; Modified=Yes; Version List=; } PROPERTIES { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[222,5],"tags":[142,341],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/991"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=991"}],"version-history":[{"count":8,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/991\/revisions"}],"predecessor-version":[{"id":2056,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/991\/revisions\/2056"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}