【问题标题】:Bus error 10 on Mac while concatenating the set of chars连接字符集时 Mac 上的总线错误 10
【发布时间】:2015-04-25 22:56:34
【问题描述】:

你能告诉我这段代码有什么问题吗:

#include <stdio.h>
#if __STDC_VERSION == 199901L
#include <stdint.h>
#include <string.h>
#else
/* change to fit the compiler */
typedef unsigned char uint8_t;
typedef unsigned long uint32_t;
#endif
#define IPV4
#if defined(IPV4)
#define NBYTES 4
#elif defined(IPV6)
#error IPV6 not supported
#else
#error No valid IP version protocol defined
#endif

char* concat(char *s1, char *s2)
{
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    //in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

int main()
{
    uint32_t ipAddress = 167840383;
    uint8_t  octet[NBYTES];
    int x;
    char buffer[4];
    char buffer2[100];

    for (x = 0; x < NBYTES; x++)
    {
        octet[x] = (ipAddress >> (x * 8)) & (uint8_t)-1;
    }
    for (x = NBYTES - 1; x >= 0; --x)
    {
        // sprintf("%d", octet[x]);
        sprintf(buffer2, "%d", octet[x]);
        // if (x > 0) printf(".");
        if (x > 0) {
            char * temp = concat(buffer2, ".");
            strcpy(buffer2, temp);
        }
    }
    sprintf("\n%s", buffer2);
    return 0;
}

我正在尝试将此代码重写为一个函数,该函数将 IP 的每个部分连接起来,该函数是使用 sprintf 从 32 位整数到 buffer2 char 变量获取的,并返回具有 IP 十进制表示的转换字符串,但同时我正在运行已编译的应用程序,它打印错误:“总线错误:10”。

调试信息如下:

MacBook-Air-Pavel:gamind paus$ gcc -o int2ip int2ip.c 
int2ip.c:21:20: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
                   ^
int2ip.c:21:20: note: please include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
int2ip.c:21:27: warning: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)'
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
                          ^
int2ip.c:21:27: note: please include the header <string.h> or explicitly provide a declaration for 'strlen'
int2ip.c:23:5: warning: implicitly declaring library function 'strcpy' with type 'char *(char *, const char *)'
    strcpy(result, s1);
    ^
int2ip.c:23:5: note: please include the header <string.h> or explicitly provide a declaration for 'strcpy'
int2ip.c:24:5: warning: implicitly declaring library function 'strcat' with type 'char *(char *, const char *)'
    strcat(result, s2);
    ^
int2ip.c:24:5: note: please include the header <string.h> or explicitly provide a declaration for 'strcat'
int2ip.c:50:21: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
    sprintf("\n%s", buffer2);
                    ^~~~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
  __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
                                                       ^
5 warnings generated.

我不知道为什么。你能帮帮我吗?

【问题讨论】:

  • 能否调试并告诉我们错误发生在哪一行?
  • 您显示的输出不是调试信息,而是编译器输出。请阅读并遵循建议:包括&lt;stdlib.h&gt;&lt;string.h&gt;。你不是在这里把事情复杂化了吗?您的结果最多可以有 24 个字符(对于 IP6,包括终止空字符),因此不需要动态分配。此外,没有free 的分配会导致内存泄漏。

标签: c arrays string function int


【解决方案1】:
sprintf("\n%s", buffer2);

sprintf 是错误的;您需要交换两个参数。此外,请考虑始终使用snprintf


另外,inet_ntop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多