MIPSpro C compiler and string literals
#1
MIPSpro C compiler and string literals
Consider this code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int main(void) {
    const char a[] = "\0This is a text";
    const char b[] = { 0x0, 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a',
                       ' ', 't', 'e', 'x', 't', 0x0 };
    const char *c  = "\0This is a text";
    char buf1[128];
    char buf2[128];
    char buf3[128];

    size_t lena = sizeof(a) - 1;
    size_t lenb = sizeof(b) - 1;
    size_t lenc = 15;

    memcpy(buf1,a,lena);
    memcpy(buf2,b,lenb);
    memcpy(buf3,c,lenc);

    printf("length a : %d, length b %d, length c %d\n", lena, lenb, lenc);
    printf("buf1[1] : %x, buf2[1] : %x, buf3[1] : %x\n", buf1[1], buf2[1], buf3[1]);
    printf("comparing a with buf1 : %d \n", memcmp(a,buf1,lena));
    printf("comparing b with buf2 : %d \n", memcmp(b,buf2,lenb));
    printf("comparing c with buf3 : %d \n", memcmp(c,buf3,lenc));
    printf("comparing buf1 with buf2 : %d \n", memcmp(buf1,buf2,lena));
    printf("comparing buf1 with buf3 : %d \n", memcmp(buf1,buf3,lenc));
    printf("comparing buf2 with buf3 : %d \n", memcmp(buf2,buf3,lenb));

    return 0;
}

I wrote it to illustrate a problem in the MISPpro C compiler handling string literals on the stack:
The three text containers a, b and c are initialized differently but all should contain "This is a text"  preceded and suffixed by a zero byte.
Container a's type is an array of char. Its lifetime is limited to the main scope and its initialized content gets put on the stack by the compiler.
Container b is the longer version of a and is also typed array of char.
Container c is a pointer to a read-only memory block of chars allocated and initialized by the compiler on the heap.

If you compile it with MIPSpro cc or c99 and run it, you will see that array a is not initialized properly. a[1] should contain capital 'T' but instead it's a zero byte: once looking at the stack where a is put with a debugger, there is nothing: The preceding zero byte interferes with compiler code and the stack, although it will allocate the correct amount of bytes on the stack, will only contain zeroes.

This 'shorthand' initialization of an array is often used in modern opensource software, and is particularly prevalent in test suites, where binary headers and unicode data content are initialized in arrays to test functions.

F.I. I had to change code in expat-2.2.6 to get through all the test cases, and am attempting this now on libarchive.

Needless to say that GCC will correctly put the content of a on the stack: try running it on linux and compare the outcome.

TLDR: shorthand initialization of char a[] = "  "; with MIPSpro will go wrong with leading zero bytes. Rewrite that code.
(This post was last modified: 10-20-2018, 11:06 PM by dexter1.)
dexter1
Administrator

Trade Count: (1)
Posts: 297
Threads: 17
Joined: May 2018
Location: The Netherlands
Find Reply
10-20-2018, 11:05 PM
#2
RE: MIPSpro C compiler and string literals
thank you for this info
gijoe77
Tezro

Trade Count: (1)
Posts: 644
Threads: 34
Joined: Jun 2018
Find Reply
10-21-2018, 02:44 AM
#3
RE: MIPSpro C compiler and string literals
I have no SGI turned on currently, but the IBM is on at work, so I tried this on xlC

Code:
-bash-4.4# ./a.out
length a : 15, length b 15, length c 15
buf1[1] : 54, buf2[1] : 54, buf3[1] : 54
comparing a with buf1 : 0
comparing b with buf2 : 0
comparing c with buf3 : 0
comparing buf1 with buf2 : 0
comparing buf1 with buf3 : 0
comparing buf2 with buf3 : 0

Hey Ho Pip And Dandy!!!!!

Onyx2  Tezro   Octane2
uunix
Octane

Trade Count: (0)
Posts: 212
Threads: 23
Joined: Dec 2017
Location: Middle of the UK..ish
Find Reply
10-21-2018, 10:35 AM


Forum Jump:


Users browsing this thread: 1 Guest(s)