Knowledge (XXG)

Off-by-one error

Source ๐Ÿ“

512: 132: 32: 73: 590:
if posts do not occur at the ends of a line-segment-like fence (e.g., a fence between and wall-anchored to two buildings). The precise problem definition must be carefully considered, as the setup for one situation may give the wrong answer for other situations. Fencepost errors come from counting
624:
In larger numbers, being off by one is often not a major issue. In smaller numbers, however, and specific cases where accuracy is paramount, committing an off-by-one error can be disastrous. Sometimes such an issue will also be repeated and, therefore, worsened, by someone passing on an incorrect
556:
sections. This relationship is important to consider when dealing with the reverse error. The reverse error occurs when the number of posts is known and the number of sections is assumed to be the same. Depending on the design of the fence, this assumption can be correct or incorrect.
530:
The common answer of 10 posts is wrong. This response comes from dividing the length of the fence by the spacing apart from each post, with the quotient being erroneously classified as the number of posts. In actuality, the fence has 10 sections and 11 posts.
598:, consisting of 120 blocks placed at 10-year intervals between blocks, is scheduled to take 1,190 years to build (not 1,200), from the installation of the first block to the last block. One of the earliest fencepost errors involved time, where the 847:
One approach that often helps avoid such problems is to use variants of these functions that calculate how much to write based on the total length of the buffer, rather than the maximum number of characters to write. Such functions include
286:
iterates one time too many or too few, usually caused by the use of non-strict inequality (โ‰ค) as the terminating condition where strict inequality (<) should have been used, or vice versa. Off-by-one errors also stem from confusion over
706:
is that the guaranteed null termination will not write beyond the maximum length. In reality it will write a terminating null character one byte beyond the maximum length specified. The following code contains such a bug:
609:"Fencepost error" can, in rare occasions, refer to an error induced by unexpected regularities in input values, which can (for instance) completely thwart a theoretically efficient 468:
Array-related confusion may also result from differences in programming languages. Numbering from 0 is most common, but some languages start array numbering with 1.
860:, and are often considered "safer" because they make it easier to avoid accidentally writing past the end of a buffer. (In the code example above, calling 914: 813:
Off-by-one errors are common in using the C library because it is not consistent with respect to whether one needs to subtract 1 byte โ€“ functions like
591:
things rather than the spaces between them, or vice versa, or by neglecting to consider whether one should count one or both ends of a row.
442:
is false and the loop ends. However, if the comparison used were <= (less than or equal to), the loop would be carried out six times:
1063: 1039: 233: 215: 149: 113: 59: 45: 579:
if the fence is a free-standing line segment bounded by a post at each of its ends (e.g., a fence between two passageway gaps),
196: 168: 153: 572:
The interpretation for the fence's design changes the answer to this problem. The correct number of sections for a fence is
583:
if the fence forms one complete, free-standing loop (e.g., enclosure accessible by surmounting, such as a boxing ring), or
998: 469: 625:
calculation, if the following person makes the same kind of mistake again (of course, the error might also be reversed).
175: 274:
that involves a number that differs from its intended value by +1 or โˆ’1. An off-by-one error can sometimes appear in a
1012: 833:
will write past the length given them. So the programmer has to remember for which functions they need to subtract 1.
606:, due to counting inclusively rather than exclusively, yielding a leap year every three years rather than every four. 981: 844:. This can cause an exploitable condition where an attacker can hijack the local variables for the calling routine. 91: 20: 472:
has arrays with user-defined indices. This makes it possible to model the array indices after the problem domain.
182: 142: 164: 924: 873: 1058: 371:
that iterates five times (from 0 to 4 inclusive) can be written as a half-open interval from 0 to 5:
83: 990: 95: 51: 888: 878: 840:
architectures in particular) this can result in the overwriting of the least significant byte of the
671:. A programmer who misunderstands the third parameter to be the number of increments might hope that 636: 495:) is a specific type of off-by-one error. An early description of this error appears in the works of 288: 279: 617:
implementation. This error involves the difference between expected and worst case behaviours of an
920: 694: 345: 1035: 946: 189: 693:
A common off-by-one error which results in a security-related bug is caused by misuse of the
603: 599: 305: 454:
takes the values 1, 2, 3, and 4. Both of these alternatives can cause off-by-one errors.
883: 316:(inclusive) are to be processed. How many items are there? An intuitive answer may be 1052: 841: 614: 458: 595: 368: 283: 1016: 511: 610: 275: 271: 131: 837: 462: 594:
Fencepost errors can also occur in units other than length. For example, the
434:
then becomes 1, 2, 3, and finally 4 on successive iterations. At that point,
618: 496: 450:
were initialized to 1 rather than 0, there would only be four iterations:
507:
30 feet long with posts spaced 3 feet apart, how many posts do you need?
856: 850: 698: 973: 629: 515:
A straight fence with 10 sections requires 11 posts. More generally,
465:(or vice versa.) A do-while loop is guaranteed to run at least once. 510: 504: 628:
An example of this error can occur in the computational language
344:
For this reason, ranges in computing are often represented by
125: 66: 25: 19:"OBOB" redirects here. For the book trivia competition, see 560:
The following problem demonstrates the reverse error:
367:(exclusive) to avoid fencepost errors. For example, a 977: 446:
takes the values 0, 1, 2, 3, 4, and 5. Likewise, if
16:
Logical error that can often be found in programming
156:. Unsourced material may be challenged and removed. 1032:Humble Pi: When Math Goes Wrong in the Real World 972:An earlier version of this article was based on 568:posts, how many sections are there between them? 562: 501: 499:. The following problem illustrates the error: 991:"Why numbering should start at zero (EWD 831)" 821:will never write past the length given them ( 356:(inclusive) is represented by the range from 8: 426:The loop body is executed first of all with 94:. There might be a discussion about this on 916:Who first described the "fence-post error?" 805:// Final parameter should be: sizeof(buf)-1 60:Learn how and when to remove these messages 545:posts. Conversely, if the fence contains 234:Learn how and when to remove this message 216:Learn how and when to remove this message 114:Learn how and when to remove this message 905: 825:subtracts 1 itself, and only retrieves 326:, but that is off by one, exhibiting a 702:routine. A common misconception with 989:Dijkstra, Edsger Wybe (May 2, 2008). 7: 327: 154:adding citations to reliable sources 457:Another such error can occur if a 14: 604:calculated leap years incorrectly 41:This article has multiple issues. 130: 71: 30: 1015:system this issue is listed as 864:instead would remove the bug.) 639:function, whose parameters are 534:In this scenario, a fence with 141:needs additional citations for 49:or discuss these issues on the 1: 999:University of Texas at Austin 829:bytes), whereas others, like 862:strlcat(buf, s, sizeof(buf)) 278:context. It often occurs in 1013:Common Weakness Enumeration 1082: 1064:Computer security exploits 675:would achieve a sequence 21:Oregon Battle of the Books 18: 1017:CWE-193: Off-by-one Error 709: 503:If you build a straight 373: 330:; the correct answer is 874:Boundary-value analysis 549:posts, it will contain 519:sections would require 484:(occasionally called a 995:E. W. Dijkstra Archive 680:but instead would get 570: 527: 509: 461:is used in place of a 418:/* Body of the loop */ 1030:Parker, Matt (2021). 689:Security implications 514: 889:Zero-based numbering 879:Pigeonhole principle 667:number of increments 637:linear interpolation 308:of items, and items 289:zero-based numbering 280:computer programming 150:improve this article 84:confusing or unclear 1034:. Riverhead Books. 913:Moniot, Robert K., 538:sections will have 346:half-open intervals 300:Looping over arrays 254:(known by acronyms 92:clarify the article 921:Fordham University 695:C standard library 528: 493:picket fence error 165:"Off-by-one error" 947:"The Jargon File" 836:On some systems ( 348:; the range from 244: 243: 236: 226: 225: 218: 200: 124: 123: 116: 64: 1071: 1045: 1008: 1006: 1005: 958: 957: 955: 953: 942: 936: 934: 933: 932: 923:, archived from 910: 863: 859: 853: 832: 828: 824: 820: 816: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 716: 713: 705: 701: 684: 683: 679: 678: 674: 673:linspace(0,10,5) 670: 654: 651:number of values 635: 589: 578: 555: 544: 525: 453: 449: 445: 441: 437: 433: 429: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 366: 340: 325: 248:off-by-one error 239: 232: 221: 214: 210: 207: 201: 199: 158: 134: 126: 119: 112: 108: 105: 99: 75: 74: 67: 56: 34: 33: 26: 1081: 1080: 1074: 1073: 1072: 1070: 1069: 1068: 1049: 1048: 1042: 1029: 1026: 1024:Further reading 1021: 1003: 1001: 988: 974:fencepost error 967: 962: 961: 951: 949: 945:Raymond, Eric. 944: 943: 939: 930: 928: 912: 911: 907: 902: 897: 870: 861: 855: 849: 830: 826: 822: 818: 814: 811: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 711: 703: 697: 691: 682: 681: 677: 676: 672: 656: 640: 633: 600:Julian calendar 584: 573: 550: 539: 520: 486:telegraph pole, 482:fencepost error 478: 476:Fencepost error 451: 447: 443: 439: 435: 431: 427: 424: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 361: 360:(inclusive) to 331: 328:fencepost error 317: 302: 297: 240: 229: 228: 227: 222: 211: 205: 202: 159: 157: 147: 135: 120: 109: 103: 100: 89: 76: 72: 35: 31: 24: 17: 12: 11: 5: 1079: 1078: 1075: 1067: 1066: 1061: 1051: 1050: 1047: 1046: 1041:978-0593084694 1040: 1025: 1022: 1020: 1019: 1009: 986: 968: 966: 963: 960: 959: 937: 904: 903: 901: 898: 896: 893: 892: 891: 886: 884:Rounding error 881: 876: 869: 866: 710: 690: 687: 477: 474: 438:becomes 5, so 374: 301: 298: 296: 293: 252:off-by-one bug 242: 241: 224: 223: 138: 136: 129: 122: 121: 79: 77: 70: 65: 39: 38: 36: 29: 15: 13: 10: 9: 6: 4: 3: 2: 1077: 1076: 1065: 1062: 1060: 1059:Software bugs 1057: 1056: 1054: 1043: 1037: 1033: 1028: 1027: 1023: 1018: 1014: 1010: 1000: 996: 992: 987: 985: 983: 979: 975: 970: 969: 964: 948: 941: 938: 927:on 2016-03-05 926: 922: 918: 917: 909: 906: 899: 894: 890: 887: 885: 882: 880: 877: 875: 872: 871: 867: 865: 858: 852: 845: 843: 842:frame pointer 839: 838:little endian 834: 708: 700: 696: 688: 686: 668: 664: 660: 652: 648: 644: 638: 631: 626: 622: 620: 616: 615:hash function 612: 607: 605: 601: 597: 592: 587: 582: 576: 569: 567: 561: 558: 553: 548: 542: 537: 532: 523: 518: 513: 508: 506: 500: 498: 494: 490: 487: 483: 475: 473: 471: 466: 464: 460: 459:do-while loop 455: 372: 370: 364: 359: 355: 351: 347: 342: 338: 334: 329: 324: 320: 315: 311: 307: 299: 294: 292: 290: 285: 281: 277: 273: 269: 265: 261: 257: 253: 249: 238: 235: 220: 217: 209: 198: 195: 191: 188: 184: 181: 177: 174: 170: 167: โ€“  166: 162: 161:Find sources: 155: 151: 145: 144: 139:This article 137: 133: 128: 127: 118: 115: 107: 97: 96:the talk page 93: 87: 85: 80:This article 78: 69: 68: 63: 61: 54: 53: 48: 47: 42: 37: 28: 27: 22: 1031: 1002:. Retrieved 994: 980:, used with 971: 950:. Retrieved 940: 929:, retrieved 925:the original 915: 908: 846: 835: 827:(length โˆ’ 1) 812: 692: 666: 662: 658: 650: 646: 642: 627: 623: 608: 596:Time Pyramid 593: 585: 580: 574: 571: 565: 564:If you have 563: 559: 551: 546: 540: 535: 533: 529: 521: 516: 502: 492: 488: 485: 481: 479: 467: 456: 440:index < 5 430:equal to 0; 425: 362: 357: 353: 349: 343: 336: 332: 322: 318: 313: 309: 304:Consider an 303: 276:mathematical 267: 263: 259: 255: 251: 247: 245: 230: 212: 203: 193: 186: 179: 172: 160: 148:Please help 143:verification 140: 110: 101: 90:Please help 81: 57: 50: 44: 43:Please help 40: 663:upper value 659:lower value 647:upper value 643:lower value 611:binary tree 602:originally 272:logic error 1053:Categories 1004:2011-03-16 982:permission 931:2016-07-07 895:References 634:linspace() 489:lamp-post, 463:while loop 176:newspapers 86:to readers 46:improve it 900:Citations 632:with the 619:algorithm 497:Vitruvius 206:July 2020 52:talk page 868:See also 655:and not 312:through 104:May 2022 1011:In the 965:Sources 857:strlcpy 851:strlcat 831:strncat 823:fgets() 819:strncpy 815:fgets() 775:strncat 704:strncat 699:strncat 282:when a 270:) is a 190:scholar 82:may be 1038:  978:FOLDOC 952:17 May 793:sizeof 763:sizeof 745:memset 630:MATLAB 526:posts. 470:Pascal 192:  185:  178:  171:  163:  505:fence 452:index 448:index 444:index 436:index 432:index 428:index 406:index 394:index 382:index 306:array 295:Cases 197:JSTOR 183:books 1036:ISBN 954:2021 854:and 817:and 736:char 721:char 712:void 397:< 369:loop 284:loop 268:OBOB 266:and 256:OBOE 169:news 976:at 802:)); 799:buf 781:buf 772:)); 769:buf 751:buf 739:buf 715:foo 613:or 588:+ 1 577:โˆ’ 1 554:โˆ’ 1 543:+ 1 524:+ 1 491:or 376:for 365:+ 1 352:to 339:+ 1 264:OB1 260:OBO 250:or 246:An 152:by 1055:: 997:. 993:. 919:, 685:. 665:, 661:, 649:, 645:, 621:. 480:A 409:++ 341:. 335:โˆ’ 321:โˆ’ 291:. 262:, 258:, 55:. 1044:. 1007:. 984:. 956:. 935:. 808:} 796:( 790:, 787:s 784:, 778:( 766:( 760:, 757:0 754:, 748:( 742:; 733:{ 730:) 727:s 724:* 718:( 669:) 657:( 653:) 641:( 586:n 581:n 575:n 566:n 552:n 547:n 541:n 536:n 522:n 517:n 421:} 415:{ 412:) 403:; 400:5 391:; 388:0 385:= 379:( 363:n 358:m 354:n 350:m 337:m 333:n 323:m 319:n 314:n 310:m 237:) 231:( 219:) 213:( 208:) 204:( 194:ยท 187:ยท 180:ยท 173:ยท 146:. 117:) 111:( 106:) 102:( 98:. 88:. 62:) 58:( 23:.

Index

Oregon Battle of the Books
improve it
talk page
Learn how and when to remove these messages
confusing or unclear
clarify the article
the talk page
Learn how and when to remove this message

verification
improve this article
adding citations to reliable sources
"Off-by-one error"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
Learn how and when to remove this message
logic error
mathematical
computer programming
loop
zero-based numbering
array
fencepost error
half-open intervals
loop
do-while loop

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

โ†‘