Joseph Shenton
Joseph Shenton

Reputation: 108

How to use a function in a compile-time constant

I am trying to declare a random string as a variable that changes each run that everything can access however my knowledge of C is very limited.

I've tried researching multiple websites and using tutorials but I cannot seem to explain it correctly.


// Declare example
#define DEST_SIZE 40

char *randstring(int length) {    
    char *string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
    size_t stringLen = 26*2+10+7;        
    char *randomString;

    randomString = malloc(sizeof(char) * (length +1));

    if (!randomString) {
        return (char*)0;
    }

    unsigned int key = 0;

    for (int n = 0;n < length;n++) {            
        key = rand() % stringLen;          
        randomString[n] = string[key];
    }

    randomString[length] = '\0';

    return randomString;
}
// char *randomHome = randstring(10);

// char * payloadPath = "~/Desktop/resign_temp_app/";

char dest[DEST_SIZE] = "~/Desktop/AppSign/";

char *randomHome = randstring(10);

char* plx = strcat(dest, randomHome);
char* plx2 = strcat(dest, "/");

const char * payloadPath = dest;


// Function Example
int rmTempAppPath(){
   char dest2[DEST_SIZE] = "rm -rf ";
   char *command = strcat(dest2, dest);
   int status = system(command);
   if (status != 0)
   {
       return -1;
   }
   return 0;
}

I was hoping that it would just work however I get the following error and warning messages

resignCore.c:39:20: error: initializer element is not a compile-time constant
char *randomHome = randstring(10);
                   ^~~~~~~~~~~~~~
resignCore.c:41:13: error: initializer element is not a compile-time constant
char* plx = strcat(dest, randomHome);
            ^~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/secure/_string.h:131:3: note: 
      expanded from macro 'strcat'
                __builtin___strcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
resignCore.c:42:14: error: initializer element is not a compile-time constant
char* plx2 = strcat(dest, "/");
             ^~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/secure/_string.h:131:3: note: 
      expanded from macro 'strcat'
                __builtin___strcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 errors generated.

I am really sorry for providing quite a bit of code and errors but I am not very good at explaining.

Upvotes: 0

Views: 75

Answers (1)

Tagger5926
Tagger5926

Reputation: 442

Here is a modified version of your code to work as a simple example. Just for the future, it doesn't matter if your code is big. Reduce the scope of the problem. I don't need your whole project source code, just give us a simple snippet that isolates the problem as much as possible.

Now the problem was that you had global variables that you try to define with non compile-time constants. So you initialize them with compile-time constants such as NULL and then define then in your main function before you call your other functions.

strcat(dest, src) appends src to dest and the return value is dest as well. char *val = strcat(dest, src) is superfluous and error prone. So just use strcat(dest,src).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DEST_SIZE 40

// Globals
char dest[DEST_SIZE] = "~/Desktop/AppSign/";
char *randomHome = NULL;

char *randstring(int length) {    
    const char * string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
    size_t stringLen = sizeof(string)/sizeof(char);        
    char *randomString = NULL;
    randomString = malloc(sizeof(char) * (length +1));
    if (!randomString) {
        return NULL;
    }

    for (int n = 0;n < length;n++) {            
        unsigned int key = rand() % stringLen;          
        randomString[n] = string[key];
    }

    randomString[length] = '\0';

    return randomString;
}

void printGlobal()
{
    printf("%s",dest);
}

int main()
{
    randomHome = randstring(10);
    strcat(dest, randomHome);
    printGlobal();
    strcat(dest, "/");
    printGlobal(); // print updated dest value
}

Disclaimer, there will be a memory leak if you don't delete randomString that you generate after usage.

What I like to do sometimes, especially when I am testing new code or libraries out it create a mini project for instance a single file project. Then try to create a minimal working example. As you can see, I didn't need the other 2 files to try and debug this problem.

Upvotes: 1

Related Questions