Now Reading
Catch-23: The New C Normal Units the World on Hearth

Catch-23: The New C Normal Units the World on Hearth

2023-04-01 16:01:59

Drill Bits


Download PDF version of this article

Drill Bits

Terence Kelly with Particular Visitor Borer Yekai Pan

A brand new main revision of the C language customary, C23, is due out this yr. We’ll tour the highs and lows of the most recent draft9 after which drill down on the mom of all breaking adjustments. Sidebars rejoice C idioms and undefined conduct with code and music, respectively.


The Good Information

Just like the earlier main revision, C11,7 the most recent customary introduces a number of helpful options. A very powerful, if not probably the most thrilling, make it simpler to put in writing secure, appropriate, and safe code. For instance, the brand new <stdckdint.h> header standardizes checked integer arithmetic:


 int i =...; unsigned lengthy ul =...; signed char sc =...; 
bool shock = ckd_add(&i, ul, sc);


The sort-generic macro ckd_add() computes the sum of ul and sc “as if each operands had been represented in a signed integer sort with infinite vary.” If the mathematically appropriate sum suits right into a signed int, it’s saved in i and the macro returns false, indicating “no shock”; in any other case, i finally ends up with the sum wrapped in a well-defined means and the macro returns true. Comparable macros deal with multiplication and subtraction. The ckd_* macros steer a refreshingly sane path round arithmetic pitfalls together with C’s “standard arithmetic conversions.”

C23 additionally provides new options to guard secrets and techniques from prying eyes and programmers from themselves. The brand new memset_explicit() perform is for erasing delicate in-memory knowledge; in contrast to peculiar memset, it’s meant to stop optimizations from eliding the erasure. Good previous calloc(size_t n, size_t s) nonetheless allocates a zero’d array of n objects of measurement s, however C23 requires that it return a null pointer if n*s would overflow.

Along with these new correctness and security aids, C23 offers many new conveniences: Constants true, false, and nullptr at the moment are language key phrases; mercifully, they imply what you anticipate. The brand new typeof characteristic makes it simpler to harmonize variable declarations. The preprocessor can now #embed arbitrary binary knowledge in supply recordsdata. Zero-initializing stack-allocated constructions and variable-length arrays is a snap with the brand new customary “={}” syntax. C23 understands binary literals and permits apostrophe as a digit separator, so you may declare int j = 0b10'01'10, and the printf household helps a brand new conversion specifier for printing unsigned varieties as binary (“01010101”). The proper resolution to the basic job interview downside “Depend the 1 bits in a given int” is now stdc_count_ones().

Sadly, excellent news is not the one information about C23. The brand new customary’s nonfeatures, misfeatures, and defeatures are sufficiently quite a few and extreme that programmers shouldn’t “improve” with out rigorously weighing dangers in opposition to advantages. Older requirements comparable to C99 and C11 weren’t excellent, however detailed evaluation will generally conclude that they’re preferable to C23.

After reviewing C23’s issues, we’ll focus on methods for peaceable coexistence with current code and hazard mitigation in new code.


C inventor Dennis Ritchie pointed to a number of flaws in [ANSI C] … which he stated is a licence for the compiler to undertake agressive opimisations which are fully authorized by the committee’s guidelines, however make hash of apparently secure applications; the confused try to enhance optimisation … spoils the language.

—Dennis Ritchie on the primary C customary 4, 27

Unfilled Potholes and Festering Sores

Legal guidelines needs to be freely obtainable, intelligible, and agreeable to the ruled, and they need to hold tempo with altering occasions. C23 lacks these virtues.

Normal C hides behind a paywall: The official customary presently prices greater than $200, so most coders make do with unofficial drafts.1 The usual routinely confuses its personal authors, and essential components mystify even skilled and well-educated programmers27; baffled silence just isn’t consent. Builders who handle to determine what the usual truly means are ceaselessly appalled.25,27 Normal C advances slowly (e.g., 30 years and 5 revisions to outline zero equal to zero26) and generally by no means.

Progress means draining swamps and fencing off tar pits, however C23 truly expands one among C’s most infamous traps for the unwary. All C requirements from C89 onward have permitted compilers to delete code paths containing undefined operations—which compilers merrily do, a lot to the shock and outrage of coders.16 C23 introduces a new mechanism for astonishing elision: By marking a code path with the brand new unreachable annotation,12 the programmer assures the compiler that management won’t ever attain it and thereby explicitly invitations the compiler to elide the marked path. C23 moreover offers the compiler license to make use of an unreachable annotation on one code path to justify eradicating, with out discover or warning, a completely totally different code path that’s not marked unreachable: see the dialogue of places() in Instance 1 on web page 316 of N3054.9

Main disappointments of inaction contain the pillar of C programming: pointers. Evaluating tips that could totally different objects (totally different arrays or dynamically allotted blocks of reminiscence) remains to be undefined conduct, which is a well mannered means of claiming that the usual permits the compiler to run mad and the machine to catch fireplace at run time.16 The usual’s pointer comparability restrictions, rooted in forgotten historical {hardware} architectures, have stunning penalties. The seemingly harmless sequence a=malloc(...) then b=malloc(...) then if (a<b)... is a recipe for conflagration, and it is inconceivable to implement the usual memmove() perform effectively in customary C.16 Moreover, after a lot dialogue, the arcane and poorly motivated “pointer zap” guidelines21 stay in impact: Tips to free‘d reminiscence are akin to uninitialized pointers, so free(p) adopted by if (p==q) is an instrument of arson. Issues needn’t be so.

C23 fails to appropriate misguidance courting to the earliest model of the usual. Its instance implementation of rand() remains to be the identical primitive linear congruential generator returning 16-bit integers—a design that was ripe for taxidermy on the flip of the century. XORshift random quantity mills, invented 20 years in the past, would make a greater instance: They’re easy and quick, accommodate 32-, 64-, and 128-bit machine phrases, and produce superior random sequences.20

Builders also needs to be aware that C23 has drifted farther from C++ than the sooner C requirements. The notion that C is (largely) a subset of C++ is farther from actuality than ever earlier than.10

Sadly, missed alternatives and incompatibilities with C++ aren’t the worst elements of the brand new customary. C23 transforms many years of completely respectable applications into Molotov cocktails.


Incendiary realloc()

The realloc perform, customary since C89, resizes a reminiscence allocation. C23 senselessly outlaws a helpful realloc characteristic that was very intentionally designed and blessed by C89 by C11, rendering C23 realloc far much less versatile and stuffing tinder into myriad applications written to earlier requirements. To know the folly of the current ban, we should evaluation the full-featured realloc of yesteryear and the elegant idiom to which it’s completely suited.

C89 outlined realloc to incorporate malloc and free as particular circumstances:


void *realloc(void *ptr, size_t measurement);


“The realloc perform adjustments the dimensions of the thing pointed to by ptr to the dimensions specified by measurement. If ptr is a null pointer, the realloc perform behaves just like the malloc perform for the required measurement…If the area can’t be allotted, the thing pointed to by ptr is unchanged. If measurement is zero and ptr just isn’t a null pointer, the thing it factors to is freed.”

— C89,2 repeated verbatim in Plauger22


Loads of real-world code exploits the flexibility of realloc. Examples embrace dozens of executables on the search $PATH of Linux machines:


$ echo foo | ltrace grep bar |& grep realloc 
realloc(0, 128)   = 0x55a17f5596f0


The C89 and C99 requirements committees strongly really useful that allocation interfaces malloc, calloc, and realloc return a null pointer in response to zero-byte requests.3,6 This means that realloc(p,0) ought to unconditionally free(p) and return NULL: No new allocation occurs on this case, so there is no chance of an allocation failure. For brevity, let “zero-null” denote allocator implementations that adjust to the C89/C99 steerage.

The Swiss-Military-knife side of realloc is daunting at first, however this interface rewards affected person research. Quickly you understand that zero-null realloc was thoughtfully designed to allow elegant dynamic arrays that do precisely the fitting factor underneath all circumstances, obviating the necessity for clunky and error-prone code to deal with grow-from-zero and shrink-to-zero as particular circumstances.

Determine 1 illustrates idiomatic realloc through a easy stack that grows with each push() and shrinks with each pop(). Pointer S and counter N (traces 1 and a pair of) symbolize the stack: S factors to an array of N strictly optimistic ints. As a result of they’re statically allotted, initially the pointer is NULL and the counter is zero, indicating an empty stack. Operate resize (traces 4–10) resizes the stack to a given new capability, checking for arithmetic overflow (line 6) earlier than calling realloc and checking the return worth for reminiscence exhaustion (line 8). Allocation failure is inferred when a nonzero new measurement is requested however NULL is returned; zero-null realloc additionally returns NULL when the second argument is zero, however this doesn’t point out an allocation failure as a result of no allocation was tried. (Checking errno does not allow transportable code to detect allocation failure as a result of the C requirements do not say how out-of-memory impacts errno.) Because of zero-null realloc‘s versatility, the resize perform needn’t think about whether or not the stack is rising from zero or shrinking to zero or re-sizing in another means; every little thing Simply Works regardless.

Catch-23: The New C Standard Sets the World on Fire

FIGURE 1: Constantly rightsizing a stack with zero-null realloc

The code of determine 1 follows a number of easy guidelines implicit within the semantics of zero-null realloc. Capabilities push and pop (traces 12–23) entry the stack solely through subscripts on S, as a result of realloc might transfer the array to a unique location in reminiscence. They by no means dereference S when N is zero. The resize perform resists the temptation of reckless S = realloc(S,...), which destroys the entry level into the array when allocation fails, thereby leaking reminiscence and dropping knowledge.

I have been seeing code resembling determine 1 for 30 years, beginning with the work of an older schoolmate who had bothered to learn the fantastic handbook; the readability and ease of his code left a deep impression. Within the many years since then I’ve repeatedly discovered idiomatic realloc in severe manufacturing code, often whereas scanning for p = realloc(p,...) bugs.

