Advertisement
losinggeneration

Untitled

Jun 26th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.  
  5. /*  This fails if str isn't correctly terminated */
  6. int nums1(const char *str, size_t unused) {
  7.     char *a;
  8.     a = (char *)str;
  9.     int num = 0;
  10.  
  11.     printf("%x: ", a);
  12.     if(!str) return num;
  13.     while(*a) {
  14.         if(*a == ',') num++;
  15.         printf("(%c) %x ", *a, a);
  16.         a++;
  17.     }
  18.     printf("\n");
  19.  
  20.     return num;
  21. }
  22.  
  23. /*  This fails if str isn't correctly terminated */
  24. int nums2(const char *str, size_t unused) {
  25.     int i;
  26.     int num = 0;
  27.     printf("%x: ", str);
  28.     if(!str) return num;
  29.     for(i = 0; i < strlen(str); i++) {
  30.         printf("(%c) %x ", str[i], &str[i]);
  31.         if(str[i] == ',') num++;
  32.     }
  33.     printf("\n");
  34.  
  35.     return num;
  36. }
  37.  
  38. /*  likely the safest assuming a correct len is passed in */
  39. int nums3(const char *str, size_t len) {
  40.     int i;
  41.     int num = 0;
  42.  
  43.     printf("%x: ", str);
  44.  
  45.     if(!str) return num;
  46.  
  47.     for(i = 0; i < strnlen(str, len); i++) {
  48.         printf("(%c) %x ", str[i], &str[i]);
  49.         if(str[i] == ',') num++;
  50.     }
  51.     printf("\n");
  52.  
  53.     return num;
  54. }
  55.  
  56. /*  I'm not so sure @koorogi */
  57. int nums4(const char *str, size_t len) {
  58.     int num = 0;
  59.  
  60.     printf("%x: ", str);
  61.     if(!str) return num;
  62.  
  63.     const char *a = strchr(str, ',');
  64.     while (a) {
  65.         /* Print the whole string of what's left */
  66.         printf("(%s) %x ", a, a);
  67.         a = strchr(a + 1, ',');
  68.         num++;
  69.     }
  70.     printf("\n");
  71.  
  72.     return num;
  73. }
  74.  
  75. #define nums(x, y) nums4(x, y)
  76. #define STRSIZE 9
  77.  
  78. int main(int argc, char *argv[]) {
  79.     printf("%d\n", nums("", 0));
  80.     printf("%d\n", nums("5, 10", 6));
  81.  
  82.     char str1[] = "5, 10";
  83.     printf("%d\n", nums(str1, strlen(str1)));
  84.  
  85.     const char *str2 = "5, 10";
  86.     printf("%d\n", nums(str2, strlen(str2)));
  87.  
  88.     char str3[STRSIZE] = "5, 10";
  89.     /* Overflow str3 by 1 */
  90.     int i;
  91.     for(i = 0; i <= STRSIZE; i++)
  92.         str3[i] = 0x61;
  93.     printf("%d\n", nums(str3, STRSIZE));
  94.  
  95.     char str4[STRSIZE] = "5, 10";
  96.     /* Overflow str4 by 1 after "5, 10" */
  97.     for(i = 5; i <= STRSIZE; i++)
  98.         str4[i] = 0x61;
  99.     printf("%d\n", nums(str4, STRSIZE));
  100.  
  101.     /*  Not terminated */
  102.     char str5[STRSIZE] = "012345678";
  103.     printf("%d\n", nums(str5, STRSIZE));
  104.  
  105.     /* Never trust the user... */
  106.     printf("%d\n", nums(NULL, 0));
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement