You are not logged in.
Pages: 1
I have been programing in C for a couple of years. Today I wanted to make a function called clean(char *str), wich should replace instances of "%20" with ' ' for my shitty webserver, the function is the next:
void clean(char *str){
char *ptr = str;
//look for instances of '%' after finding that, check if the next 2 caracters are '2' and '0', if they are, you found a "%20", after that replace the "%20" with a ' ' and move the string 2 bytes back
while((ptr = strchr(ptr, '%')) != NULL){
if(ptr[1] == '2' && ptr[2] == '0'){
*ptr = ' ';
strcpy(ptr+1, ptr+3);
/* makeshift implementation of strcpy.
int i, q;
for(i = 1, q = 3; ptr[q] != '\0'; i++, q++)
ptr[\i] = ptr[q];
ptr[\i] = '\0'; */
}
ptr++;
}
}
When I execute the function with the following code:
char asd[] = "How%20To%20Get%20Arrested%20In%2030%20Minutes%20Cracking%20A%20GSM%20Capture%20File%20In%20Real-time%20With%20AIRPROBE%20And%20KRAKEN-EFLvHMJ5PHk.mp4.mp4.html";
clean(asd);
puts(asd);
I get: "How To Get Arrested In 30 Minutes Cracking A GSM Capture File In Real-time With AIRPROBE And 5PHkHk.m.mp4p4.m.mp4p4.h.htmtml", as you can read, the last part of the string is completly messed up, that's weird as I only modify the string by changing parts of it with ' '. Also I included an implementation of the function wich works correctly. I want to know if this also happens in your computer or if I have somehow a wrong copy of the strcpy function. If I am the one messing things up tell me, I'll delete the post so that I don't waste anyone's time anymore.
Last edited by reiman (2025-06-05 09:50:53)
Offline
forgot to say, you can try to comment strcpy and uncomment the code next to it to make clean() work properly. If anymore information is needed tell me, I'll be happy to provide it.
Last edited by reiman (2025-06-04 23:26:42)
Offline
Overlapping src and dest arrays for strcpy is an undefined behavior.
Offline
If you keep track of the string's length, you could use memmove() instead, for which this sort of thing is well-defined.
EDIT: Here's what I mean:
#include <string.h>
#include <stdio.h>
/* Replace all instances of "%20" in s with " ". */
void clean(char *s){
int len = strlen(s) + 1; /* +1 for the null byte */
char *p = s;
while(len > 0 && (p = strstr(p, "%20"))){
*p = ' ';
len -= p - s; /* account for the chars skipped by strstr() */
len -= 3; /* account for the "%20" */
memmove(p + 1, p + 3, len);
s = ++p;
}
}
int main(){
char s[] = "String%20with%20spaces.%20Yeah.%20Spaces!";
clean(s);
printf("%s\n", s);
}
This version uses strstr() instead of strchr() so it has the disadvantage that it'd be slightly harder to make it properly handle hex codes. Also, both versions have the problem that they are O(N^2) instead of O(N).
If I am the one messing things up tell me, I'll delete the post so that I don't waste anyone's time anymore.
Please don't; then my version would be lost to time :(.
Last edited by 256 (2025-06-05 10:29:27)
"Don't comment bad code - rewrite it." - The Elements of Programming Style (1978), Brian W. Kernighan & P. J. Plauger, p. 144.
Offline
Pages: 1