Think about, then, my dismay once I discovered that C23 declares realloc(ptr,0) to be undefined conduct, thereby pulling the rug out from underneath a widespread and exemplary sample intentionally condoned by C89 by C11. A lot for stare decisis. Compile idiomatic realloc code as C23 and the compiler would possibly maul the supply in most astonishing methods and your machine may ignite at runtime.16 To make issues a lot worse, recompilation just isn’t a prerequisite for conflagration: Merely re-linking current compiled binaries with a brand new or “upgraded” customary library units the stage for catastrophe. In case your customary library is carried out as a dynamically linked shared library (e.g.,, working a binary executable from yesteryear will load the most recent library at run time, so have a fireplace extinguisher available while you improve that shared library to C23. Each program that makes use of realloc as free within the method meant by three generations of requirements is an inferno ready to occur, and the legions of programmers accustomed to basic versatile realloc want re-education.

The rapid clarification for this disastrous change is remarkably unconvincing and apparently took scant discover of many years of sound idiomatic utilization: Basically, “implementations of realloc(p,0) differ, so let’s scrap the lot,”23 which inverts a boldface tenet of C standardization: “Present code is vital, current implementations usually are not.3,6 The total unhappy historical past reveals a a lot bigger and extra troubling downside that bodes unwell for the way forward for C.


The (Com)Promised Land

Requirements are purported to paved the way to a greater world by making transportable code doable. Real standardization inevitably requires herding cats—allowing various compiler and library implementations to flourish whereas imposing smart conduct. The chronicle of the realloc affair reveals that C standardization does not work that means these days.

As C89 was taking form, the neurodivergent notion of a “zero-length object” was making the rounds: Proponents argued {that a} non-null pointer to such an object needs to be returned for zero-byte allocation requests.

Why are such requests made? Typically due to arithmetic bugs. And what’s a non-null pointer from malloc(0) good for? Completely nothing, besides taking pictures your self within the foot.

It’s unlawful to dereference such a pointer and even evaluate it to some other non-null pointer (recall that pointer comparisons are flamable in the event that they contain totally different objects). Scour the annals of computing and you will find few issues extra completely ineffective than a zero-length object and few issues extra hazardous than a pointer thereto. Not surprisingly, analogs are uncommon on the earth past computing: Attempt depositing a verify within the quantity of $0 into your checking account.

Each C89 and C99 properly “determined to not settle for the thought of zero-length objects,” however foolishly did not ban it. As we noticed earlier, they strongly really useful zero-null allocation, however in addition they reluctantly allowed malloc(0) to return non-null as an amnesty for wayward implementations that already did so.3,6 Which implies that realloc(p,0) would possibly have to allocate a brand new zero-length object. And this allocation would possibly fail (simply as an try and deposit money right into a checking account and concurrently withdraw $0 would possibly fail—say it to your self slowly, savoring the absurdity). By the point C17 was within the works, implementations that tried to allocate a zero-length object for realloc(p,0) disagreed about whether or not free(p) ought to occur if the allocation fails. So C17 made this conduct implementation-defined and declared realloc-as-free obsolescent,8 setting the stage for C23’s outright ban.


xkcd 2730


To summarize, this downward spiral started with an idea worthy of Monty Python that snowballed and metastasized, because of a feckless compromise. The C23 realloc mess is simply the tip of the iceberg. The foundation downside is the failure of a typical to standardize. Trying ahead, marijuana legalization will certainly beget notions comparable to fractional-, imaginary-, and negative-length objects, every with as a lot potential for mayhem as zero-length objects. Allow us to hope that future requirements committees will work up the braveness to do greater than survey the established order, sprinkle most of it with holy water, and consign to flame no matter truly must be standardized.


Muddling By

How do you have to reply to C23? Perceive its implications for each your current code and for code but unwritten. Compile previous code as C23 just for good motive and solely after verifying that it does not run afoul of any constriction within the new customary. In case you want new C23 options, think about quarantining C23 code in separate translation models; luckily, object-code recordsdata compiled from totally different supply dialects might be linked collectively. Beware that adjustments to the usual library may impose unwelcome new semantics—or abolish required previous semantics, as with realloc—and that such adjustments might impose themselves on previous code with out recompilation when dynamically linked libraries are upgraded.

Requirements usually are not some sort of holy guide that needs to be revered. Requirements too have to be questioned.

— Linus Torvalds on C requirements25

In case you’re the kind of one that thinks independently and insists that the instruments of the commerce be intelligible and smart, you are within the majority. Work along with your colleagues to foyer your compiler and library distributors and the requirements committee ([email protected]) if issues aren’t to your liking.


Drilling Deeper

To write down new code, you could observe present language requirements; to take care of previous code, you could perceive earlier ones. Kernighan and Ritchie18 present the basic account of C892; Plauger paperwork its customary library.22 Harbison and Steele13 cowl C99.5 Klemens19 explains helpful options launched in C11.7 Hatton particulars precautions for safety-critical C coding.14



Obtain the instance code at You get the stack of determine 1 and easy wrapper code that transforms any standards-compliant reminiscence allocator right into a zero-null allocaor.



1. Determine 1’s stack sacrifices velocity for readability and brevity. Implement a extra environment friendly design that individually tracks capability and merchandise depend, resizing capability by 2x as applicable.17

2. In case your malloc(0) returns non-null, what number of calls does it take to exhaust reminiscence? What number of $0 withdrawals does it take to bankrupt your financial institution?

3. Use the brand new C23 #embed characteristic to implement literate executables.15

4. Seek for the p = realloc(p,...) bug in actual code and textbooks (e.g., web page 253 of Klemens19). See additionally web page 101 of the C89 Rationale3 and web page 160 of the C99 Rationale.6

5. In case you suppose idiomatic C is cryptic, recall the previous joke concerning the Perl mafioso: He makes you a suggestion you may’t perceive. Record the perfect idioms and worst abuses of your favourite languages.

6. Verify the #outline of INT_MIN in <limits.h>. In case you see one thing like (-INT_MAX - 1), why is not it extra easy? See web page 46 of Gustedt.11

7. C178 purports to be a bug-fix revision of C11. Does the phrase “toto” on web page 1 point out (a) the editor’s musical tastes; (b) that no person bothered to spell-check the doc; (c) that we’re not in Kansas anymore; or (d) not one of the above?

8. Programmer Yossarian’s software requires the brand new C23 memset_explicit perform but additionally requires realoc(p,0) to be nicely outlined. If each capabilities stay in, is Yossarian caught in a Catch-23? What ought to he do?

9. Following Shiffman,24 write a Socratic dialogue through which C inventor Dennis Ritchie interrogates the C requirements committee. See Yodaiken27 for speaking factors.



Idioms and Fluency

Catch-23: The New C Standard Sets the World on Fire

I decry C23’s ban on the elegant idiomatic use of a basic reminiscence allocation perform. Why do you have to care? What’s so vital about idioms?

Fluent, idiomatic code expresses the programmer’s intentions extra precisely, extra clearly, and infrequently extra succinctly than rookie code. C’s idioms usually are not excessively quite a few or abstruse, however to grasp them you could climb a studying curve. For instance, the snippets above present how most C programmers be taught to break down a numeric variable to a Boolean. The “bang-bang” idiom on the backside is not finest in each state of affairs, but it surely’s usually useful and you could acknowledge it to learn professional code.

Idiomatic expression proves its practicality when cluttery options would complicate an inherently easy chore. For instance, think about a lodge that prices further for pets: The primary canine prices $10 and every extra pooch $5 extra; likewise for different animals however with totally different constants. Idiomatic code is pure, straightforward to put in writing, compact, and apparent to fluent readers:


 petFee = !!ndogs * (10 + (ndogs-1) * 5)  // danger: rugs 
        + !!ncats * ( 7 + (ncats-1) * 3)  // " furnishings
        + !!nfish * (47 + (nfish-1) * 1); // " floods


Bang-bang, like most C idioms, relies not on esoteric data however moderately on a radical understanding of fundamentals. Newbies who overlook the bang-bang possibility learn about logical NOT, however maybe they assume that double negation cannot be helpful. Fluent programmers, nonetheless, recognize the peculiar nuances of the “!” operator. Mastering double-bang is essentially a matter of absolutely understanding single-bang.

Kernighan and Pike focus on programming idioms at size.17 Klemens describes cool idioms enabled by C11 options.19 Yodaiken explains how elements of the C requirements meant to allow efficiency optimizations undermine methods programming idioms.27

Train: Amend the petFee method above so as to add premiums for mixtures of species that do not play good collectively: $30 for any numbers of canines and cats, as a result of noise; and $20 for any numbers of cats and fish, as a result of splashes. Trace: What occurs while you multiply Booleans?



Undefined Conduct Acid Journey

This parody of Jefferson Airplane’s basic music “White Rabbit” is about programming psychedelia—undefined conduct in C. The title refers back to the empty assembly-code file you get when the compiler elides code paths with UB.16 Helgrind is a software within the Valgrind suite. Chris Lattner created the Clang compiler.


One flag makes it sooner

and one flag makes it small

and the deprecated -Wchkp

does not do something in any respect.

Go ask Lattner

if we should always use -Wall.

And if you happen to go evaluating pointers

throughout segments you are going to fall.

That is how a hookah-smoking working group

See Also

has standardized all of it.

Go ask Lattner

did they make the fitting name?

When your loops and expressions

rise up from the place you stated they go

and Clang simply had some sort of warning

and Valgrind is transferring gradual,

go ask Lattner;

I hope he’ll know.

When the logic of -O3

is looking your code lifeless

and the essential() activity is writing backwards

whereas the employees race forward,

keep in mind what the Helgrind stated:

Lock your thread. Lock your thread.


Undefined Behavior Acid Trip: white.s



Jon Bentley, Hans Boehm, John Dilley, Kevin O’Malley, and Charlotte Zhuang reviewed drafts and supplied useful suggestions. Dilley and O’Malley scrutinized the instance code and really useful helpful enhancements. Dhruva Chakrabarti and Pramod Joisha fielded technical questions. We thank C23 requirements committee members Aaron Ballman, Robert Seacord, and JeanHeyd Meneide for exchanges of correspondence.



1. C Requirements Committee (Working Group 14). Paperwork;

2. C89 Normal;

3. C89 Rationale, ANSI X3J11/88–151, November 1988. Obtainable through

4. Laptop Enterprise Assessment employees. 1988. Proposed ANSI C language customary attracts criticism as remark interval ends;

5. C99 Normal (draft n1256). 2007.

6. C99 Rationale. 2003.

7. C11 Normal (draft n1570). 2011.

8. C17 Normal (draft n2176).

9. C23 Normal (draft n3054). 2022.

10. Ballman, A. 2022. WG14 doc n3065: C xor C++ programming; [Also available at Reference 1.]

11. Gustedt, J. 2019. Fashionable C, second version. Manning;

12. Gustedt, J. 2021. WG14 doc n2826v2. Add annotations for unreachable management movement; [Also available at Reference 1.]

13. Harbison, S. P., Steele III, G. L. 2002. C: A Reference Guide, fifth version. Prentice Corridor.

14. Hatton, L. 1995. Safer C: Creating Software program for Excessive-Integrity and Security-Essential Programs. McGraw-Hill.

15. Kelly, T. 2022. Literate executables. acmqueue 20(5);

16. Kelly, T., Gu, W., Maksimovski, V. 2021. Schrödinger’s code: undefined conduct in concept and observe. acmqueue 19(2);

17. Kernighan, B., Pike, R. 1999. The Observe of Programming. Addison-Wesley.

18. Kernighan, B. W., Ritchie, D. M. 1988. The C Programming Language, second version. Prentice Corridor.

19. Klemens, B. 2014. twenty first Century C, second version. O’Reilly Media.

20. Marsaglia, G. 2003. Xorshift RNGs. Journal of Statistical Software program 8(14);

21. McKenney, P. E., Michael, M., Mauer, J., Sewell, P., Uecker, M., Boehm, H., Tong, H., Douglas, N., Rodgers, T., Deacon, W., Wong, M. 2019. WG14 doc n2443: Lifetime-end pointer zap; [Also available at Reference 1.]

22. Plauger, P. J. 1992. The Normal C Library. Prentice Corridor.

23. Seacord, R. C. 2019. WG14 doc n2464: Zero-size reallocations are undefined conduct; [Also available at Reference 1.]

24. Shiffman, M. 2022. A person of all causes. Harper’s Journal (April), 15–16;

25. Torvalds, L. 2018. Linux kernel mailing checklist posting;

26. C FP Group. 2021. WG14 doc n2670: Zeros evaluate equal; [Also available at Reference 1.]

27. Yodaiken, V. 2021. How ISO C turned unusable for working methods improvement. eleventh Workshop on Programming Languages and Working Programs (PLOS ’21).


Terence Kelly ([email protected]) and Yekai Pan take pleasure in surveying the established order, sprinkling most of it with holy water, and consigning to flame the components they do not like.

Copyright © 2023 held by proprietor/writer. Publication rights licensed to ACM.


Initially printed in Queue vol. 21, no. 1

Touch upon this text within the ACM Digital Library

© ACM, Inc. All Rights Reserved.

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top