I believe I am hitting some boundary conditions, my requirement is to send some wifi scan report to mosquitto followed by Kibana
When I get wifi scan report in msg and try to convert that into a string as below
char *json = blobmsg_format_json(msg, true);
printf("json - %s and len - %ld", json, strlen(json));
this prints only 2048 characters in printf and strlen - ~28000
So my question is what is the max length of a constant string a char pointer can point to in C?
I am worried that char *json
may not be pointing to complete string and what if I am getting only part of the string because of limitations?
More details: I suspected above problem in question then I tried to declare another char pointer with malloc and then copy the string followed by terminating the string later to make sure its null terminated
/* 30000 Magic number I am using only for this example where I
see that string len is not never greater than ~29000. This is
just a debug and not will never make it to production */
char *tmpjson = (char *) calloc(30000, sizeof(char));
strncpy(tmpjson, json, strlen(json));
strcat(tmpjson, "\"}");
printf("tmpjson - %s and len - %ld\n", tmpjson, strlen(tmpjson));
output is still the same as first printf except for terminating char
CodePudding user response:
For your test code, you might be hit by an environmental limit: here is the relevant paragraph from the C Standard:
7.23.6.1 The
fprintf
function[...]
Environmental limits 16 The number of characters that can be produced by any single conversion shall be at least 4095.
Hence the implementation of printf
might limit the number of characters printed from the json
string to 4095, which makes this printf
statement inappropriate for debugging your problem. In your observations, you only get 2048 characters, so either your library is seriously deficient or the way you collect the output and mesure its length is not working as expected.
The maximum size of a C string in only limited by available memory given by the operating system to your process with an absolute maximum at SIZE_MAX
, which is at least 65535
and is the huge 64-bit number 18 446 744 073 709 551 615 on most current systems, so you are unlikely to hit this limit.
The function blobmsg_format_json
might have its own limitations, which you can read in the documentation or determine from the source code.
Your second code fragment is incorrect: strncpy
does not do what you expect, your code will have undefined behavior if the string pointed to by json
has a length >= 30000. Learn why you should never use strncpy
.
If you want to make allocate a copy of the string, use this:
char *tmpjson = strdup(json);
printf("tmpjson - %s and len - %zu\n", tmpjson, strlen(tmpjson));
But it should not come as a surprise that the result be identical.
To avoid printf
limitations, use this:
printf("tmpjson - ");
fputs(json, stdout);
printf(" and len - %zu\n", strlen(json));
If the output is still too short, the problem definitely comes from the way you collect and measure the output.
The way you transmit the JSON data to the logging service is also something to investigate. Please post more details about that and your execution environment.
CodePudding user response:
The maximum length is the maximum number representable by size_t
, I believe, but this is guaranteed to be at least 16-bit and is 64-bit on my HP laptop. This is probably not your issue.
CodePudding user response:
The pointer refers the address of the first element of the string. In that sense there is no limit on the length of string a pointer can refer to, because the end is not implied in the pointer value.