apps/x509.c:962: error: NULL_DEREFERENCE
  pointer `buf` last assigned on line 961 could be null and is dereferenced by call to `memcpy()` at line 962, column 9.
Showing all 26 steps of the trace


apps/x509.c:950:1: start of procedure x509_load_serial()
948.   }
949.   
950. > static ASN1_INTEGER *x509_load_serial(const char *CAfile,
951.                                         const char *serialfile, int create)
952.   {

apps/x509.c:953:5: 
951.                                         const char *serialfile, int create)
952.   {
953. >     char *buf = NULL;
954.       ASN1_INTEGER *bs = NULL;
955.       BIGNUM *serial = NULL;

apps/x509.c:954:5: 
952.   {
953.       char *buf = NULL;
954. >     ASN1_INTEGER *bs = NULL;
955.       BIGNUM *serial = NULL;
956.   

apps/x509.c:955:5: 
953.       char *buf = NULL;
954.       ASN1_INTEGER *bs = NULL;
955. >     BIGNUM *serial = NULL;
956.   
957.       if (serialfile == NULL) {

apps/x509.c:957:9: Taking true branch
955.       BIGNUM *serial = NULL;
956.   
957.       if (serialfile == NULL) {
               ^
958.           const char *p = strrchr(CAfile, '.');
959.           size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);

apps/x509.c:958:9: 
956.   
957.       if (serialfile == NULL) {
958. >         const char *p = strrchr(CAfile, '.');
959.           size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
960.   

apps/x509.c:959:22: Condition is true
957.       if (serialfile == NULL) {
958.           const char *p = strrchr(CAfile, '.');
959.           size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
                            ^
960.   
961.           buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");

apps/x509.c:959:9: 
957.       if (serialfile == NULL) {
958.           const char *p = strrchr(CAfile, '.');
959. >         size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
960.   
961.           buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");

apps/x509.c:961:9: 
959.           size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
960.   
961. >         buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
962.           memcpy(buf, CAfile, len);
963.           memcpy(buf + len, POSTFIX, sizeof(POSTFIX));

test/testutil/apps_mem.c:14:1: start of procedure app_malloc()
  12.   /* shim that avoids sucking in too much from apps/apps.c */
  13.   
  14. > void* app_malloc(int sz, const char *what)
  15.   {
  16.       void *vp = OPENSSL_malloc(sz);

test/testutil/apps_mem.c:16:5: 
  14.   void* app_malloc(int sz, const char *what)
  15.   {
  16. >     void *vp = OPENSSL_malloc(sz);
  17.   
  18.       return vp;

crypto/mem.c:192:1: start of procedure CRYPTO_malloc()
    190.   #endif
    191.   
    192. > void *CRYPTO_malloc(size_t num, const char *file, int line)
    193.   {
    194.       void *ret = NULL;

crypto/mem.c:194:5: 
    192.   void *CRYPTO_malloc(size_t num, const char *file, int line)
    193.   {
    194. >     void *ret = NULL;
    195.   
    196.       INCREMENT(malloc_count);

crypto/mem.c:197:9: Taking true branch
    195.   
    196.       INCREMENT(malloc_count);
    197.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
                   ^
    198.           return malloc_impl(num, file, line);
    199.   

crypto/mem.c:197:32: Taking false branch
    195.   
    196.       INCREMENT(malloc_count);
    197.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
                                          ^
    198.           return malloc_impl(num, file, line);
    199.   

crypto/mem.c:200:9: Taking false branch
    198.           return malloc_impl(num, file, line);
    199.   
    200.       if (num == 0)
                   ^
    201.           return NULL;
    202.   

crypto/mem.c:204:9: Taking true branch
    202.   
    203.       FAILTEST();
    204.       if (allow_customize) {
                   ^
    205.           /*
    206.            * Disallow customization after the first allocation. We only set this

crypto/mem.c:210:9: 
    208.            * allocation.
    209.            */
    210. >         allow_customize = 0;
    211.       }
    212.   #if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)

crypto/mem.c:221:5: 
    219.       }
    220.   #else
    221. >     (void)(file); (void)(line);
    222.       ret = malloc(num);
    223.   #endif

crypto/mem.c:221:19: 
    219.       }
    220.   #else
    221. >     (void)(file); (void)(line);
    222.       ret = malloc(num);
    223.   #endif

crypto/mem.c:222:5: 
    220.   #else
    221.       (void)(file); (void)(line);
    222. >     ret = malloc(num);
    223.   #endif
    224.   

crypto/mem.c:225:5: 
    223.   #endif
    224.   
    225. >     return ret;
    226.   }
    227.   

crypto/mem.c:226:1: return from a call to CRYPTO_malloc
    224.   
    225.       return ret;
    226. > }
    227.   
    228.   void *CRYPTO_zalloc(size_t num, const char *file, int line)

test/testutil/apps_mem.c:18:5: 
  16.       void *vp = OPENSSL_malloc(sz);
  17.   
  18. >     return vp;
  19.   }

test/testutil/apps_mem.c:19:1: return from a call to app_malloc
  17.   
  18.       return vp;
  19. > }

apps/x509.c:962:9: 
960.   
961.           buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
962. >         memcpy(buf, CAfile, len);
963.           memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
964.           serialfile = buf;

static ASN1_INTEGER *x509_load_serial(const char *CAfile,
                                      const char *serialfile, int create)
{
    char *buf = NULL;
    ASN1_INTEGER *bs = NULL;
    BIGNUM *serial = NULL;
    if (serialfile == NULL) {
        const char *p = strrchr(CAfile, '.');
        size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);
        buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");
        memcpy(buf, CAfile, len);
        memcpy(buf + len, POSTFIX, sizeof(POSTFIX));
        serialfile = buf;
    }
    serial = load_serial(serialfile, create, NULL);
    if (serial == NULL)
        goto end;
    if (!BN_add_word(serial, 1)) {
        BIO_printf(bio_err, "add_word failure\n");
        goto end;
    }
    if (!save_serial(serialfile, NULL, serial, &bs))
        goto end;
 end:
    OPENSSL_free(buf);
    BN_free(serial);
    return bs;
}
['static ASN1_INTEGER *x509_load_serial(const char *CAfile,\n                                      const char *serialfile, int create)\n{\n    char *buf = NULL;\n    ASN1_INTEGER *bs = NULL;\n    BIGNUM *serial = NULL;\n    if (serialfile == NULL) {\n        const char *p = strrchr(CAfile, \'.\');\n        size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile);\n        buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer");\n        memcpy(buf, CAfile, len);\n        memcpy(buf + len, POSTFIX, sizeof(POSTFIX));\n        serialfile = buf;\n    }\n    serial = load_serial(serialfile, create, NULL);\n    if (serial == NULL)\n        goto end;\n    if (!BN_add_word(serial, 1)) {\n        BIO_printf(bio_err, "add_word failure\\n");\n        goto end;\n    }\n    if (!save_serial(serialfile, NULL, serial, &bs))\n        goto end;\n end:\n    OPENSSL_free(buf);\n    BN_free(serial);\n    return bs;\n}', 'void* app_malloc(int sz, const char *what)\n{\n    void *vp = OPENSSL_malloc(sz);\n    return vp;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    INCREMENT(malloc_count);\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    if (allow_customize) {\n        allow_customize = 0;\n    }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    (void)(file); (void)(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']