Proof that strlcpy is un-needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • endman
    Senior Member
    • Oct 2013
    • 176

    Proof that strlcpy is un-needed

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[]){
            char *input = argv[1];
            char *str;
    
            str = malloc(strlen(input)*sizeof(char));
            strcpy(str,input);
            printf("%s\n",str);
    }
  • JX8p
    Senior Member
    • Aug 2013
    • 125

    #2
    Linus Torvalds and the Linux kernel developers do not care. In fact, so little do they care about your 'proof' that they added strlcpy() to the Linux kernel over 11 years ago.

    Comment

    • movieman
      Senior Member
      • Apr 2008
      • 455

      #3
      BTW, you do realize you have a buffer overflow in that code, don't you?

      Comment

      • nasyt
        Senior Member
        • Oct 2014
        • 227

        #4
        Originally posted by endman View Post
        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        int main(int argc, char *argv[]){
                char *input = argv[1];
                char *str;
        
                str = malloc(strlen(input)*sizeof(char));
                strcpy(str,input);
                printf("%s\n",str);
        }
        Malloc in the kernel is evil!

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        int main(int argc, char *argv[]){
                char *input = argv[1];
                char str[100];
        
                strcpy(str,input);
                printf("%s\n",str);
        }
        You know: string longer than 100 = overflow

        Best choice:

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        int min(int a,int b){return (a<b)?a:b;}
        
        int main(int argc, char *argv[]){
                char *input = argv[1];
                char str[100];
        
                memcpy(str,input,min(strlen(str)+1,100);
                printf("%s\n",str);
        }

        Comment

        • nslay
          Senior Member
          • Jun 2012
          • 227

          #5
          Originally posted by endman View Post
          Code:
          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>
          
          int main(int argc, char *argv[]){
                  char *input = argv[1];
                  char *str;
          
                  str = malloc(strlen(input)*sizeof(char));
                  strcpy(str,input);
                  printf("%s\n",str);
          }
          When used this way, strdup() is proof that neither strcpy() nor strlcpy() are needed.

          Code:
          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>
          
          int main(int argc, char *argv[]){
                  char *input = argv[1];
                  char *str;
          
                  str = strdup(input);
          
                  printf("%s\n",str);
          }

          Comment

          • haagch
            Senior Member
            • Sep 2014
            • 961

            #6
            Since you can just write your stuff in inline assembly, none of these functions are needed.

            Comment

            • nasyt
              Senior Member
              • Oct 2014
              • 227

              #7
              Proof that &lt;string.h&gt; un-needed

              Code:
              #include <stdio.h>
              #include <stdlib.h>
              
              #define MAXS 100
              
              int main(int argc, char *argv[]){
                      char *input = argv[1];
                      char* ptr;
                      char str[MAXS];
                      ptr = str;
              
                      int n=MAXS;
                      while(n--&&*input)*(ptr++)=*(input++);
                      *ptr=0;
                      printf("%s\n",str);
              }

              Comment

              Working...
              X