Program: 197161
Project: 197161
Buggy source:
private void grantPermissionsLPw(PackageParser.Package pkg,boolean replace){
  final PackageSetting ps=(PackageSetting)pkg.mExtras;
  if (ps == null) {
    return;
  }
  final GrantedPermissions gp=ps.sharedUser != null ? ps.sharedUser : ps;
  HashSet<String> origPermissions=gp.grantedPermissions;
  boolean changedPermission=false;
  if (replace) {
    ps.permissionsFixed=false;
    if (gp == ps) {
      origPermissions=new HashSet<String>(gp.grantedPermissions);
      gp.grantedPermissions.clear();
      gp.gids=mGlobalGids;
    }
  }
  if (gp.gids == null) {
    gp.gids=mGlobalGids;
  }
  final int N=pkg.requestedPermissions.size();
  for (int i=0; i < N; i++) {
    final String name=pkg.requestedPermissions.get(i);
    final boolean required=pkg.requestedPermissionsRequired.get(i);
    final BasePermission bp=mSettings.mPermissions.get(name);
    if (DEBUG_INSTALL) {
      if (gp != ps) {
        Log.i(TAG,"String_Node_Str" + pkg.packageName + "String_Node_Str"+ name+ "String_Node_Str"+ bp);
      }
    }
    if (bp == null || bp.packageSetting == null) {
      Slog.w(TAG,"String_Node_Str" + name + "String_Node_Str"+ pkg.packageName);
      continue;
    }
    final String perm=bp.name;
    boolean allowed;
    boolean allowedSig=false;
    if ((bp.protectionLevel & PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
      ArraySet<String> pkgs=mAppOpPermissionPackages.get(bp.name);
      if (pkgs == null) {
        pkgs=new ArraySet<>();
        mAppOpPermissionPackages.put(bp.name,pkgs);
      }
      pkgs.add(pkg.packageName);
    }
    final int level=bp.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
    if (level == PermissionInfo.PROTECTION_NORMAL || level == PermissionInfo.PROTECTION_DANGEROUS) {
      allowed=(required || origPermissions.contains(perm) || (isSystemApp(ps) && !isUpdatedSystemApp(ps)));
    }
 else     if (bp.packageSetting == null) {
      allowed=false;
    }
 else     if (level == PermissionInfo.PROTECTION_SIGNATURE) {
      allowed=grantSignaturePermission(perm,pkg,bp,origPermissions);
      if (allowed) {
        allowedSig=true;
      }
    }
 else {
      allowed=false;
    }
    if (DEBUG_INSTALL) {
      if (gp != ps) {
        Log.i(TAG,"String_Node_Str" + pkg.packageName + "String_Node_Str"+ perm);
      }
    }
    if (allowed) {
      if (!isSystemApp(ps) && ps.permissionsFixed) {
        if (!allowedSig && !gp.grantedPermissions.contains(perm)) {
          allowed=isNewPlatformPermissionForPackage(perm,pkg);
        }
      }
      if (allowed) {
        if (!gp.grantedPermissions.contains(perm)) {
          changedPermission=true;
          gp.grantedPermissions.add(perm);
          gp.gids=appendInts(gp.gids,bp.gids);
        }
 else         if (!ps.haveGids) {
          gp.gids=appendInts(gp.gids,bp.gids);
        }
      }
 else {
        Slog.w(TAG,"String_Node_Str" + perm + "String_Node_Str"+ pkg.packageName+ "String_Node_Str");
      }
    }
 else {
      if (gp.grantedPermissions.remove(perm)) {
        changedPermission=true;
        gp.gids=removeInts(gp.gids,bp.gids);
        Slog.i(TAG,"String_Node_Str" + perm + "String_Node_Str"+ pkg.packageName+ "String_Node_Str"+ bp.protectionLevel+ "String_Node_Str"+ Integer.toHexString(pkg.applicationInfo.flags)+ "String_Node_Str");
      }
 else       if ((bp.protectionLevel & PermissionInfo.PROTECTION_FLAG_APPOP) == 0) {
        Slog.w(TAG,"String_Node_Str" + perm + "String_Node_Str"+ pkg.packageName+ "String_Node_Str"+ bp.protectionLevel+ "String_Node_Str"+ Integer.toHexString(pkg.applicationInfo.flags)+ "String_Node_Str");
      }
    }
  }
  if ((changedPermission || replace) && !ps.permissionsFixed && !isSystemApp(ps) || isUpdatedSystemApp(ps)) {
    ps.permissionsFixed=true;
  }
  ps.haveGids=true;
}

Fixed version (ground truth):
private void grantPermissionsLPw(PackageParser.Package pkg,boolean replace,String packageOfInterest){
  final PackageSetting ps=(PackageSetting)pkg.mExtras;
  if (ps == null) {
    return;
  }
  final GrantedPermissions gp=ps.sharedUser != null ? ps.sharedUser : ps;
  HashSet<String> origPermissions=gp.grantedPermissions;
  boolean changedPermission=false;
  if (replace) {
    ps.permissionsFixed=false;
    if (gp == ps) {
      origPermissions=new HashSet<String>(gp.grantedPermissions);
      gp.grantedPermissions.clear();
      gp.gids=mGlobalGids;
    }
  }
  if (gp.gids == null) {
    gp.gids=mGlobalGids;
  }
  final int N=pkg.requestedPermissions.size();
  for (int i=0; i < N; i++) {
    final String name=pkg.requestedPermissions.get(i);
    final boolean required=pkg.requestedPermissionsRequired.get(i);
    final BasePermission bp=mSettings.mPermissions.get(name);
    if (DEBUG_INSTALL) {
      if (gp != ps) {
        Log.i(TAG,"String_Node_Str" + pkg.packageName + "String_Node_Str"+ name+ "String_Node_Str"+ bp);
      }
    }
    if (bp == null || bp.packageSetting == null) {
      if (packageOfInterest == null || packageOfInterest.equals(pkg.packageName)) {
        Slog.w(TAG,"String_Node_Str" + name + "String_Node_Str"+ pkg.packageName);
      }
      continue;
    }
    final String perm=bp.name;
    boolean allowed;
    boolean allowedSig=false;
    if ((bp.protectionLevel & PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
      ArraySet<String> pkgs=mAppOpPermissionPackages.get(bp.name);
      if (pkgs == null) {
        pkgs=new ArraySet<>();
        mAppOpPermissionPackages.put(bp.name,pkgs);
      }
      pkgs.add(pkg.packageName);
    }
    final int level=bp.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
    if (level == PermissionInfo.PROTECTION_NORMAL || level == PermissionInfo.PROTECTION_DANGEROUS) {
      allowed=(required || origPermissions.contains(perm) || (isSystemApp(ps) && !isUpdatedSystemApp(ps)));
    }
 else     if (bp.packageSetting == null) {
      allowed=false;
    }
 else     if (level == PermissionInfo.PROTECTION_SIGNATURE) {
      allowed=grantSignaturePermission(perm,pkg,bp,origPermissions);
      if (allowed) {
        allowedSig=true;
      }
    }
 else {
      allowed=false;
    }
    if (DEBUG_INSTALL) {
      if (gp != ps) {
        Log.i(TAG,"String_Node_Str" + pkg.packageName + "String_Node_Str"+ perm);
      }
    }
    if (allowed) {
      if (!isSystemApp(ps) && ps.permissionsFixed) {
        if (!allowedSig && !gp.grantedPermissions.contains(perm)) {
          allowed=isNewPlatformPermissionForPackage(perm,pkg);
        }
      }
      if (allowed) {
        if (!gp.grantedPermissions.contains(perm)) {
          changedPermission=true;
          gp.grantedPermissions.add(perm);
          gp.gids=appendInts(gp.gids,bp.gids);
        }
 else         if (!ps.haveGids) {
          gp.gids=appendInts(gp.gids,bp.gids);
        }
      }
 else {
        if (packageOfInterest == null || packageOfInterest.equals(pkg.packageName)) {
          Slog.w(TAG,"String_Node_Str" + perm + "String_Node_Str"+ pkg.packageName+ "String_Node_Str");
        }
      }
    }
 else {
      if (gp.grantedPermissions.remove(perm)) {
        changedPermission=true;
        gp.gids=removeInts(gp.gids,bp.gids);
        Slog.i(TAG,"String_Node_Str" + perm + "String_Node_Str"+ pkg.packageName+ "String_Node_Str"+ bp.protectionLevel+ "String_Node_Str"+ Integer.toHexString(pkg.applicationInfo.flags)+ "String_Node_Str");
      }
 else       if ((bp.protectionLevel & PermissionInfo.PROTECTION_FLAG_APPOP) == 0) {
        if (packageOfInterest == null || packageOfInterest.equals(pkg.packageName)) {
          Slog.w(TAG,"String_Node_Str" + perm + "String_Node_Str"+ pkg.packageName+ "String_Node_Str"+ bp.protectionLevel+ "String_Node_Str"+ Integer.toHexString(pkg.applicationInfo.flags)+ "String_Node_Str");
        }
      }
    }
  }
  if ((changedPermission || replace) && !ps.permissionsFixed && !isSystemApp(ps) || isUpdatedSystemApp(ps)) {
    ps.permissionsFixed=true;
  }
  ps.haveGids=true;
}