Is it dangerous to promote types in printf arguments?

前端 未结 4 622
囚心锁ツ
囚心锁ツ 2021-02-09 06:36

My questions stem from trying to use printf to log things when trying to build for multiple bit-depth platforms (32/64 for example).

A problem that keeps rearing its ugl

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 07:07

    Regarding printf: In the case you sited, "%d" must, by specification, handle the platform-defined 'int' data type. It doesn't matter whether it is 32bits, 64bits, or 128bit linear AS/400 value. If you want to promote the value to a larger field type (and match that promotion with the related format string particle) you're certainly free to do so,

    int a=0;
    printf("%ld", (long)a);
    

    is certainly defined behavior using promotion.

    I think the real crux of your question comes in cases like the following, and whether forcing promotion can "solve" any problems that arise. For example:

    char ch = 'a';
    printf("%d", ch);
    

    or what about:

    char ch = 'a';
    printf("%ld", (long)ch);
    

    or maybe this (which is the real condition you seem to be trying to avoid):

    char ch = 'a';
    printf("%ld", ch);
    

    The first of these will work, but only because the minimum size of anything stack-pushed on a va-arg list is the platform-size of an int. The compiler will auto-promote the value to an int for you. Since "%d" expects a platform int all will appear well.

    The second will work always and is fully supported. There is a clear and defined promotion from char to long. Even if long is 64bits (or larger) it will still work.

    The third is UB all the way. printf is looking for a long and will be presented with only bytes for an int. If this seems to "work" on your platform, then check your platform width for int and long. It is likely "working" only because your platform long and int are the same bit-width. It makes for fun surprises when porting the code to platforms where they are not, and since it is pushed through a va-arg, you won't see it until real different widths come in to play.

    All of that being said, now throw an actual address to something (anything, really) such as that required by scanf and we're looking at something entirely different.

    int val;
    sscanf("%ld",&val);
    

    This is a seg-fault waiting to happen. Just like above, you'll never know it if your platform long and platform int are the same width. Take this code to a box where long and int are different sizes and prep yourself for a gdb-load of the ensuing core file.

提交回复
热议问题