1

我正在开发一个程序,其中包含“应该”过滤字符串的代码选择,以防止非字母数字字符(下划线除外)被进一步传递到程序中。当我在主程序中实现它之前对其进行测试时,执行此操作的部分工作正常(如下所示),但是当我现在运行它时,输出只是重复了很多次,直到出现分段错误,我不能我的生活弄清楚是什么导致了这种情况发生,所以如果有人能帮助解决这个问题,我将非常感激,谢谢!同样为了便于查看它在主程序中的实现位置,我截取了它在主代码http://i.imgur.com/dKLgx.png中的位置的屏幕截图,并为较长的帖子长度道歉

austin@Ruby:~/cprac$ ./words
[hel123_lo]

测试有效的代码

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

int main(void) {
    int i;
    char *p;
    char stg[] = "hel123*^_l!o";
    char output[200] = {0x00};
    int index = 0;


    p = stg;
    while( *p )
    {
    if (isalnum(*p) || *p == '_')
    {
        output[index++] = *p;
    }
    p++;
    }

printf("[%s]\n", output);
return 0;
}

主要代码

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

#ifndef max
#define max(a, b) ((a)>(b))? (a) : (b)
#endif

long GetFileSize(FILE *fp){
long fsize = 0;

fseek(fp,0,SEEK_END);
fsize = ftell(fp);
fseek(fp,0,SEEK_SET);//reset stream position!!

return fsize;
}
char *lastline(char *filepath){
FILE *fp;
char buff[4096+1];
int size,i;
long fsize;
if(NULL==(fp=fopen(filepath, "r"))){
    perror("file cannot open at lastline");
    return NULL;
}
fsize= -1L*GetFileSize(fp);
if(size=fseek(fp, max(fsize, -4096L), SEEK_END)){
    perror("cannot seek");
    exit(0);
}
size=fread(buff, sizeof(char), 4096, fp);
fclose(fp);
buff[size] = '\0';
i=size-1;
if(buff[i]=='\n'){
    buff[i] = '\0';
}
while(i >=0 && buff[i] != '\n')
    --i;
++i;
return strdup(&buff[i]);
}

int main(int argc, char *argv[], char *envp[]){
    char *last;
    char *name;
    char field_x[128];
    char field_y[128];
    char field_z[128];
    char field_world[128];
    char field_cause[128];
    char field_killer[128];
    name = getenv("MCEXEC_PLAYERNAME");
    char *filename;
    char *p;
    char *ispvp;
    // m
    int i;
    char *f;
    char output[200] = {0x00};
    int index = 0;
    filename = malloc(sizeof "/home/minecraft/freedonia/playerdata/deathlog-.txt" - 1 +     strlen(name) + 1);
    if (!filename) exit(EXIT_FAILURE);
    snprintf(filename,4096,"/home/minecraft/freedonia/playerdata/deathlog-  %s.txt",name);
    last = lastline(filename);
    if( last != NULL ) {
    printf( "%s\n", last );
    sscanf(last, "%*[^:]:%*[^:]:%*[^:]:%127[^:]:", field_x);
    sscanf(last, "%*[^:]:%*[^:]:%*[^:]:%*[^:]:%127[^:]:", field_y);
    sscanf(last, "%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%127[^:]:", field_z);
    sscanf(last, "%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%127[^:]:", field_world);
    sscanf(last, "%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%127[^:]:", field_cause);
    p = strchr(field_cause, '_');
    printf( "X coord: %s\n", field_x);
    printf( "Y coord: %s\n", field_y);
    printf( "Z coord: %s\n", field_z);
    printf( "World: %s\n", field_world);
    printf( "Cause: %s\n", field_cause);
    while (p != NULL) {
    ispvp = "true";
   // printf ("found at %d\n",p - field_cause + 1);
    sscanf(field_cause, "%*[^_]_%128[^_]_", field_killer);
    printf( "%s\n", field_killer);
    f = field_cause;
    while( *f )
    {
      if (isalnum(*f) || *f == '_')
      {
        output[index++] = *f;
      }
      f++;
    }

    printf("[%s]\n", output);
 //   p = strchr(p + 1, '_');
    }

    }
//    printf("\"%s\"\n", last);
free(last);
return 0;
}
4

1 回答 1

1
    while (p != NULL) {
    ispvp = "true";
    // printf ("found at %d\n",p - field_cause + 1);
    sscanf(field_cause, "%*[^_]_%128[^_]_", field_killer);
    printf( "%s\n", field_killer);
    f = field_cause;
    while( *f )
    {
      if (isalnum(*f) || *f == '_')
      {
        output[index++] = *f;
      }
      f++;
    }

    printf("[%s]\n", output);
    //   p = strchr(p + 1, '_');
    }

P 在这个块中永远不会改变,所以它总是 != NULL。

于 2012-05-14T01:01:16.830 回答