Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Static analysis - custom rule for ascii characters >127

SLT
SLT Posts: 9
edited April 2018 in C/C++test

Hi,
I have to create a custom rule that checks that ascii characters > 127 are not present.
I have some issues doing it.
Can someone help me?

The rule is:
"Define identifiers shall be as following:
< DEFINE_NAME > = D _ < USER_NAME >
Where:
< USER_NAME > length is at most 12 characters and not composed by ascii characters > 127."

Thanks.

Comments

  • mstaron
    mstaron Posts: 35

    You can try to use the following python method:

    use correct indentations, that were removed automatically in this post
    def checkCharValue( node, context ):
    stringName = node.getProperty( 'name' )
    for i in range(0, len(stringName)):
    charName = stringName[i]
    charValue = ord(stringName[i])
    if charValue > 127:
    context.report("String %s contains character '%s' with value greater than 127: (%d)" % (stringName, charName, charValue))
    return 1

    This method can be added to the appropriate node (in C++Text rule) e.g. for 'String Constant'.
    The example of rule: