Skip to content

Commit

Permalink
Replace strdup with xrl_strdup
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoonj committed Jun 13, 2020
1 parent 3a18b41 commit 108080d
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 53 deletions.
4 changes: 3 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ libprdata_la_SOURCES = \
radrate.c \
auger_trans.c \
kissel_pe.c \
cross_sections.c
cross_sections.c \
xraylib-aux.c \
xraylib-aux.h

if !OS_WINDOWS
libprdata_la_LIBADD = -lm
Expand Down
6 changes: 3 additions & 3 deletions src/crystal_diffraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Crystal_Struct* Crystal_MakeCopy (Crystal_Struct *crystal, xrl_error **error) {
}

*crystal_out = *crystal;
crystal_out->name = strdup(crystal->name);
crystal_out->name = xrl_strdup(crystal->name);
n = crystal->n_atom * sizeof(Crystal_Atom);
crystal_out->atom = malloc(n);
if (crystal_out->atom == NULL) {
Expand Down Expand Up @@ -195,7 +195,7 @@ char** Crystal_GetCrystalsList(Crystal_Array *c_array, int *nCrystals, xrl_error
return NULL;
}
for (i = 0 ; i < c_array->n_crystal ; i++)
rv[i] = strdup(c_array->crystal[i].name);
rv[i] = xrl_strdup(c_array->crystal[i].name);

rv[c_array->n_crystal] = NULL;

Expand Down Expand Up @@ -587,7 +587,7 @@ int Crystal_ReadFile(const char* file_name, Crystal_Array* c_array, xrl_error **
}
crystal = &(c_array->crystal[c_array->n_crystal++]);

crystal->name = strdup(compound);
crystal->name = xrl_strdup(compound);

/*
* Parse lines of the crystal definition before list of atom positions.
Expand Down
18 changes: 9 additions & 9 deletions src/xrayfiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void XRayInit(void)


for (i = 0 ; i < MENDEL_MAX ; i++) {
MendelArraySorted[i].name = strdup(MendelArray[i].name);
MendelArraySorted[i].name = xrl_strdup(MendelArray[i].name);
MendelArraySorted[i].Zatom = MendelArray[i].Zatom;
}

Expand Down Expand Up @@ -296,7 +296,7 @@ void XRayInit(void)
if (nerror_lines == 0) {
fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name);
error_lines = malloc(sizeof(char *) * ++nerror_lines);
error_lines[0] = strdup(line_name);
error_lines[0] = xrl_strdup(line_name);
}
else {
found_error_line = 0;
Expand All @@ -309,7 +309,7 @@ void XRayInit(void)
if (!found_error_line) {
fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name);
error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines);
error_lines[nerror_lines-1] = strdup(line_name);
error_lines[nerror_lines-1] = xrl_strdup(line_name);
}
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ void XRayInit(void)
if (nerror_lines == 0) {
fprintf(stderr,"%s is not present in the shellnames database: adjust xraylib-shells.h and xrayvars.c/h\n",shell_name);
error_lines = malloc(sizeof(char *) * ++nerror_lines);
error_lines[0] = strdup(shell_name);
error_lines[0] = xrl_strdup(shell_name);
}
else {
found_error_line = 0;
Expand All @@ -358,7 +358,7 @@ void XRayInit(void)
if (!found_error_line) {
fprintf(stderr,"%s is not present in the shellnames database: adjust xraylib-shells.h and xrayvars.c/h\n",shell_name);
error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines);
error_lines[nerror_lines-1] = strdup(shell_name);
error_lines[nerror_lines-1] = xrl_strdup(shell_name);
}
}
}
Expand Down Expand Up @@ -456,7 +456,7 @@ void XRayInit(void)
if (nerror_lines == 0) {
fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name);
error_lines = malloc(sizeof(char *) * ++nerror_lines);
error_lines[0] = strdup(line_name);
error_lines[0] = xrl_strdup(line_name);
}
else {
found_error_line = 0;
Expand All @@ -469,7 +469,7 @@ void XRayInit(void)
if (!found_error_line) {
fprintf(stderr, "%s is not present in the linenames database: adjust xraylib-lines.h and xrayvars.c/h\n",line_name);
error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines);
error_lines[nerror_lines-1] = strdup(line_name);
error_lines[nerror_lines-1] = xrl_strdup(line_name);
}
}
}
Expand Down Expand Up @@ -512,7 +512,7 @@ void XRayInit(void)
if (nerror_lines == 0) {
fprintf(stderr, "%s is not present in the Auger transition names database: adjust xraylib-auger.h and xrayvars.c/h\n",auger_name);
error_lines = malloc(sizeof(char *) * ++nerror_lines);
error_lines[0] = strdup(auger_name);
error_lines[0] = xrl_strdup(auger_name);
}
else {
found_error_line = 0;
Expand All @@ -525,7 +525,7 @@ void XRayInit(void)
if (!found_error_line) {
fprintf(stderr, "%s is not present in the Auger transition names database: adjust xraylib-auger.h and xrayvars.c/h\n",auger_name);
error_lines= (char **) realloc((char **) error_lines,sizeof(char *)*++nerror_lines);
error_lines[nerror_lines-1] = strdup(auger_name);
error_lines[nerror_lines-1] = xrl_strdup(auger_name);
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/xraylib-aux.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2010, Tom Schoonjans
Copyright (c) 2010-2020, Tom Schoonjans
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand All @@ -17,20 +17,28 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED
#include <string.h>

#ifndef HAVE_STRDUP
char *strdup(const char *str) {
char *xrl_strdup(const char *str) {
char *dup= (char *)malloc( strlen(str)+1 );
if (dup) strcpy(dup,str);
return dup;
}
#else
char *xrl_strdup(const char *str) {
return strdup(str);
}
#endif

#ifndef HAVE_STRNDUP
char *strndup(const char *str, size_t len) {
char *xrl_strndup(const char *str, size_t len) {
char *dup= (char *)malloc( len+1 );
if (dup) {
strncpy(dup,str,len);
dup[len]= '\0';
}
return dup;
}
#else
char *xrl_strndup(const char *str, size_t len) {
return strndup(str, len);
}
#endif
21 changes: 3 additions & 18 deletions src/xraylib-aux.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2010, Tom Schoonjans
Copyright (c) 2010-2020, Tom Schoonjans
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand All @@ -14,25 +14,10 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED
#ifndef _XRAYLIB_AUX_H
#define _XRAYLIB_AUX_H

#include "config.h"
#include <stddef.h>

/*
*
* The following two functions are ISO C extensions and are not available on all platforms.
* We will use our own functions if they are absent from libc.
*
*/


#ifndef HAVE_STRDUP
extern char *strdup(const char *str);
#endif

#ifndef HAVE_STRNDUP
extern char *strndup(const char *str, size_t len);
#endif

char *xrl_strdup(const char *str);

char *xrl_strndup(const char *str, size_t len);

#endif
5 changes: 3 additions & 2 deletions src/xraylib-error.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans 'AS IS' AND ANY EXPRESS OR IMPLIED W
*/

#include "config.h"
#include "xraylib-aux.h"
#include "xraylib-error-private.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -77,7 +78,7 @@ xrl_error* xrl_error_new_literal(xrl_error_code code, const char *message) {

error = malloc(sizeof(xrl_error));
error->code = code;
error->message = strdup(message);
error->message = xrl_strdup(message);

return error;
}
Expand All @@ -104,7 +105,7 @@ xrl_error* xrl_error_copy(const xrl_error *error) {

copy->message = NULL;
if (error->message)
copy->message = strdup(error->message);
copy->message = xrl_strdup(error->message);

return copy;
}
Expand Down
8 changes: 4 additions & 4 deletions src/xraylib-nist-compounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct compoundDataNIST *GetCompoundDataNISTByName(const char compoundString[],
xrl_set_error_literal(error, XRL_ERROR_INVALID_ARGUMENT, "compoundString cannot be NULL");
return NULL;
}
key->name = strdup(compoundString);
key->name = xrl_strdup(compoundString);


#ifndef _WIN32
Expand All @@ -64,7 +64,7 @@ struct compoundDataNIST *GetCompoundDataNISTByName(const char compoundString[],
free(key->name);

if (rv != NULL) {
key->name = strdup(rv->name);
key->name = xrl_strdup(rv->name);
key->nElements = rv->nElements;
key->Elements = malloc(sizeof(int)*rv->nElements);
memcpy(key->Elements, rv->Elements, sizeof(int)*rv->nElements);
Expand Down Expand Up @@ -93,7 +93,7 @@ struct compoundDataNIST *GetCompoundDataNISTByIndex(int compoundIndex, xrl_error
xrl_set_error(error, XRL_ERROR_MEMORY, MALLOC_ERROR, strerror(errno));
return NULL;
}
key->name = strdup(compoundDataNISTList[compoundIndex].name);
key->name = xrl_strdup(compoundDataNISTList[compoundIndex].name);
key->nElements = compoundDataNISTList[compoundIndex].nElements;
key->Elements = malloc(sizeof(int)*compoundDataNISTList[compoundIndex].nElements);
memcpy(key->Elements, compoundDataNISTList[compoundIndex].Elements, sizeof(int)*compoundDataNISTList[compoundIndex].nElements);
Expand All @@ -118,7 +118,7 @@ char **GetCompoundDataNISTList(int *nCompounds, xrl_error **error) {
}

for (i = 0 ; i < nCompoundDataNISTList; i++)
rv[i] = strdup(compoundDataNISTList[i].name);
rv[i] = xrl_strdup(compoundDataNISTList[i].name);

rv[nCompoundDataNISTList] = NULL;

Expand Down
16 changes: 8 additions & 8 deletions src/xraylib-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca,
for (i = 0 ; i < nuppers ; i++) {
if (islower(upper_locs[i][1]) && !islower(upper_locs[i][2])) {
/*second letter is lowercase and third one isn't -> valid */
tempElement = strndup(upper_locs[i],2);
tempElement = xrl_strndup(upper_locs[i],2);
/*get corresponding atomic number */
res = bsearch(tempElement, MendelArraySorted, MENDEL_MAX, sizeof(struct MendelElement), matchMendelElement);
if (res == NULL) {
Expand All @@ -147,7 +147,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca,
tempnAtoms = 1.0;
}
else {
tempSubstring = strndup(upper_locs[i] + 2, j - 2);
tempSubstring = xrl_strndup(upper_locs[i] + 2, j - 2);
tempnAtoms = strtod(tempSubstring, &endPtr);
if (endPtr != tempSubstring+strlen(tempSubstring)) {
xrl_set_error(error, XRL_ERROR_INVALID_ARGUMENT, "Invalid chemical formula: could not convert subscript %s to a real number", tempSubstring);
Expand All @@ -165,7 +165,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca,
}
else if (!islower(upper_locs[i][1])) {
/*second letter is not lowercase -> valid */
tempElement = strndup(upper_locs[i], 1);
tempElement = xrl_strndup(upper_locs[i], 1);
/*get corresponding atomic number */
res = bsearch(tempElement, MendelArraySorted, MENDEL_MAX, sizeof(struct MendelElement), matchMendelElement);
if (res == NULL) {
Expand All @@ -188,7 +188,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca,
tempnAtoms = 1.0;
}
else {
tempSubstring = strndup(upper_locs[i] + 1, j - 1);
tempSubstring = xrl_strndup(upper_locs[i] + 1, j - 1);
tempnAtoms = strtod(tempSubstring, &endPtr);
if (endPtr != tempSubstring + strlen(tempSubstring)) {
xrl_set_error(error, XRL_ERROR_INVALID_ARGUMENT, "Invalid chemical formula: could not convert subscript %s to a real number", tempSubstring);
Expand Down Expand Up @@ -241,7 +241,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca,
/*handle the brackets... */
for (i = 0 ; i < nbracket_pairs ; i++) {
tempBracketAtoms = malloc(sizeof(struct compoundAtoms));
tempBracketString = strndup(brackets_begin_locs[i]+1,(size_t) (brackets_end_locs[i]-brackets_begin_locs[i]-1));
tempBracketString = xrl_strndup(brackets_begin_locs[i]+1,(size_t) (brackets_end_locs[i]-brackets_begin_locs[i]-1));
tempBracketAtoms->nElements = 0;
tempBracketAtoms->singleElements = NULL;
/*recursive call */
Expand All @@ -265,7 +265,7 @@ static int CompoundParserSimple(char compoundString[], struct compoundAtoms *ca,
tempnAtoms = 1.0;
}
else {
tempSubstring = strndup(brackets_end_locs[i]+1,j-1);
tempSubstring = xrl_strndup(brackets_end_locs[i]+1,j-1);
tempnAtoms = strtod(tempSubstring,&endPtr);
if (endPtr != tempSubstring+strlen(tempSubstring)) {
xrl_set_error(error, XRL_ERROR_INVALID_ARGUMENT, "Invalid chemical formula: could not convert subscript %s to a real number", tempSubstring);
Expand Down Expand Up @@ -337,7 +337,7 @@ struct compoundData* CompoundParser(const char compoundString[], xrl_error **err
/* the locale is changed to default locale because we'll be using strtod later on */
backup_locale = setlocale(LC_NUMERIC, "C");

compoundStringCopy = strdup(compoundString);
compoundStringCopy = xrl_strdup(compoundString);

rvCPS = CompoundParserSimple(compoundStringCopy, &ca, error);

Expand Down Expand Up @@ -458,7 +458,7 @@ char *AtomicNumberToSymbol(int Z, xrl_error **error) {
return NULL;
}

return strdup(MendelArray[Z-1].name);
return xrl_strdup(MendelArray[Z-1].name);
}

int SymbolToAtomicNumber(const char *symbol, xrl_error **error) {
Expand Down
8 changes: 4 additions & 4 deletions src/xraylib-radionuclides.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct radioNuclideData *GetRadioNuclideDataByName(const char radioNuclideString
xrl_set_error_literal(error, XRL_ERROR_INVALID_ARGUMENT, "radioNuclideString cannot be NULL");
return NULL;
}
key->name = strdup(radioNuclideString);
key->name = xrl_strdup(radioNuclideString);

#ifndef _WIN32
nelp = nNuclideDataList;
Expand All @@ -66,7 +66,7 @@ struct radioNuclideData *GetRadioNuclideDataByName(const char radioNuclideString
free(key->name);

if (rv != NULL) {
key->name = strdup(rv->name);
key->name = xrl_strdup(rv->name);
key->Z = rv->Z;
key->A = rv->A;
key->N = rv->N;
Expand Down Expand Up @@ -104,7 +104,7 @@ struct radioNuclideData *GetRadioNuclideDataByIndex(int radioNuclideIndex, xrl_e
xrl_set_error(error, XRL_ERROR_MEMORY, MALLOC_ERROR, strerror(errno));
return NULL;
}
key->name = strdup(nuclideDataList[radioNuclideIndex].name);
key->name = xrl_strdup(nuclideDataList[radioNuclideIndex].name);
key->Z = nuclideDataList[radioNuclideIndex].Z;
key->A = nuclideDataList[radioNuclideIndex].A;
key->N = nuclideDataList[radioNuclideIndex].N;
Expand Down Expand Up @@ -137,7 +137,7 @@ char **GetRadioNuclideDataList(int *nRadioNuclides, xrl_error **error) {
}

for (i = 0 ; i < nNuclideDataList; i++)
rv[i] = strdup(nuclideDataList[i].name);
rv[i] = xrl_strdup(nuclideDataList[i].name);

rv[nNuclideDataList] = NULL;

Expand Down
3 changes: 2 additions & 1 deletion tests/test-crystal_diffraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED

#include <config.h>
#include "xraylib.h"
#include "xraylib-aux.h"
#include "xraylib-error-private.h"
#ifdef NDEBUG
#undef NDEBUG
Expand Down Expand Up @@ -119,7 +120,7 @@ int main(int argc, char **argv) {
assert(error == NULL);
assert(cs_copy != NULL);
free(cs_copy->name);
cs_copy->name = strdup("Diamond-copy");
cs_copy->name = xrl_strdup("Diamond-copy");

rv = Crystal_AddCrystal(cs, NULL, &error);
assert(rv == 0);
Expand Down

0 comments on commit 108080d

Please sign in to comment.